3 namespace Drupal\Console\Bootstrap;
5 use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
6 use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
7 use Symfony\Component\DependencyInjection\ContainerBuilder;
8 use Symfony\Component\Config\FileLocator;
9 use Symfony\Component\Finder\Finder;
10 use Symfony\Component\Yaml\Yaml;
11 use Drupal\Console\Utils\TranslatorManager;
12 use Drupal\Console\Extension\Extension;
13 use Drupal\Console\Extension\Manager;
16 * FindCommandsCompilerPass
18 class AddServicesCompilerPass implements CompilerPassInterface
36 * AddCommandsCompilerPass constructor.
39 * @param string $appRoot
40 * @param boolean $rebuild
42 public function __construct($root, $appRoot, $rebuild = false)
45 $this->appRoot = $appRoot;
46 $this->rebuild = $rebuild;
52 public function process(ContainerBuilder $container)
54 $loader = new YamlFileLoader(
56 new FileLocator($this->root)
59 $loader->load($this->root. DRUPAL_CONSOLE_CORE . 'services.yml');
60 $loader->load($this->root. DRUPAL_CONSOLE . 'services-drupal-install.yml');
61 $loader->load($this->root. DRUPAL_CONSOLE . 'services.yml');
63 $container->get('console.configuration_manager')
64 ->loadConfiguration($this->root)
67 $cacheDirectory = $container->get('console.site')->getCacheDirectory();
68 $consoleServicesFile = $cacheDirectory.'/console.services.yml';
70 if (!$this->rebuild && file_exists($consoleServicesFile)) {
71 $loader->load($consoleServicesFile);
73 if (file_exists($consoleServicesFile)) {
74 unlink($consoleServicesFile);
76 $finder = new Finder();
81 '%s/config/services/drupal-console',
82 $this->root.DRUPAL_CONSOLE
87 foreach ($finder as $file) {
88 $loader->load($file->getPathName());
89 $servicesData = $this->extractServiceData(
96 * @var Manager $extensionManager
98 $extensionManager = $container->get('console.extension_manager');
100 * @var Extension[] $modules
102 $modules = $extensionManager->discoverModules()
108 foreach ($modules as $module) {
109 if ($module->origin == 'core') {
110 $consoleServicesExtensionFile = $this->root . DRUPAL_CONSOLE .
111 'config/services/drupal-core/'.$module->getName().'.yml';
112 if (is_file($consoleServicesExtensionFile)) {
113 $loader->load($consoleServicesExtensionFile);
114 $servicesData = $this->extractServiceData(
115 $consoleServicesExtensionFile,
121 $consoleServicesExtensionFile = $this->appRoot . '/' .
122 $module->getPath() . '/console.services.yml';
123 if (is_file($consoleServicesExtensionFile)) {
124 $loader->load($consoleServicesExtensionFile);
125 $servicesData = $this->extractServiceData(
126 $consoleServicesExtensionFile,
133 * @var Extension[] $themes
135 $themes = $extensionManager->discoverThemes()
140 foreach ($themes as $theme) {
141 $consoleServicesExtensionFile = $this->appRoot . '/' .
142 $theme->getPath() . '/console.services.yml';
143 if (is_file($consoleServicesExtensionFile)) {
144 $loader->load($consoleServicesExtensionFile);
145 $servicesData = $this->extractServiceData(
146 $consoleServicesExtensionFile,
152 if ($servicesData && is_writable($cacheDirectory)) {
154 $consoleServicesFile,
155 Yaml::dump($servicesData, 4, 2)
160 $consoleExtendServicesFile = $this->root. DRUPAL_CONSOLE .'/extend.console.services.yml';
161 if (file_exists($consoleExtendServicesFile)) {
162 $loader->load($consoleExtendServicesFile);
165 $configurationManager = $container->get('console.configuration_manager');
166 $directory = $configurationManager->getConsoleDirectory() . 'extend/';
167 $autoloadFile = $directory . 'vendor/autoload.php';
168 if (is_file($autoloadFile)) {
169 include_once $autoloadFile;
170 $extendServicesFile = $directory . 'extend.console.services.yml';
171 if (is_file($extendServicesFile)) {
172 $loader->load($extendServicesFile);
176 $container->setParameter(
177 'console.service_definitions',
178 $container->getDefinitions()
181 $definition = $container->getDefinition('console.translator_manager');
182 $definition->setClass(TranslatorManager::class);
187 * @param $servicesData
191 protected function extractServiceData($filePath, $servicesData)
193 $serviceFileData = Yaml::parse(
194 file_get_contents($filePath)
197 $servicesData = array_merge_recursive(
202 return $servicesData;