Skip to content

Commit

Permalink
Update compile services file. (#3157)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmolivas authored Feb 5, 2017
1 parent 94aba20 commit 2ed0506
Show file tree
Hide file tree
Showing 6 changed files with 353 additions and 12 deletions.
2 changes: 2 additions & 0 deletions bin/drupal.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

set_time_limit(0);

$autoloaders = [];

if (file_exists(__DIR__ . '/../autoload.local.php')) {
include_once __DIR__ . '/../autoload.local.php';
} else {
Expand Down
2 changes: 2 additions & 0 deletions services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,5 @@ services:
console.annotation_validator:
class: Drupal\Console\Utils\AnnotationValidator
arguments: ['@console.annotation_command_reader', '@console.extension_manager']
console.extend_extension_manager:
class: Drupal\Console\Utils\ExtendExtensionManager
56 changes: 44 additions & 12 deletions src/Bootstrap/AddServicesCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@

namespace Drupal\Console\Bootstrap;

use Drupal\Console\Extension\Extension;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;
use Drupal\Console\Extension\Manager;
use Drupal\Console\Utils\ExtendExtensionManager;
use Drupal\Console\Utils\TranslatorManager;
use Drupal\Console\Extension\Extension;
use Drupal\Console\Extension\Manager;

/**
* FindCommandsCompilerPass
Expand Down Expand Up @@ -56,13 +57,21 @@ public function process(ContainerBuilder $container)
new FileLocator($this->root)
);

$loader->load($this->root. DRUPAL_CONSOLE_CORE . 'services.yml');
$loader->load($this->root. DRUPAL_CONSOLE . 'services-drupal-install.yml');
$loader->load($this->root. DRUPAL_CONSOLE . 'services.yml');
$loader->load($this->root. DRUPAL_CONSOLE_CORE . 'services.yml');
$loader->load($this->root. DRUPAL_CONSOLE . 'services-drupal-install.yml');
$loader->load($this->root. DRUPAL_CONSOLE . 'services.yml');

$consoleServicesFile = $this->root.DRUPAL_CONSOLE.'services-console.yml';
$basePath = $container->get('console.site')->getCacheDirectory();
$consoleServicesFile = $basePath.'/console.services.yml';
$consoleExtendServicesFile = $basePath.'/extend.console.services.yml';
$consoleExtendConfigFile = $basePath.'/extend.console.config.yml';

if ($this->rebuild || !file_exists($consoleServicesFile)) {
if ($basePath && !$this->rebuild && file_exists($consoleServicesFile)) {
$loader->load($consoleServicesFile);
if (file_exists($consoleExtendServicesFile)) {
$loader->load($consoleExtendServicesFile);
}
} else {
$finder = new Finder();
$finder->files()
->name('*.yml')
Expand Down Expand Up @@ -139,14 +148,37 @@ public function process(ContainerBuilder $container)
}
}

if ($servicesData) {
if ($servicesData && is_writable($basePath)) {
file_put_contents(
$consoleServicesFile,
Yaml::dump($servicesData, 4, 2)
);
}
} else {
$loader->load($consoleServicesFile);

/**
* @var ExtendExtensionManager $extendExtensionManager
*/
$extendExtensionManager = $container->get('console.extend_extension_manager');
$extendExtensionManager->processProjectPackages($this->root);
$configData = $extendExtensionManager->getConfigData();
if ($configData && is_writable($basePath)) {
file_put_contents(
$consoleExtendConfigFile,
Yaml::dump($configData, 6, 2)
);
}
$servicesData = $extendExtensionManager->getServicesData();
if ($servicesData && is_writable($basePath)) {
file_put_contents(
$consoleExtendServicesFile,
Yaml::dump($servicesData, 4, 2)
);
}

$servicesFiles = $extendExtensionManager->getServicesFiles();
foreach ($servicesFiles as $servicesFile) {
$loader->load($servicesFile);
}
}

$configurationManager = $container->get('console.configuration_manager');
Expand Down
22 changes: 22 additions & 0 deletions src/Bootstrap/Drupal.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Core\Utils\ArgvInputReader;
use Drupal\Console\Core\Bootstrap\DrupalConsoleCore;
use Drupal\Console\Utils\ExtendExtensionManager;

class Drupal
{
Expand Down Expand Up @@ -129,6 +130,27 @@ public function boot($debug)
$this->root
);

$basePath = $container->get('console.site')->getCacheDirectory();
$consoleExtendConfigFile = $basePath.'/extend.console.config.yml';

if ($basePath && file_exists($consoleExtendConfigFile)) {
$container->get('console.configuration_manager')
->importConfigurationFile($consoleExtendConfigFile);
} else {
/**
* @var ExtendExtensionManager $extendExtensionManager
*/
$extendExtensionManager = $container->get('console.extend_extension_manager');
if (!$extendExtensionManager->isProcessed()) {
$extendExtensionManager->processProjectPackages($this->root);
}
$configFiles = $extendExtensionManager->getConfigFiles();
foreach ($configFiles as $configFile) {
$container->get('console.configuration_manager')
->importConfigurationFile($configFile);
}
}

$container->get('console.renderer')
->setSkeletonDirs(
[
Expand Down
241 changes: 241 additions & 0 deletions src/Utils/ExtendExtensionManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
<?php

namespace Drupal\Console\Utils;

use Symfony\Component\Yaml\Yaml;
use Symfony\Component\Finder\Finder;

class ExtendExtensionManager
{
/**
* @var array
*/
protected $configData = [];

/**
* @var array
*/
protected $servicesData = [];

/**
* @var array
*/
protected $configFiles;

/**
* @var array
*/
protected $servicesFiles;

/**
* @var boolean
*/
protected $processed;

/**
* ExtendExtensionManager constructor.
*/
public function __construct()
{
$this->init();
}

/**
* @param string $composerFile
*
* @return bool
*/
public function isValidPackageType($composerFile)
{
if (!is_file($composerFile)) {
return false;
}

$composerContent = json_decode(file_get_contents($composerFile), true);
if (!$composerContent) {
return false;
}

if (!array_key_exists('type', $composerContent)) {
return false;
}

return $composerContent['type'] === 'drupal-console-library';
}

/**
* @param string $configFile
*/
public function addConfigFile($configFile)
{
$configData = $this->parseData($configFile);
if ($this->isValidConfigData($configData)) {
$this->configFiles[] = $configFile;
$this->configData = array_merge_recursive(
$configData,
$this->configData
);
}
}

/**
* @param string $servicesFile
*/
public function addServicesFile($servicesFile)
{
$servicesData = $this->parseData($servicesFile);
if ($this->isValidServicesData($servicesData)) {
$this->servicesFiles[] = $servicesFile;
$this->servicesData = array_merge_recursive(
$servicesData,
$this->servicesData
);
}
}

/**
* init
*/
private function init()
{
$this->configData = [];
$this->servicesData = [];
$this->configFiles = [];
$this->servicesFiles = [];
$this->processed = false;
}

/**
* @param $file
* @return array|mixed
*/
private function parseData($file)
{
if (!file_exists($file)) {
return [];
}

$data = Yaml::parse(
file_get_contents($file)
);

if (!$data) {
return [];
}

return $data;
}

public function processProjectPackages($directory)
{
$finder = new Finder();
$finder->files()
->name('composer.json')
->contains('drupal-console-library')
->in($directory);

foreach ($finder as $file) {
$this->processComposerFile($file->getPathName());
}

$this->processed = true;
}

/**
* @param $composerFile
*/
private function processComposerFile($composerFile)
{
$packageDirectory = dirname($composerFile);

$configFile = $packageDirectory.'/console.config.yml';
$this->addConfigFile($configFile);

$servicesFile = $packageDirectory.'/console.services.yml';
$this->addServicesFile($servicesFile);
}

/**
* @param array $configData
*
* @return boolean
*/
private function isValidConfigData($configData)
{
if (!$configData) {
return false;
}

if (!array_key_exists('application', $configData)) {
return false;
}

if (!array_key_exists('autowire', $configData['application'])) {
return false;
}

if (!array_key_exists('commands', $configData['application']['autowire'])) {
return false;
}

return true;
}

/**
* @param array $servicesData
*
* @return boolean
*/
private function isValidServicesData($servicesData)
{
if (!$servicesData) {
return false;
}

if (!array_key_exists('services', $servicesData)) {
return false;
}

return true;
}

/**
* @return array
*/
public function getConfigData()
{
return $this->configData;
}

/**
* @return array
*/
public function getServicesData()
{
return $this->servicesData;
}

/**
* @return array
*/
public function getConfigFiles()
{
return $this->configFiles;
}

/**
* @return array
*/
public function getServicesFiles()
{
return $this->servicesFiles;
}

/**
* @return bool
*/
public function isProcessed()
{
return $this->processed;
}
}
Loading

0 comments on commit 2ed0506

Please sign in to comment.