diff --git a/src/Console/Commands/Install.php b/src/Console/Commands/Install.php index 6f65a865f9..2d6acbaccf 100644 --- a/src/Console/Commands/Install.php +++ b/src/Console/Commands/Install.php @@ -5,6 +5,7 @@ use Illuminate\Console\Command; use Statamic\Console\RunsInPlease; use Statamic\Facades\File; +use Statamic\Statamic; class Install extends Command { @@ -34,6 +35,7 @@ public function handle() $this->addons() ->createFiles() ->publish() + ->runCallbacks() ->clearViews() ->clearCache(); } @@ -98,4 +100,11 @@ protected function clearCache() return $this; } + + protected function runCallbacks() + { + Statamic::runAfterInstalledCallbacks($this); + + return $this; + } } diff --git a/src/Providers/AddonServiceProvider.php b/src/Providers/AddonServiceProvider.php index 875634ef9f..1d36489c9e 100644 --- a/src/Providers/AddonServiceProvider.php +++ b/src/Providers/AddonServiceProvider.php @@ -30,6 +30,7 @@ abstract class AddonServiceProvider extends ServiceProvider protected $routes = []; protected $middlewareGroups = []; protected $viewNamespace; + protected $publishAfterInstall = true; public function boot() { @@ -52,7 +53,8 @@ public function boot() ->bootPublishables() ->bootRoutes() ->bootMiddleware() - ->bootViews(); + ->bootViews() + ->bootPublishAfterInstall(); }); } @@ -167,7 +169,7 @@ protected function bootPublishables() return [$origin => public_path("vendor/{$package}/{$destination}")]; }); - $this->publishes($publishables->all()); + $this->publishes($publishables->all(), $this->getAddon()->slug()); return $this; } @@ -291,7 +293,7 @@ public function registerScript(string $path) $this->publishes([ $path => public_path("vendor/{$name}/js/{$filename}.js"), - ]); + ], $this->getAddon()->slug()); Statamic::script($name, $filename); } @@ -308,7 +310,7 @@ public function registerStylesheet(string $path) $this->publishes([ $path => public_path("vendor/{$name}/css/{$filename}.css"), - ]); + ], $this->getAddon()->slug()); Statamic::style($name, $filename); } @@ -333,4 +335,20 @@ private function getAddon() return Str::startsWith($class, $addon->namespace()); }); } + + protected function bootPublishAfterInstall() + { + if (! $this->publishAfterInstall) { + return $this; + } + + Statamic::afterInstalled(function ($command) { + $command->call('vendor:publish', [ + '--tag' => $this->getAddon()->slug(), + '--force' => true + ]); + }); + + return $this; + } } diff --git a/src/Statamic.php b/src/Statamic.php index 9f71a261e3..4b0b80b1cb 100644 --- a/src/Statamic.php +++ b/src/Statamic.php @@ -22,6 +22,7 @@ class Statamic protected static $actionRoutes = []; protected static $jsonVariables = []; protected static $bootedCallbacks = []; + protected static $afterInstalledCallbacks = []; public static function version() { @@ -245,4 +246,16 @@ public static function runBootedCallbacks() $callback(); } } + + public static function afterInstalled(Closure $callback) + { + static::$afterInstalledCallbacks[] = $callback; + } + + public static function runAfterInstalledCallbacks($command) + { + foreach (static::$afterInstalledCallbacks as $callback) { + $callback($command); + } + } }