1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WellCommerce Open-Source E-Commerce Platform |
4
|
|
|
* |
5
|
|
|
* This file is part of the WellCommerce package. |
6
|
|
|
* |
7
|
|
|
* (c) Adam Piotrowski <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, |
10
|
|
|
* please view the LICENSE file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace WellCommerce\Bundle\GeneratorBundle\Command; |
14
|
|
|
|
15
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
17
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
18
|
|
|
use Symfony\Component\Finder\Finder; |
19
|
|
|
use WellCommerce\Bundle\GeneratorBundle\Model\WellCommerceBundle; |
20
|
|
|
use Wingu\OctopusCore\Reflection\ReflectionFile; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Class RegisterCommand |
24
|
|
|
* |
25
|
|
|
* @author Adam Piotrowski <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
final class RegisterCommand extends AbstractBundleCommand |
28
|
|
|
{ |
29
|
|
|
protected function configure() |
30
|
|
|
{ |
31
|
|
|
$this->setDescription('Registers a bundle by its prefix'); |
32
|
|
|
$this->setName('wellcommerce:bundle:register'); |
33
|
|
|
|
34
|
|
|
$this->addOption( |
35
|
|
|
'prefix', |
36
|
|
|
null, |
37
|
|
|
InputOption::VALUE_REQUIRED, |
38
|
|
|
'Bundle prefix' |
39
|
|
|
); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
43
|
|
|
{ |
44
|
|
|
$prefix = $input->getOption('prefix'); |
45
|
|
|
|
46
|
|
|
if ($this->isRegisteredBundle($prefix)) { |
47
|
|
|
$output->writeln('<error>' . $prefix . ' is already registered in AppKernel.</error>'); |
48
|
|
|
|
49
|
|
|
return 0; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$bundleClass = $this->findBundleClass($prefix); |
53
|
|
|
|
54
|
|
|
if (!$bundleClass) { |
55
|
|
|
$output->writeln('<error>Bundle class was not found in src/ directory</error>'); |
56
|
|
|
|
57
|
|
|
return 0; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
$rf = new ReflectionFile($bundleClass); |
61
|
|
|
|
62
|
|
|
/** @var \ReflectionClass $object */ |
63
|
|
|
foreach ($rf->getObjects() as $object) { |
64
|
|
|
$bundle = $this->initBundle($object, $prefix); |
|
|
|
|
65
|
|
|
$this->manipulator->enableBundle($bundle); |
66
|
|
|
$output->writeln('<info>' . $bundle->getPrefix() . ' was successfully enabled</info>'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return 0; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
private function initBundle(\ReflectionClass $reflectionClass, string $prefix): WellCommerceBundle |
73
|
|
|
{ |
74
|
|
|
$shortName = $reflectionClass->getShortName(); |
75
|
|
|
$namespace = $reflectionClass->getNamespaceName(); |
76
|
|
|
$targetDirectory = dirname($reflectionClass->getFileName()); |
77
|
|
|
|
78
|
|
|
return new WellCommerceBundle($prefix, $namespace, $shortName, $targetDirectory, 'yml', true); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
private function isRegisteredBundle(string $bundleName): bool |
82
|
|
|
{ |
83
|
|
|
foreach ($this->kernel->getBundles() as $bundle) { |
84
|
|
|
if ($bundleName === $bundle->getName()) { |
85
|
|
|
return true; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return false; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function findBundleClass(string $bundleName) |
93
|
|
|
{ |
94
|
|
|
$finder = new Finder(); |
95
|
|
|
$finder->in($this->kernel->getRootDir() . '/../src')->name('*' . $bundleName . 'Bundle.php')->depth(3); |
96
|
|
|
|
97
|
|
|
foreach ($finder->files() as $file) { |
98
|
|
|
return $file; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return false; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.