Source of file GenerateContainer.php
Size: 7,037 Bytes - Last Modified: 2021-12-24T06:51:36+00:00
/var/www/docs.ssmods.com/process/src/src/Console/Command/GenerateContainer.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225 | <?php namespace Heystack\Core\Console\Command; use Heystack\Core\DependencyInjection\SilverStripe\HeystackSilverStripeContainerBuilder; use RuntimeException; use Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator; use Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper; use Symfony\Component\Config\FileLocator; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input; use Symfony\Component\Console\Output; use Symfony\Component\DependencyInjection\Compiler\PassConfig; use Symfony\Component\DependencyInjection\Dumper\PhpDumper; use Symfony\Component\DependencyInjection\Dumper\YamlDumper; use Symfony\Component\DependencyInjection\Loader\YamlFileLoader; /** * Generates the dependency injection container * * This command generates the DI container from the provided services files * * It is also used via the "GenerateContainerDataObjectTrait" to regenerate the container * on certain database writes * * @package Heystack\Core\Console\Command */ class GenerateContainer extends Command { /** * @var string */ protected $basePath; /** * @var string */ protected $heystackBasePath; /** * @param string|void $basePath * @param string|void $heystackBasePath */ public function __construct($basePath = null, $heystackBasePath = null) { $this->basePath = $basePath ?: BASE_PATH; $this->heystackBasePath = $heystackBasePath ?: HEYSTACK_BASE_PATH; parent::__construct(); } /** * Configure the commands options * @return void */ protected function configure() { $this ->setName('generate-container') ->setDescription('Generate container') ->addOption( 'mode', 'm', Input\InputOption::VALUE_OPTIONAL, 'The mode (dev, live, test)', null ) ->addOption( 'debug', 'd', Input\InputOption::VALUE_NONE, 'Debug the container output' ) ->addOption( 'dev-build', 'b', Input\InputOption::VALUE_NONE, 'Build the database' ); } /** * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void * @throws \Exception */ protected function execute(Input\InputInterface $input, Output\OutputInterface $output) { if ($input->getOption('dev-build')) { $output->writeln('Building database'); $databaseAdmin = new \DatabaseAdmin(); $databaseAdmin->doBuild(true); $output->writeln('Database built'); } // Get mode if ($input->getOption('mode')) { $mode = $input->getOption('mode'); } else { $mode = \Director::get_environment_type(); } $container = $this->createContainer(); $this->loadConfig($container, $mode); $output->writeln( $this->dumpContainer($container, $mode, $input->getOption('debug')) ); } /** * @return \Heystack\Core\DependencyInjection\SilverStripe\HeystackSilverStripeContainerBuilder */ protected function createContainer() { $container = new HeystackSilverStripeContainerBuilder(); if (class_exists('Symfony\Bridge\ProxyManager\LazyProxy\Instantiator\RuntimeInstantiator')) { $container->setProxyInstantiator(new RuntimeInstantiator()); } foreach (new \DirectoryIterator($this->basePath) as $directory) { if (!$directory->isDir()) { continue; } $directory = $this->basePath . '/' . $directory; if (!file_exists($directory . '/_heystack_subsystem')) { continue; } if (file_exists($directory . '/config/extensions.php')) { $extensions = require $directory . '/config/extensions.php'; if (is_array($extensions)) { foreach ($extensions as $extension) { $container->registerExtension($extension); } } } if (file_exists($directory . '/config/compiler_passes.php')) { $compilerPasses = require $directory . '/config/compiler_passes.php'; if (is_array($compilerPasses)) { foreach ($compilerPasses as $compilerPass) { if (is_array($compilerPass)) { list($compilerPass, $type) = $compilerPass; } else { $type = PassConfig::TYPE_BEFORE_OPTIMIZATION; } $container->addCompilerPass($compilerPass, $type); } } } } return $container; } /** * @param \Heystack\Core\DependencyInjection\SilverStripe\HeystackSilverStripeContainerBuilder $container * @param string $mode * @return void */ protected function loadConfig($container, $mode) { (new YamlFileLoader( $container, new FileLocator($this->basePath . '/mysite/config/') ))->load("services_$mode.yml"); } /** * @param \Heystack\Core\DependencyInjection\SilverStripe\HeystackSilverStripeContainerBuilder $container * @param stirng $mode * @param bool|void $debug * @return string */ protected function dumpContainer(HeystackSilverStripeContainerBuilder $container, $mode, $debug = false) { $container->compile(); if ($debug) { $dumper = new YamlDumper($container); return $dumper->dump(); } else { $location = $this->heystackBasePath . '/cache/'; if (!file_exists($location)) { mkdir($location, 0777, true); } $class = "HeystackServiceContainer$mode"; $dumper = new PhpDumper($container); if (class_exists('Symfony\Bridge\ProxyManager\LazyProxy\PhpDumper\ProxyDumper')) { $dumper->setProxyDumper(new ProxyDumper()); } file_put_contents( $this->getRealPath($location) . "/$class.php", $dumper->dump([ 'class' => $class, 'base_class' => "Heystack\\Core\\DependencyInjection\\SilverStripe\\HeystackSilverStripeContainer" ]) ); return 'Container generated'; } } /** * Allows testing with vfsStream * @param string $path * @return string */ protected function getRealPath($path) { return realpath($path); } } |