Skip to content

Instantly share code, notes, and snippets.

@geerteltink
Last active March 10, 2018 07:54
Show Gist options
  • Save geerteltink/cfff4d3ce0957dcde08b06870878b1d8 to your computer and use it in GitHub Desktop.
Save geerteltink/cfff4d3ce0957dcde08b06870878b1d8 to your computer and use it in GitHub Desktop.
Zend Expressive Symfony console with lazy loaded services
#!/usr/bin/env php
<?php
declare(strict_types=1);
require __DIR__ . '/../vendor/autoload.php';
use Symfony\Component\Console\Application;
use Zend\ServiceManager\Proxy\LazyServiceFactory;
use Zend\ServiceManager\ServiceManager;
/** @var ServiceManager $container */
$container = require __DIR__ . '/../config/container.php';
// Lazy load command dependencies
$lazyServices = $container->get('config')['console']['lazy_services'] ?? [];
$services = [];
foreach ($lazyServices as $lazyService) {
$services['lazy_services']['class_map'][$lazyService] = $lazyService;
$services['delegators'][$lazyService] = [LazyServiceFactory::class,];
}
$container->configure($services);
// Setup console application
$app = new Application('Console application');
$commands = $container->get('config')['console']['commands'] ?? [];
foreach ($commands as $command) {
$app->add($container->get($command));
}
$app->run();
<?php
use Application\Command\I18nUpdateCommand;
use Application\Command\TwigLintCommand;
use Application\Command\UserRegisterCommand;
use Factory\Application\Command\I18nUpdateCommandFactory;
use Factory\Application\Command\TwigLintCommandFactory;
use Factory\Application\Command\UserRegisterCommandFactory;
return [
'dependencies' => [
'invokables' => [
],
'factories' => [
I18nUpdateCommand::class => I18nUpdateCommandFactory::class,
UserRegisterCommand::class => UserRegisterCommandFactory::class,
TwigLintCommand::class => TwigLintCommandFactory::class,
],
],
'console' => [
'commands' => [
'twig:i18n:update' => I18nUpdateCommand::class,
'user:register' => UserRegisterCommand::class,
'twig:lint' => TwigLintCommand::class,
],
'lazy_services' => [
EntityManager::class,
Psr\Log\LoggerInterface::class,
UserRepository::class,
],
],
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment