-
-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1634 from bolt/feature/extension-services-and-routes
Copy extension services and routes into Bolt
- Loading branch information
Showing
6 changed files
with
160 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# This file is auto-generated by Bolt. Do not modify. | ||
|
||
services: | ||
_defaults: | ||
autowire: true | ||
autoconfigure: true | ||
AcmeCorp\ReferenceExtension\: | ||
resource: '../../vendor/acmecorp/reference-extension/src/*' | ||
exclude: '../../vendor/acmecorp/reference-extension/src/{Entity,Exception}' | ||
BobdenOtter\ConfigurationNotices\: | ||
resource: '../../vendor/bobdenotter/configuration-notices/src/*' | ||
exclude: '../../vendor/bobdenotter/configuration-notices/src/{Entity,Exception}' | ||
BobdenOtter\WeatherWidget\: | ||
resource: '../../vendor/bobdenotter/weatherwidget/src/*' | ||
exclude: '../../vendor/bobdenotter/weatherwidget/src/{Entity,Exception}' | ||
Bolt\NewsWidget\: | ||
resource: '../../vendor/bolt/newswidget/src/*' | ||
exclude: '../../vendor/bolt/newswidget/src/{Entity,Exception}' |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,140 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Bolt\Command; | ||
|
||
use Bolt\Common\Str; | ||
use Bolt\Extension\ExtensionRegistry; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\DependencyInjection\ContainerInterface; | ||
use Webmozart\PathUtil\Path; | ||
|
||
class ExtensionsConfigureCommand extends Command | ||
{ | ||
protected static $defaultName = 'extensions:configure'; | ||
|
||
/** @var ExtensionRegistry */ | ||
private $extensionRegistry; | ||
|
||
/** @var mixed */ | ||
private $projectDir; | ||
|
||
public function __construct(ExtensionRegistry $extensionRegistry, ContainerInterface $container) | ||
{ | ||
$this->extensionRegistry = $extensionRegistry; | ||
$this->projectDir = $container->getParameter('kernel.project_dir'); | ||
|
||
parent::__construct(); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this | ||
->setDescription('Copy the config/config.yaml, config/services.yaml and config/routes.yaml files from extensions.') | ||
->addOption('with-config', null, InputOption::VALUE_NONE, 'If set, Bolt will copy the default extension config.yaml file'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$extensions = $this->extensionRegistry->getExtensions(); | ||
|
||
$this->copyExtensionRoutesAndServices($extensions); | ||
|
||
if ($input->getOption('with-config')) { | ||
$this->copyExtensionConfig($extensions); | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
public function copyExtensionConfig(array $packages): void | ||
{ | ||
// @todo: Combine this with Bolt\Extension\ConfigTrait.php | ||
foreach ($packages as $package) { | ||
$path = $this->getPackagePath($package); | ||
|
||
$configPath = $this->getRelativePath($path) . '/config/config.yaml'; | ||
if (file_exists($configPath)) { | ||
[$namespace, $name] = explode('\\', mb_strtolower($this->getNamespace($package))); | ||
$destination = $this->getExtensionConfigPath($namespace, $name); | ||
file_put_contents($destination, file_get_contents($configPath)); | ||
} | ||
} | ||
} | ||
|
||
public function copyExtensionRoutesAndServices(array $packages): void | ||
{ | ||
$oldExtensionsRoutes = glob($this->getExtensionRoutesPath()); | ||
$oldExtensionsServices = glob($this->getExtensionServicesPath()); | ||
|
||
foreach ($packages as $package) { | ||
$path = $this->getPackagePath($package); | ||
|
||
$extensionRoutesPath = $this->getRelativePath($path) . '/config/routes.yaml'; | ||
if (file_exists($extensionRoutesPath)) { | ||
$destination = $this->getExtensionRoutesPath($path); | ||
$oldExtensionsRoutes = array_diff($oldExtensionsRoutes, [$destination]); | ||
file_put_contents($destination, file_get_contents($extensionRoutesPath)); | ||
} | ||
|
||
$extensionServicesPath = $this->getRelativePath($path) . '/../config.services.yaml'; | ||
if (file_exists($extensionServicesPath)) { | ||
$destination = $this->getExtensionServicesPath($path); | ||
$oldExtensionsServices = array_diff($oldExtensionsServices, [$destination]); | ||
file_put_contents($destination, file_get_contents($extensionRoutesPath)); | ||
} | ||
} | ||
|
||
// Remove routes.yaml files for old (uninstalled) extensions | ||
array_map('unlink', $oldExtensionsRoutes); | ||
|
||
// Remove services.yaml files for old (uninstalled) extensions | ||
array_map('unlink', $oldExtensionsServices); | ||
} | ||
|
||
private function getRelativePath(string $path): string | ||
{ | ||
return Path::makeRelative($path, $this->projectDir); | ||
} | ||
|
||
/** | ||
* Helper function that returns the path of the extension routes.yaml file | ||
* inside Bolt. | ||
*/ | ||
private function getExtensionRoutesPath(string $path = '*'): string | ||
{ | ||
return $this->projectDir . '/config/routes/extension_' . Str::splitLast($path, '/') . '.yaml'; | ||
} | ||
|
||
/** | ||
* Helper function that returns the path of the extension services.yaml file | ||
* inside Bolt. | ||
*/ | ||
private function getExtensionServicesPath(string $path = '*'): string | ||
{ | ||
return $this->projectDir . '/config/packages/services_extension_' . Str::splitLast($path, '/') . '.yaml'; | ||
} | ||
|
||
private function getExtensionConfigPath(string $namespace, string $name): string | ||
{ | ||
return $this->projectDir . '/config/extensions/' . $namespace . '-' . $name . '.yaml'; | ||
} | ||
|
||
private function getPackagePath($package): string | ||
{ | ||
$reflection = new \ReflectionClass($package); | ||
|
||
return dirname(dirname($reflection->getFilename())); | ||
} | ||
|
||
private function getNamespace($package): string | ||
{ | ||
$reflection = new \ReflectionClass($package); | ||
|
||
return $reflection->getNamespaceName(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters