Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[console] Relocate code to core #3580

Merged
merged 3 commits into from
Nov 13, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
195 changes: 49 additions & 146 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,110 +59,46 @@ public function getLongVersion()
*/
public function doRun(InputInterface $input, OutputInterface $output)
{
$this->registerGenerators();
$this->registerCommands();
$clear = $this->container->get('console.configuration_manager')
->getConfiguration()
->get('application.clear')?:false;
if ($clear === true || $clear === 'true') {
$output->write(sprintf("\033\143"));
}
$this->validateCommands();

$exitCode = parent::doRun($input, $output);
return $exitCode;
return parent::doRun($input, $output);
}

private function registerGenerators()
public function validateCommands()
{
if ($this->container->hasParameter('drupal.generators')) {
$consoleGenerators = $this->container->getParameter(
'drupal.generators'
);
} else {
$consoleGenerators = array_keys(
$this->container->findTaggedServiceIds('drupal.generator')
);
}

foreach ($consoleGenerators as $name) {
if (!$this->container->has($name)) {
continue;
}

try {
$generator = $this->container->get($name);
} catch (\Exception $e) {
echo $name . ' - ' . $e->getMessage() . PHP_EOL;

continue;
}

if (!$generator) {
continue;
}
$consoleCommands = $this->container
->findTaggedServiceIds('drupal.command');

if (method_exists($generator, 'setRenderer')) {
$generator->setRenderer(
$this->container->get('console.renderer')
);
}

if (method_exists($generator, 'setFileQueue')) {
$generator->setFileQueue(
$this->container->get('console.file_queue')
);
}

if (method_exists($generator, 'setCountCodeLines')) {
$generator->setCountCodeLines(
$this->container->get('console.count_code_lines')
);
}
if (!$consoleCommands) {
return;
}
}

private function registerCommands()
{
if ($this->container->hasParameter('drupal.commands')) {
$consoleCommands = $this->container->getParameter(
'drupal.commands'
);
} else {
$consoleCommands = array_keys(
$this->container->findTaggedServiceIds('drupal.command')
);
$this->container->setParameter(
'console.warning',
'application.site.errors.settings'
);
if (!$this->container->hasParameter('console.service_definitions')) {
return;
}

$serviceDefinitions = [];
$annotationValidator = null;
$annotationCommandReader = null;
if ($this->container->hasParameter('console.service_definitions')) {
$serviceDefinitions = $this->container
->getParameter('console.service_definitions');

/**
* @var DrupalCommandAnnotationReader $annotationCommandReader
*/
$annotationCommandReader = $this->container
->get('console.annotation_command_reader');

/**
* @var AnnotationValidator $annotationValidator
*/
$annotationValidator = $this->container
->get('console.annotation_validator');
$serviceDefinitions = $this->container
->getParameter('console.service_definitions');

if (!$serviceDefinitions) {
return;
}

$aliases = $this->container->get('console.configuration_manager')
->getConfiguration()
->get('application.commands.aliases')?:[];
/**
* @var DrupalCommandAnnotationReader $annotationCommandReader
*/
$annotationCommandReader = $this->container
->get('console.annotation_command_reader');

foreach ($consoleCommands as $name) {
/**
* @var AnnotationValidator $annotationValidator
*/
$annotationValidator = $this->container
->get('console.annotation_validator');

$invalidCommands = [];

foreach ($consoleCommands as $name => $tags) {
AnnotationRegistry::reset();
AnnotationRegistry::registerLoader(
[
Expand All @@ -172,66 +108,40 @@ private function registerCommands()
);

if (!$this->container->has($name)) {
$invalidCommands[] = $name;
continue;
}

if ($annotationValidator && $annotationCommandReader) {
if (!$serviceDefinition = $serviceDefinitions[$name]) {
continue;
}

if (!$annotationValidator->isValidCommand($serviceDefinition->getClass())) {
continue;
}

$annotation = $annotationCommandReader
->readAnnotation($serviceDefinition->getClass());
if ($annotation) {
$this->container->get('console.translator_manager')
->addResourceTranslationsByExtension(
$annotation['extension'],
$annotation['extensionType']
);
}
}

try {
$command = $this->container->get($name);
} catch (\Exception $e) {
echo $name . ' - ' . $e->getMessage() . PHP_EOL;

if (!$serviceDefinition = $serviceDefinitions[$name]) {
$invalidCommands[] = $name;
continue;
}

if (!$command) {
if (!$annotationValidator->isValidCommand(
$serviceDefinition->getClass()
)
) {
$invalidCommands[] = $name;
continue;
}

if (method_exists($command, 'setTranslator')) {
$command->setTranslator(
$this->container->get('console.translator_manager')
);
}

if (method_exists($command, 'setContainer')) {
$command->setContainer(
$this->container->get('service_container')
);
$annotation = $annotationCommandReader
->readAnnotation($serviceDefinition->getClass());
if ($annotation) {
$this->container->get('console.translator_manager')
->addResourceTranslationsByExtension(
$annotation['extension'],
$annotation['extensionType']
);
}
}

if (array_key_exists($command->getName(), $aliases)) {
$commandAliases = array_unique(array_merge(
$command->getAliases()?$command->getAliases():[],
array_key_exists($command->getName(), $aliases)?$aliases[$command->getName()]:[]
));
if (!is_array($commandAliases)) {
$commandAliases = [$commandAliases];
}
$command->setAliases($commandAliases);
}
$this->container->set(
'console.invalid_commands',
$invalidCommands
);

$this->add($command);
}
return;
}

public function getData()
Expand Down Expand Up @@ -399,11 +309,4 @@ private function commandData($commandName)

return $data;
}

public function setContainer($container)
{
$this->container = $container;
$this->registerGenerators();
$this->registerCommands();
}
}
8 changes: 7 additions & 1 deletion src/Bootstrap/AddServicesCompilerPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class AddServicesCompilerPass implements CompilerPassInterface
/**
* AddCommandsCompilerPass constructor.
*
* @param string $root
* @param string $root
*/
public function __construct($root)
{
Expand Down Expand Up @@ -91,6 +91,12 @@ public function process(ContainerBuilder $container)
'console.service_definitions',
$container->getDefinitions()
);

// Set console.invalid_commands service
$container->set(
'console.invalid_commands',
null
);
}

protected function addDrupalConsoleServiceFiles($servicesFiles)
Expand Down
16 changes: 9 additions & 7 deletions src/Bootstrap/Drupal.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\ConsoleOutput;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\Compiler\PassConfig;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Core\Site\Settings;
use Drupal\Console\Core\Style\DrupalStyle;
use Drupal\Console\Core\Utils\ArgvInputReader;
use Drupal\Console\Core\Bootstrap\DrupalConsoleCore;
use Drupal\Console\Core\Utils\DrupalFinder;
use Drupal\Component\FileCache\FileCacheFactory;
use Drupal\Core\Site\Settings;
use Drupal\Console\Core\Bootstrap\DrupalInterface;

class Drupal
class Drupal implements DrupalInterface
{
protected $autoload;

Expand Down Expand Up @@ -74,10 +76,10 @@ public function boot()
}
}

$rebuildServicesFile = false;
if ($command=='cache:rebuild' || $command=='cr') {
$rebuildServicesFile = true;
}
// $rebuildServicesFile = false;
// if ($command=='cache:rebuild' || $command=='cr') {
// $rebuildServicesFile = true;
// }

if ($debug) {
$io->writeln('➤ Creating request');
Expand Down
12 changes: 1 addition & 11 deletions src/Bootstrap/DrupalKernelTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@ public static function createFromRequest(Request $request, $class_loader, $envir
$kernel = new static($environment, $class_loader, $allow_dumping, $app_root);
static::bootEnvironment($app_root);
$kernel->initializeSettings($request);
// Calling the request handle causes that a page request "/" is
// processed for any console execution even: help or --version and
// with sites that have globally displayed blocks contexts are not
// ready for blocks plugins so this causes lot of problems like:
// https://github.com/hechoendrupal/drupal-console/issues/3091 and
// https://github.com/hechoendrupal/drupal-console/issues/3553 Also
// handle does a initializeContainer which originally was invalidated
// and rebuild at Console Drupal Bootstrap. By disabling handle
// and processing the boot() at Bootstrap commands that do not
// depend on requests works well.
// $kernel->handle($request);

return $kernel;
}

Expand Down
19 changes: 10 additions & 9 deletions src/Bootstrap/DrupalServiceModifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

/**
* Class DrupalServiceModifier
*
* @package Drupal\Console\Bootstrap
*/
class DrupalServiceModifier implements ServiceModifierInterface
Expand All @@ -29,9 +30,9 @@ class DrupalServiceModifier implements ServiceModifierInterface
/**
* DrupalServiceModifier constructor.
*
* @param string $root
* @param string $serviceTag
* @param string $generatorTag
* @param string $root
* @param string $serviceTag
* @param string $generatorTag
*/
public function __construct(
$root = null,
Expand All @@ -52,11 +53,11 @@ public function alter(ContainerBuilder $container)
new AddServicesCompilerPass($this->root)
);

$container->addCompilerPass(
new FindCommandsCompilerPass($this->commandTag)
);
$container->addCompilerPass(
new FindGeneratorsCompilerPass($this->generatorTag)
);
// $container->addCompilerPass(
// new FindCommandsCompilerPass($this->commandTag)
// );
// $container->addCompilerPass(
// new FindGeneratorsCompilerPass($this->generatorTag)
// );
}
}