Created
May 20, 2014 12:48
-
-
Save designermonkey/13f91060344be50f224f to your computer and use it in GitHub Desktop.
Example of using the containers in Symphony
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| // Usage of Core | |
| namespace SymphonyCMS; | |
| class MyService | |
| { | |
| /** | |
| * Constructor expects a tailored subset of core components | |
| * @param Core $core | |
| */ | |
| public function __construct(Container $container) | |
| { | |
| $this->container = $container; | |
| } | |
| } | |
| $container = new Container; | |
| /** | |
| * We can create a service that uses a tailored subset of services and/or values from the core | |
| * container. | |
| */ | |
| $container['MyService'] = function ($container) { | |
| $subSet = $container->subSet([ | |
| 'list', | |
| 'of', | |
| 'required', | |
| 'services' | |
| ]); | |
| return new MyService($subSet); | |
| } | |
| /** | |
| * Or we can create a service that takes the entire container. | |
| */ | |
| $container['MyService'] = function ($container) { | |
| return new MyService($container); | |
| } | |
| /** | |
| * Or we can create a service that takes a single dependency | |
| */ | |
| $container['MyService'] = function ($container) { | |
| return new MyOtherService($container['MyService']); | |
| } | |
| /** | |
| * For backwards compatibility with Symphony CMS, we define it a little differently | |
| */ | |
| class Symphony extends Core {} | |
| // Inside Symphony, we will declare all the dependencies as such | |
| Symphony::set('Log', new Log(...)); | |
| // And call them as we currently do | |
| Symphony::Log() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment