-
-
Save Koopzington/d022f7b97a0cf11aaf3630ac7f37b674 to your computer and use it in GitHub Desktop.
Various examples of Dependency Injection done right using Zend Framework's ServiceManager and Psr\Container
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 | |
namespace Application\Service; | |
use DateTime; | |
class BarService | |
{ | |
protected $dateTime; | |
public function __construct(DateTime $dateTime) | |
{ | |
$this->dateTime = $dateTime; | |
} | |
} |
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 | |
namespace Application\Factory\Service; | |
use Application\Service\BarService; | |
use DateTime; | |
use Psr\Container\ContainerInterface; | |
class BarServiceFactory | |
{ | |
/** | |
* @param ContainerInterface $container | |
* @return BarService | |
*/ | |
public function __invoke(ContainerInterface $container) | |
{ | |
return new BarService(new DateTime()); | |
} | |
} |
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 | |
namespace Application\Controller; | |
use Application\Form\FooForm; | |
use Zend\Mvc\Controller\AbstractActionController; | |
final class FooController extends AbstractActionController | |
{ | |
/** @var FooForm */ | |
private $form; | |
/** @var FooService */ | |
private $fooService; | |
public function __construct(FooForm $fooForm, FooService $fooService) | |
{ | |
$this->form = $fooForm; | |
$this->fooService = $fooService; | |
} | |
} |
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 | |
namespace Application\Factory\Controller; | |
use Psr\Container\ContainerInterface; | |
use Application\Controller\FooController; | |
use Application\Form\FooForm; | |
final class FooControllerFactory | |
{ | |
public function __invoke(ContainerInterface $container): FooController | |
{ | |
// TODO: Remove this line if this is a Factory for a Service or you've upgraded to ServiceManager ^3.0 | |
/** @var ContainerInterface $container */ | |
$container = $container->getServiceLocator(); | |
return new FooController( | |
$container->get('FormElementManager')->get(FooForm::class), | |
$container->get('SomeAlias') // SomeAlias is an alias to Application\Service\FooService::class | |
); | |
} | |
} |
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 | |
namespace Application\Form; | |
use Zend\Form\Form; | |
use Zend\InputFilter\InputFilterProviderInterface; | |
final class FooForm extends Form implements InputFilterProviderInterface | |
{ | |
public function init() | |
{ | |
$this->add([ | |
'name' => 'foo', | |
]); | |
} | |
/** | |
* Should return an array specification compatible with | |
* {@link Zend\InputFilter\Factory::createInputFilter()}. | |
* | |
* @return array | |
*/ | |
public function getInputFilterSpecification(): array | |
{ | |
return [ | |
'foo' => [ | |
'required' => true, | |
], | |
]; | |
} | |
} |
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 | |
namespace Application\Service; | |
class FooService | |
{ | |
protected $barService; | |
protected $invokableService; | |
public function __construct( | |
BarService $barService, | |
InvokableService $invokableService | |
) { | |
$this->barService = $barService; | |
$this->invokableService = $invokableService; | |
} | |
} |
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 | |
namespace Application\Factory\Service; | |
use Application\Service\BarService; | |
use Application\Service\FooService; | |
use Application\Service\InvokableService; | |
use Psr\Container\ContainerInterface; | |
class FooServiceFactory | |
{ | |
/** | |
* @param ContainerInterface $container | |
* @return FooService | |
*/ | |
public function __invoke(ContainerInterface $container): FooService | |
{ | |
return new FooService( | |
$container->get(BarService::class), | |
$container->get(InvokableService::class) | |
); | |
} | |
} |
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 | |
namespace Application\Service; | |
class InvokableService | |
{ | |
} |
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 | |
use Application\Controller; | |
use Application\Factory; | |
use Application\Form; | |
use Application\Service; | |
use Zend\ServiceManager\Factory\InvokableFactory; | |
return [ | |
'controllers' => [ | |
'factories' => [ | |
Controller\FooController::class => Factory\Controller\FooControllerFactory::class, | |
], | |
], | |
'form_elements' => [ | |
'factories' => [ | |
Form\FooForm::class => InvokableFactory::class, | |
], | |
], | |
'service_manager' => [ | |
'factories' => [ | |
Service\FooService::class => Factory\Service\FooServiceFactory::class, | |
Service\BarService::class => Factory\Service\BarServiceFactory::class, | |
Service\InvokableService::class => InvokableFactory::class, | |
], | |
'aliases' => [ | |
'SomeAlias' => Service\FooService::class, | |
], | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment