8000 [FrameworkBundle] Commands as a service · symfony/symfony@de1dc0b · GitHub
[go: up one dir, main page]

Skip to content

Commit de1dc0b

Browse files
ro0NLnicolas-grekas
authored andcommitted
[FrameworkBundle] Commands as a service
1 parent 8e517f6 commit de1dc0b

38 files changed

+714
-171
lines changed

src/Symfony/Bridge/Twig/Command/DebugCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
100100
if (__CLASS__ !== get_class($this)) {
101101
$r = new \ReflectionMethod($this, 'getTwigEnvironment');
102102
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
103-
@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);
103+
@trigger_error(sprintf('Usage of method "%s" is deprecated since version 3.4 and will no longer be supported in 4.0. Construct the command with its required arguments instead.', get_class($this).'::getTwigEnvironment'), E_USER_DEPRECATED);
104104

105105
$this->twig = $this->getTwigEnvironment();
106106
}

src/Symfony/Bridge/Twig/Command/LintCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
105105
if (__CLASS__ !== get_class($this)) {
106106
$r = new \ReflectionMethod($this, 'getTwigEnvironment');
107107
if (__CLASS__ !== $r->getDeclaringClass()->getName()) {
108-
@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);
108+
@trigger_error(sprintf('Usage of method "%s" is deprecated since version 3.4 and will no longer be supported in 4.0. Construct the command with its required arguments instead.', get_class($this).'::getTwigEnvironment'), E_USER_DEPRECATED);
109109

110110
$this->twig = $this->getTwigEnvironment();
111111
}

src/Symfony/Bundle/FrameworkBundle/Command/AboutCommand.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* A console command to display information about the current installation.
2424
*
2525
* @author Roland Franssen <franssen.roland@gmail.com>
26+
*
27+
* @final since version 3.4
2628
*/
2729
class AboutCommand extends ContainerAwareCommand
2830
{
@@ -45,7 +47,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4547
$io = new SymfonyStyle($input, $output);
4648

4749
/** @var $kernel KernelInterface */
48-
$kernel = $this->getContainer()->get('kernel');
50+
$kernel = $this->getApplication()->getKernel();
4951

5052
$io->table(array(), array(
5153
array('<info>Symfony</>'),

src/Symfony/Bundle/FrameworkBundle/Command/AbstractConfigCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ protected function listBundles($output)
3131
$headers = array('Bundle name', 'Extension alias');
3232
$rows = array();
3333

34-
$bundles = $this->getContainer()->get('kernel')->getBundles();
34+
$bundles = $this->getApplication()->getKernel()->getBundles();
3535
usort($bundles, function ($bundleA, $bundleB) {
3636
return strcmp($bundleA->getName(), $bundleB->getName());
3737
});
@@ -117,7 +117,7 @@ private function initializeBundles()
117117
// Re-build bundle manually to initialize DI extensions that can be extended by other bundles in their build() method
118118
// as this method is not called when the container is loaded from the cache.
119119
$container = $this->getContainerBuilder();
120-
$bundles = $this->getContainer()->get('kernel')->getBundles();
120+
$bundles = $this->getApplication()->getKernel()->getBundles();
121121
foreach ($bundles as $bundle) {
122122
if ($extension = $bundle->getContainerExtension()) {
123123
$container->registerExtension($extension);

src/Symfony/Bundle/FrameworkBundle/Command/AssetsInstallCommand.php

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,17 +26,34 @@
2626
*
2727
* @author Fabien Potencier <fabien@symfony.com>
2828
* @author Gábor Egyed <gabor.egyed@gmail.com>
29+
*
30+
* @final since version 3.4
2931
*/
3032
class AssetsInstallCommand extends ContainerAwareCommand
3133
{
3234
const METHOD_COPY = 'copy';
3335
const METHOD_ABSOLUTE_SYMLINK = 'absolute symlink';
3436
const METHOD_RELATIVE_SYMLINK = 'relative symlink';
3537

38+
private $filesystem;
39+
3640
/**
37-
* @var Filesystem
41+
* @param Filesystem $filesystem
3842
*/
39-
private $filesystem;
43+
public function __construct($filesystem = null)
44+
{
45+
parent::__construct();
46+
47+
if (!$filesystem instanceof Filesystem) {
48+
@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);
49+
50+
$this->setName(null === $filesystem ? 'assets:install' : $filesystem);
51+
52+
return;
53+
}
54+
55+
$this->filesystem = $filesystem;
56+
}
4057

4158
/**
4259
* {@inheritdoc}
@@ -79,10 +96,17 @@ protected function configure()
7996
*/
8097
protected function execute(InputInterface $input, OutputInterface $output)
8198
{
99+
// BC to be removed in 4.0
100+
if (null === $this->filesystem) {
101+
$this->filesystem = $this->getContainer()->get('filesystem');
102+
$baseDir = $this->getContainer()->getParameter('kernel.project_dir');
103+
}
104+
105+
$kernel = $this->getApplication()->getKernel();
82106
$targetArg = rtrim($input->getArgument('target'), '/');
83107

84108
if (!is_dir($targetArg)) {
85-
$targetArg = $this->getContainer()->getParameter('kernel.project_dir').'/'.$targetArg;
109+
$targetArg = (isset($baseDir) ? $baseDir : $kernel->getContainer()->getParameter('kernel.project_dir')).'/'.$targetArg;
86110

87111
if (!is_dir($targetArg)) {
88112
// deprecated, logic to be removed in 4.0
@@ -95,8 +119,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
95119
}
96120
}
97121

98-
$this->filesystem = $this->getContainer()->get('filesystem');
99-
100122
// Create the bundles directory otherwise symlink will fail.
101123
$bundlesDir = $targetArg.'/bundles/';
102124
$this->filesystem->mkdir($bundlesDir, 0777);
@@ -122,7 +144,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
122144
$exitCode = 0;
123145
$validAssetDirs = array();
124146
/** @var BundleInterface $bundle */
125-
foreach ($this->getContainer()->get('kernel')->getBundles() as $bundle) {
147+
foreach ($kernel->getBundles() as $bundle) {
126148
if (!is_dir($originDir = $bundle->getPath().'/Resources/public')) {
127149
continue;
128150
}

src/Symfony/Bundle/FrameworkBundle/Command/CacheClearCommand.php

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
use Symfony\Component\Console\Input\InputOption;
1616
use Symfony\Component\Console\Output\OutputInterface;
1717
use Symfony\Component\Console\Style\SymfonyStyle;
18+
use Symfony\Component\Filesystem\Filesystem;
19+
use Symfony\Component\HttpKernel\CacheClearer\CacheClearerInterface;
1820
use Symfony\Component\HttpKernel\KernelInterface;
1921
use Symfony\Component\Finder\Finder;
2022

@@ -23,9 +25,34 @@
2325
*
2426
* @author Francis Besset <francis.besset@gmail.com>
2527
* @author Fabien Potencier <fabien@symfony.com>
28+
*
29+
* @final since version 3.4
2630
*/
2731
class CacheClearCommand extends ContainerAwareCommand
2832
{
33+
private $cacheClearer;
34+
private $filesystem;
35+
36+
/**
37+
* @param CacheClearerInterface $cacheClearer
38+
* @param Filesystem|null $filesystem
39+
*/
40+
public function __construct($cacheClearer = null, Filesystem $filesystem = null)
41+
{
42+
parent::__construct();
43+
44+
if (!$cacheClearer instanceof CacheClearerInterface) {
45+
@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);
46+
47+
$this->setName(null === $cacheClearer ? 'cache:clear' : $cacheClearer);
48+
49+
return;
50+
}
51+
52+
$this->cacheClearer = $cacheClearer;
53+
$this->filesystem = $filesystem ?: new Filesystem();
54+
}
55+
2956
/**
3057
* {@inheritdoc}
3158
*/
@@ -54,28 +81,34 @@ protected function configure()
5481
*/
5582
protected function execute(InputInterface $input, OutputInterface $output)
5683
{
84+
// BC to be removed in 4.0
85+
if (null === $this->cacheClearer) {
86+
$this->cacheClearer = $this->getContainer()->get('cache_clearer');
87+
$this->filesystem = $this->getContainer()->get('filesystem');
88+
$realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir');
89+
}
90+
5791
$io = new SymfonyStyle($input, $output);
5892

59-
$realCacheDir = $this->getContainer()->getParameter('kernel.cache_dir');
93+
$kernel = $this->getApplication()->getKernel();
94+
$realCacheDir = isset($realCacheDir) ? $realCacheDir : $kernel->getContainer()->getParameter('kernel.cache_dir');
6095
// the old cache dir name must not be longer than the real one to avoid exceeding
6196
// the maximum length of a directory or file path within it (esp. Windows MAX_PATH)
6297
$oldCacheDir = substr($realCacheDir, 0, -1).('~' === substr($realCacheDir, -1) ? '+' : '~');
63-
$filesystem = $this->getContainer()->get('filesystem');
6498

6599
if (!is_writable($realCacheDir)) {
66100
throw new \RuntimeException(sprintf('Unable to write in the "%s" directory', $realCacheDir));
67101
}
68102

69-
if ($filesystem->exists($oldCacheDir)) {
70-
$filesystem->remove($oldCacheDir);
103+
if ($this->filesystem->exists($oldCacheDir)) {
104+
$this->filesystem->remove($oldCacheDir);
71105
}
72106

73-
$kernel = $this->getContainer()->get('kernel');
74107
$io->comment(sprintf('Clearing the cache for the <info>%s</info> environment with debug <info>%s</info>', $kernel->getEnvironment(), var_export($kernel->isDebug(), true)));
75-
$this->getContainer()->get('cache_clearer')->clear($realCacheDir);
108+
$this->cacheClearer->clear($realCacheDir);
76109

77110
if ($input->getOption('no-warmup')) {
78-
$filesystem->rename($realCacheDir, $oldCacheDir);
111+
$this->filesystem->rename($realCacheDir, $oldCacheDir);
79112
} else {
80113
$warning = 'Calling cache:clear without the --no-warmup option is deprecated since version 3.3. Cache warmup should be done with the cache:warmup command instead.';
81114

@@ -90,7 +123,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
90123
$io->comment('Removing old cache directory...');
91124
}
92125

93-
$filesystem->remove($oldCacheDir);
126+
$this->filesystem->remove($oldCacheDir);
94127

95128
if ($output->isVerbose()) {
96129
$io->comment('Finished');
@@ -101,31 +134,30 @@ protected function execute(InputInterface $input, OutputInterface $output)
101134

102135
private function war 10000 mupCache(InputInterface $input, OutputInterface $output, $realCacheDir, $oldCacheDir)
103136
{
104-
$filesystem = $this->getContainer()->get('filesystem');
105137
$io = new SymfonyStyle($input, $output);
106138

107139
// the warmup cache dir name must have the same length than the real one
108140
// to avoid the many problems in serialized resources files
109141
$realCacheDir = realpath($realCacheDir);
110142
$warmupDir = substr($realCacheDir, 0, -1).('_' === substr($realCacheDir, -1) ? '-' : '_');
111143

112-
if ($filesystem->exists($warmupDir)) {
144+
if ($this->filesystem->exists($warmupDir)) {
113145
if ($output->isVerbose()) {
114146
$io->comment('Clearing outdated warmup directory...');
115147
}
116-
$filesystem->remove($warmupDir);
148+
$this->filesystem->remove($warmupDir);
117149
}
118150

119151
if ($output->isVerbose()) {
120152
$io->comment('Warming up cache...');
121153
}
122154
$this->warmup($warmupDir, $realCacheDir, !$input->getOption('no-optional-warmers'));
123155

124-
$filesystem->rename($realCacheDir, $oldCacheDir);
156+
$this->filesystem->rename($realCacheDir, $oldCacheDir);
125157
if ('\\' === DIRECTORY_SEPARATOR) {
126158
sleep(1); // workaround for Windows PHP rename bug
127159
}
128-
$filesystem->rename($warmupDir, $realCacheDir);
160+
$this->filesystem->rename($warmupDir, $realCacheDir);
129161
}
130162

131163
/**
@@ -138,7 +170,7 @@ private function warmupCache(InputInterface $input, OutputInterface $output, $re
138170
protected function warmup($warmupDir, $realCacheDir, $enableOptionalWarmers = true)
139171
{
140172
// create a temporary kernel
141-
$realKernel = $this->getContainer()->get('kernel');
173+
$realKernel = $this->getApplication()->getKernel();
142174
$realKernelClass = get_class($realKernel);
143175
$namespace = '';
144176
if (false !== $pos = strrpos($realKernelClass, '\\')) {
@@ -274,7 +306,7 @@ protected function buildContainer()
274306
}
275307
}
276308
EOF;
277-
$this->getContainer()->get('filesystem')->mkdir($warmupDir);
309+
$this->filesystem->mkdir($warmupDir);
278310
file_put_contents($file = $warmupDir.'/kernel.tmp', $code);
279311
require_once $file;
280312
$class = "$namespace\\$class";

src/Symfony/Bundle/FrameworkBundle/Command/CachePoolClearCommand.php

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,26 @@
2525
*/
2626
final class CachePoolClearCommand extends ContainerAwareCommand
2727
{
28+
private $poolClearer;
29+
30+
/**
31+
* @param Psr6CacheClearer $poolClearer
32+
*/
33+
public function __construct($poolClearer = null)
34+
{
35+
parent::__construct();
36+
37+
if (!$poolClearer instanceof Psr6CacheClearer) {
38+
@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);
39+
40+
$this->setName(null === $poolClearer ? 'cache:pool:clear' : $poolClearer);
41+
42+
return;
43+
}
44+
45+
$this->poolClearer = $poolClearer;
46+
}
47+
2848
/**
2949
* {@inheritdoc}
3050
*/
@@ -50,18 +70,22 @@ protected function configure()
5070
*/
5171
protected function execute(InputInterface $input, OutputInterface $output)
5272
{
73+
// BC to be removed in 4.0
74+
if (null === $this->poolClearer) {
75+
$this->poolClearer = $this->getContainer()->get('cache.global_clearer');
76+
$cacheDir = $this->getContainer()->getParameter('kernel.cache_dir');
77+
}
78+
5379
$io = new SymfonyStyle($input, $output);
80+
$kernel = $this->getApplication()->getKernel();
5481
$pools = array();
5582
$clearers = array();
56-
$container = $this->getContainer();
57-
$cacheDir = $container->getParameter('kernel.cache_dir');
58-
$globalClearer = $container->get('cache.global_clearer');
5983

6084
foreach ($input->getArgument('pools') as $id) {
61-
if ($globalClearer->hasPool($id)) {
85+
if ($this->poolClearer->hasPool($id)) {
6286
$pools[$id] = $id;
6387
} else {
64-
$pool = $container->get($id);
88+
$pool = $kernel->getContainer()->get($id);
6589

6690
if ($pool instanceof CacheItemPoolInterface) {
6791
$pools[$id] = $pool;
@@ -75,7 +99,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7599

76100
foreach ($clearers as $id => $clearer) {
77101
$io->comment(sprintf('Calling cache clearer: <info>%s</info>', $id));
78-
$clearer->clear($cacheDir);
102+
$clearer->clear(isset($cacheDir) ? $cacheDir : $kernel->getContainer()->getParameter('kernel.cache_dir'));
79103
}
80104

81105
foreach ($pools as $id => $pool) {
@@ -84,7 +108,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
84108
if ($pool instanceof CacheItemPoolInterface) {
85109
$pool->clear();
86110
} else {
87-
$globalClearer->clearPool($id);
111+
$this->poolClearer->clearPool($id);
88112
}
89113
}
90114

0 commit comments

Comments
 (0)
0