Skip to content

Commit

Permalink
Merge pull request #1656 from bolt/feature/extensions-install-callback
Browse files Browse the repository at this point in the history
Allow extensions to have an `install` method, which is called on installation (to install assets, for example)
  • Loading branch information
I-Valchev authored Jul 29, 2020
2 parents 1b1da0c + 86cbde2 commit 594d0ff
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions src/Command/ExtensionsConfigureCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$this->copyExtensionConfig($extensions);
}

$this->runExtensionInstall($extensions);

return 0;
}

public function copyExtensionConfig(array $packages): void
private function copyExtensionConfig(array $packages): void
{
// @todo: Combine this with Bolt\Extension\ConfigTrait.php
foreach ($packages as $package) {
$path = $this->getPackagePath($package);
$origin = $this->getRelativePath($path) . '/config/config.yaml';

[$namespace, $name] = explode('\\', mb_strtolower($this->getNamespace($package)));
[$namespace, $name] = explode('\\', mb_strtolower($this->getNamespace($package) . '\\'));
$destination = $this->getExtensionConfigPath($namespace, $name);

if (file_exists($origin) && ! file_exists($destination)) {
Expand All @@ -67,7 +69,7 @@ public function copyExtensionConfig(array $packages): void
}
}

public function copyExtensionRoutesAndServices(array $packages): void
private function copyExtensionRoutesAndServices(array $packages): void
{
$oldExtensionsRoutes = glob($this->getExtensionRoutesPath());
$oldExtensionsServices = glob($this->getExtensionServicesPath());
Expand Down Expand Up @@ -97,6 +99,15 @@ public function copyExtensionRoutesAndServices(array $packages): void
array_map('unlink', $oldExtensionsServices);
}

private function runExtensionInstall(array $packages): void
{
foreach ($packages as $package) {
if (method_exists($package, 'install')) {
$package->install();
}
}
}

private function getRelativePath(string $path): string
{
return Path::makeRelative($path, $this->projectDir);
Expand All @@ -122,7 +133,11 @@ private function getExtensionServicesPath(string $path = '*'): string

private function getExtensionConfigPath(string $namespace, string $name): string
{
return $this->projectDir . '/config/extensions/' . $namespace . '-' . $name . '.yaml';
return sprintf('%s/config/extensions/%s%s%s.yaml',
$this->projectDir,
$namespace,
(! empty($name) ? '-' : ''),
$name);
}

private function getPackagePath($package): string
Expand Down

0 comments on commit 594d0ff

Please sign in to comment.