Skip to content

Instantly share code, notes, and snippets.

@designermonkey
Created May 20, 2014 12:48
Show Gist options
  • Select an option

  • Save designermonkey/13f91060344be50f224f to your computer and use it in GitHub Desktop.

Select an option

Save designermonkey/13f91060344be50f224f to your computer and use it in GitHub Desktop.
Example of using the containers in Symphony
<?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