Created
October 5, 2017 12:08
-
-
Save marek-pietrzak-tg/eb7cb8c5224d984f1fa0f867255ee5a3 to your computer and use it in GitHub Desktop.
Service Manager aware Symfony Console in Zend Framework app
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
#!/usr/bin/env php | |
<?php | |
use Symfony\Component\Console\Application; | |
use Zend\Stdlib\ArrayUtils; | |
use Zend\Mvc\Service\ServiceManagerConfig; | |
use Zend\ServiceManager\ServiceManager; | |
if (!defined('APPLICATION_PATH')) { | |
define('APPLICATION_PATH', realpath(__DIR__ . '/../')); | |
} | |
if (is_file($autoload = APPLICATION_PATH . '/vendor/autoload.php')) { | |
require($autoload); | |
} else { | |
fwrite(STDERR, | |
'You must set up the project dependencies, run composer install first' . PHP_EOL | |
); | |
exit(1); | |
} | |
$configuration = include APPLICATION_PATH . '/config/application.config.php'; | |
if (file_exists(APPLICATION_PATH . '/config/development.config.php')) { | |
$configuration = ArrayUtils::merge($configuration, include APPLICATION_PATH . '/config/development.config.php'); | |
} | |
// Prepare the service manager | |
$smConfig = isset($configuration['service_manager']) ? $configuration['service_manager'] : []; | |
$smConfig = new ServiceManagerConfig($smConfig); | |
$serviceManager = new ServiceManager(); | |
$smConfig->configureServiceManager($serviceManager); | |
$serviceManager->setService('ApplicationConfig', $configuration); | |
// Load modules | |
$serviceManager->get('ModuleManager')->loadModules(); | |
$app = $serviceManager->get(Application::class); | |
$app->run(); |
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 Acme\Factory; | |
use Interop\Container\ContainerInterface; | |
use Zend\ServiceManager\Factory\FactoryInterface; | |
use Symfony\Component\Console\Application; | |
class ConsoleApplicationFactory implements FactoryInterface | |
{ | |
const APP_NAME = 'Acme'; | |
const APP_VERSION = '0.1.0'; | |
public function __invoke(ContainerInterface $container, $requestedName, array $options = null) | |
{ | |
$application = new Application(self::APP_NAME, self::APP_VERSION); | |
$application->addCommands([ | |
// add commands here, instantiate them directly or get from the service container | |
// example: $container->get(MyFancyCommand::class), | |
]); | |
return $application; | |
} | |
} |
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 Acme; | |
return [ | |
'service_manager' => [ | |
'factories' => [ | |
Symfony\Component\Console\Application::class => Factory\ConsoleApplicationFactory::class, | |
], | |
], | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment