-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[TwigBundle] Commands as a service #23519
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
Changes from all commits
d796f95
42b71b2
552f368
a2a58f5
58194cd
08feece
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -29,15 +29,27 @@ class DebugCommand extends Command | |||
private $twig; | ||||
|
||||
/** | ||||
* {@inheritdoc} | ||||
* @param Environment $twig | ||||
*/ | ||||
public function __construct($name = 'debug:twig') | ||||
public function __construct($twig = null) | ||||
{ | ||||
parent::__construct($name); | ||||
parent::__construct(); | ||||
|
||||
if (!$twig instanceof Environment) { | ||||
@trigger_error(sprintf('Passing a command name as the first argument of "%s" is deprecated since version 3.4 and will be removed in 4.0. If the command was registered by convention, make it a service instead.', __METHOD__), E_USER_DEPRECATED); | ||||
|
||||
$this->setName(null === $twig ? 'debug:twig' : $twig); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Minor, but it looks safe to me to simply use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, but who would name this command There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah.. to me that should be
or null otherwise )
If you insist i change it, just saying im preserving previous behavior :) |
||||
|
||||
return; | ||||
} | ||||
|
||||
$this->twig = $twig; | ||||
} | ||||
|
||||
public function setTwigEnvironment(Environment $twig) | ||||
{ | ||||
@trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED); | ||||
|
||||
$this->twig = $twig; | ||||
} | ||||
|
||||
|
@@ -46,12 +58,15 @@ public function setTwigEnvironment(Environment $twig) | |||
*/ | ||||
protected function getTwigEnvironment() | ||||
{ | ||||
@trigger_error(sprintf('Method "%s" is deprecated since version 3.4 and will be removed in 4.0.', __METHOD__), E_USER_DEPRECATED); | ||||
|
||||
return $this->twig; | ||||
} | ||||
|
||||
protected function configure() | ||||
{ | ||||
$this | ||||
->setName('debug:twig') | ||||
->setDefinition(array( | ||||
new InputArgument('filter', InputArgument::OPTIONAL, 'Show details for all entries matching this filter'), | ||||
new InputOption('format', null, InputOption::VALUE_REQUIRED, 'The output format (text or json)', 'text'), | ||||
|
@@ -80,9 +95,17 @@ protected function configure() | |||
protected function execute(InputInterface $input, OutputInterface $output) | ||||
{ | ||||
$io = new SymfonyStyle($input, $output); | ||||
$twig = $this->getTwigEnvironment(); | ||||
|
||||
if (null === $twig) { | ||||
// BC to be removed in 4.0 | ||||
if (__CLASS__ !== get_class($this)) { | ||||
$r = new \ReflectionMethod($this, 'getTwigEnvironment'); | ||||
if (__CLASS__ !== $r->getDeclaringClass()->getName()) { | ||||
@trigger_error(sprintf('Usage of method "%s" is deprecated since version 3.4 and will no longer be supported in 4.0.', get_class($this).'::getTwigEnvironment'), E_USER_DEPRECATED); | ||||
|
||||
$this->twig = $this->getTwigEnvironment(); | ||||
} | ||||
} | ||||
if (null === $this->twig) { | ||||
throw new \RuntimeException('The Twig environment needs to be set.'); | ||||
} | ||||
|
||||
|
@@ -91,7 +114,7 @@ protected function execute(InputInterface $input, OutputInterface $output) | |||
if ($input->getOption('format') === 'json') { | ||||
$data = array(); | ||||
foreach ($types as $type) { | ||||
foreach ($twig->{'get'.ucfirst($type)}() as $name => $entity) { | ||||
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) { | ||||
$data[$type][$name] = $this->getMetadata($type, $entity); | ||||
} | ||||
} | ||||
|
@@ -105,7 +128,7 @@ protected function execute(InputInterface $input, OutputInterface $output) | |||
|
||||
foreach ($types as $index => $type) { | ||||
$items = array(); | ||||
foreach ($twig->{'get'.ucfirst($type)}() as $name => $entity) { | ||||
foreach ($this->twig->{'get'.ucfirst($type)}() as $name => $entity) { | ||||
if (!$filter || false !== strpos($name, $filter)) { | ||||
$items[$name] = $name.$this->getPrettyMetadata($type, $entity); | ||||
} | ||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?xml version="1.0" ?> | ||
|
||
<container xmlns="http://symfony.com/schema/dic/services" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"> | ||
|
||
<services> | ||
<defaults public="false" /> | ||
|
||
<service id="twig.command.debug" class="Symfony\Bridge\Twig\Command\DebugCommand"> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are we not using class name == service id convention for new code? |
||
<argument type="service" id="twig" /> | ||
<tag name="console.command" command="debug:twig" /> | ||
</service> | ||
|
||
<service id="twig.command.lint" class="Symfony\Bundle\TwigBundle\Command\LintCommand"> | ||
<argument type="service" id="twig" /> | ||
<tag name="console.command" command="lint:twig" /> | ||
</service> | ||
|
||
<!-- BC to be removed in 4.0 --> | ||
<service id="console.command.symfony_bundle_twigbundle_command_debugcommand" class="Symfony\Bundle\TwigBundle\Command\DebugCommand" public="true"> | ||
<deprecated>The "%service_id%" service is deprecated since Symfony 3.4 and will be removed in 4.0. Use "twig.command.debug" instead.</deprecated> | ||
</service> | ||
</services> | ||
</container> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
$name
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Patched.