Skip to content

Commit

Permalink
feature #426 Add support for Sylius plugins (dunglas)
Browse files Browse the repository at this point in the history
This PR was squashed before being merged into the 1.2-dev branch (closes #426).

Discussion
----------

Add support for Sylius plugins

They use a special convention: `Bundle` is replaced by `Plugin`.

Commits
-------

1922db3 Add support for Sylius plugins
  • Loading branch information
fabpot committed May 6, 2019
2 parents d65041a + 1922db3 commit 5bc24c9
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
18 changes: 13 additions & 5 deletions src/SymfonyBundle.php
Expand Up @@ -35,6 +35,7 @@ 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;
Expand All @@ -45,7 +46,7 @@ public function getClassNames(): array
$paths = [$paths];
}
foreach ($paths as $path) {
foreach ($this->extractClassNames($namespace) as $class) {
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->checkClassExists($class, $path, $isPsr4)) {
Expand All @@ -61,27 +62,34 @@ public function getClassNames(): array
return $classes;
}

private function extractClassNames(string $namespace): array
private function extractClassNames(string $namespace, bool $isSyliusPlugin): array
{
$namespace = trim($namespace, '\\');
$class = $namespace.'\\';
$parts = explode('\\', $namespace);
$suffix = $parts[\count($parts) - 1];
if ('Bundle' !== substr($suffix, -6)) {
$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) {
if ('Bundle' === $part || ($isSyliusPlugin && 'Plugin' === $part)) {
continue;
}
$classes[] = $class.$part.$suffix;
$acc .= $part;
$classes[] = $class.$acc.$suffix;
}

return $classes;
return array_unique($classes);
}

private function checkClassExists(string $class, string $path, bool $isPsr4): bool
Expand Down
Empty file.
Empty file.
18 changes: 17 additions & 1 deletion tests/SymfonyBundleTest.php
Expand Up @@ -20,14 +20,18 @@ class SymfonyBundleTest extends TestCase
/**
* @dataProvider getNamespaces
*/
public function testGetClassNamesForInstall($package, $autoload, $classes)
public function testGetClassNamesForInstall($package, $autoload, $classes, $type = null)
{
$config = $this->getMockBuilder('Composer\Config')->getMock();
$config->expects($this->any())->method('get')->will($this->returnValue(__DIR__.'/Fixtures/vendor'));
$composer = $this->getMockBuilder('Composer\Composer')->getMock();
$composer->expects($this->once())->method('getConfig')->will($this->returnValue($config));
$package = new Package($package, '1.0', '1.0');
$package->setAutoload($autoload);
if ($type) {
$package->setType($type);
}

$bundle = new SymfonyBundle($composer, $package, 'install');
$this->assertSame($classes, $bundle->getClassNames());
}
Expand Down Expand Up @@ -80,6 +84,18 @@ public function getNamespaces()
['psr-4' => ['Jose\\Bundle\\JoseFramework\\' => ['']]],
['Jose\\Bundle\\JoseFramework\\JoseFrameworkBundle'],
],
[
'sylius/shop-api-plugin',
['psr-4' => ['Sylius\\ShopApiPlugin\\' => 'src/']],
['Sylius\\ShopApiPlugin\\ShopApiPlugin'],
'sylius-plugin',
],
[
'dunglas/sylius-acme-plugin',
['psr-4' => ['Dunglas\\SyliusAcmePlugin\\' => 'src/']],
['Dunglas\\SyliusAcmePlugin\\DunglasSyliusAcmePlugin'],
'sylius-plugin',
],
];
}
}

0 comments on commit 5bc24c9

Please sign in to comment.