Successfully reported this slideshow.

Dependency injection in CakePHP

1

Share

Upcoming SlideShare
Swiz DAO
Swiz DAO
Loading in …3
×
1 of 38
1 of 38

More Related Content

Related Books

Free with a 14 day trial from Scribd

See all

Related Audiobooks

Free with a 14 day trial from Scribd

See all

Dependency injection in CakePHP

  1. 1. Dependency Injection In CakePHP
  2. 2. Goals ● Understanding what dependency injection is, and a few patterns to achieve it. ● You know how CakePHP does dependency injection internally. ● You know what is coming next.
  3. 3. What is Dependency Injection?
  4. 4. … is a technique in which an object receives other objects that it depends on. These other objects are called dependencies. In the typical "using" relationship the receiving object is called a client and the passed (that is, "injected") object is called a service. -Wikipedia
  5. 5. OK...
  6. 6. Simple Dependency Injection
  7. 7. Constructors!
  8. 8. Constructors! public function __construct(StripeClient $stripe) { $this->stripe = $stripe; }
  9. 9. Seems simple
  10. 10. Real Projects are Complicated
  11. 11. Factories & Locators & Registries & Containers
  12. 12. Factories Make one thing really well
  13. 13. Factories $factory = new ShapeFactory($color); $triangle = $factory->makeSided(3); $square = $factory->makeSided(4);
  14. 14. Locators & Registries Get or make a thing by a name
  15. 15. Locators & Registries $registry = new LogRegistry($config); $logger = $registry->get(‘default’);
  16. 16. Containers Generalized creators of things
  17. 17. Containers $container->add(ServiceConfig::class); $container->add(HttpClient::class); $container->add(StripeService::class) ->addArgument(ServiceConfig::class) ->addArgument(HttpClient::class); $stripe = $container->get(StripeService::class);
  18. 18. Dependency Injection in CakePHP Today
  19. 19. Dependency Injection today Using helpers in your templates: echo $this->Html->link(‘Click me’); Using caching: Cache::write(‘key’, $value);
  20. 20. What about application logic?
  21. 21. Until now CakePHP didn’t help
  22. 22. Coming in 4.2 Injected Application Services
  23. 23. Application Services?
  24. 24. Application Services Contain one or more of: ● Logic that combines and coordinates multiple models. ● Logic that combines models with remote API. ● Logic that combines several remote APIs together. ● Logic that combines remote APIs and email. ● Logic that combines other services together. ● Or a variety of other things.
  25. 25. CakePHP Container Coming in 4.2
  26. 26. Registering Services public function register( ContainerInterface $container ): ContainerInterface { $container->add(...); $container->add(...); return $container; }
  27. 27. Bundling Services class BillingServiceProvider extends AbstractServiceProvider { // A list of services provided protected $provides = [ ‘AppServiceInvoiceService’, ]; public function register() { $this->getContainer()->add(...); } } // Add a service provider $container->addServiceProvider(BillingServiceProvider::class);
  28. 28. Handling Configuration use CakeCoreServiceConfig; // Add a read-only wrapper around Configure. $container->share(ServiceConfig::class); // Add scalar values you can reference by name in definitions. $container->add(‘apiKey’, ‘abc123’);
  29. 29. Getting Services If defined in the container, CakePHP will inject services into: ● Controller constructors ● Command constructors Automatically based on reflection into: ● Controller actions
  30. 30. Controller Actions // Parameters to controllers public function upgrade(BillingService $billing) { … } // Parameters to controllers with routing parameters public function modify($id, BillingService $billing) { … }
  31. 31. Experimental Features
  32. 32. Backwards Compatibility
  33. 33. Experimental Features Fewer backwards compatibility promises
  34. 34. Stabilization After a release*
  35. 35. Containers Will be the first experimental feature
  36. 36. Hopefully Stable in 4.3 Might be in 4.4 if we need to change
  37. 37. We need your help Testing and finding use cases
  38. 38. Thank you

×