forked from symfony/flex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymfonyBundle.php
112 lines (96 loc) · 3.53 KB
/
SymfonyBundle.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Flex;
use Composer\Composer;
use Composer\Package\PackageInterface;
/**
* @author Fabien Potencier <[email protected]>
*/
class SymfonyBundle
{
private $package;
private $operation;
private $vendorDir;
public function __construct(Composer $composer, PackageInterface $package, string $operation)
{
$this->package = $package;
$this->operation = $operation;
$this->vendorDir = rtrim($composer->getConfig()->get('vendor-dir'), '/');
}
public function getClassNames(): array
{
$uninstall = 'uninstall' === $this->operation;
$classes = [];
$autoload = $this->package->getAutoload();
$isSyliusPlugin = 'sylius-plugin' === $this->package->getType();
foreach (['psr-4' => true, 'psr-0' => false] as $psr => $isPsr4) {
if (!isset($autoload[$psr])) {
continue;
}
foreach ($autoload[$psr] as $namespace => $paths) {
if (!\is_array($paths)) {
$paths = [$paths];
}
foreach ($paths as $path) {
foreach ($this->extractClassNames($namespace, $isSyliusPlugin) as $class) {
// we only check class existence on install as we do have the code available
// in contrast to uninstall operation
if (!$uninstall && !$this->isBundleClass($class, $path, $isPsr4)) {
continue;
}
$classes[] = $class;
}
}
}
}
return $classes;
}
private function extractClassNames(string $namespace, bool $isSyliusPlugin): array
{
$namespace = trim($namespace, '\\');
$class = $namespace.'\\';
$parts = explode('\\', $namespace);
$suffix = $parts[\count($parts) - 1];
$endOfWord = substr($suffix, -6);
if ($isSyliusPlugin) {
if ('Bundle' !== $endOfWord && 'Plugin' !== $endOfWord) {
$suffix .= 'Bundle';
}
} elseif ('Bundle' !== $endOfWord) {
$suffix .= 'Bundle';
}
$classes = [$class.$suffix];
$acc = '';
foreach (\array_slice($parts, 0, -1) as $part) {
if ('Bundle' === $part || ($isSyliusPlugin && 'Plugin' === $part)) {
continue;
}
$classes[] = $class.$part.$suffix;
$acc .= $part;
$classes[] = $class.$acc.$suffix;
}
return array_unique($classes);
}
private function isBundleClass(string $class, string $path, bool $isPsr4): bool
{
$classPath = ($this->vendorDir ? $this->vendorDir.'/' : '').$this->package->getPrettyName().'/'.$path.'/';
$parts = explode('\\', $class);
$class = $parts[\count($parts) - 1];
if (!$isPsr4) {
$classPath .= str_replace('\\', '', implode('/', \array_slice($parts, 0, -1))).'/';
}
$classPath .= str_replace('\\', '/', $class).'.php';
if (!file_exists($classPath)) {
return false;
}
// heuristic that should work in almost all cases
return false !== strpos(file_get_contents($classPath), 'Symfony\Component\HttpKernel\Bundle\Bundle');
}
}