glastree_on_gitea

This commit is contained in:
2026-05-26 08:14:29 +02:00
commit 0bed099d05
9556 changed files with 1186307 additions and 0 deletions
@@ -0,0 +1,28 @@
<?php
namespace Spatie\LaravelPackageTools\Commands\Concerns;
trait AskToRunMigrations
{
protected bool $askToRunMigrations = false;
public function askToRunMigrations(): self
{
$this->askToRunMigrations = true;
return $this;
}
protected function processAskToRunMigrations(): self
{
if ($this->askToRunMigrations) {
if ($this->confirm('Would you like to run the migrations now?')) {
$this->comment('Running migrations...');
$this->call('migrate');
}
}
return $this;
}
}
@@ -0,0 +1,39 @@
<?php
namespace Spatie\LaravelPackageTools\Commands\Concerns;
trait AskToStarRepoOnGitHub
{
protected ?string $starRepo = null;
protected bool $defaultStarAnswer = true;
public function askToStarRepoOnGitHub($vendorSlashRepoName, bool $defaultAnswer = false): self
{
$this->starRepo = $vendorSlashRepoName;
$this->defaultStarAnswer = $defaultAnswer;
return $this;
}
protected function processStarRepo(): self
{
if ($this->starRepo) {
if ($this->confirm('Would you like to star our repo on GitHub?', $this->defaultStarAnswer)) {
$repoUrl = "https://github.com/{$this->starRepo}";
if (PHP_OS_FAMILY == 'Darwin') {
exec("open {$repoUrl}");
}
if (PHP_OS_FAMILY == 'Windows') {
exec("start {$repoUrl}");
}
if (PHP_OS_FAMILY == 'Linux') {
exec("xdg-open {$repoUrl}");
}
}
}
return $this;
}
}
@@ -0,0 +1,49 @@
<?php
namespace Spatie\LaravelPackageTools\Commands\Concerns;
trait PublishesResources
{
protected array $publishes = [];
public function publish(string ...$tag): self
{
$this->publishes = array_merge($this->publishes, $tag);
return $this;
}
public function publishAssets(): self
{
return $this->publish('assets');
}
public function publishConfigFile(): self
{
return $this->publish('config');
}
public function publishInertiaComponents(): self
{
return $this->publish('inertia-components');
}
public function publishMigrations(): self
{
return $this->publish('migrations');
}
protected function processPublishes(): self
{
foreach ($this->publishes as $tag) {
$name = str_replace('-', ' ', $tag);
$this->comment("Publishing {$name}...");
$this->callSilently("vendor:publish", [
'--tag' => "{$this->package->shortName()}-{$tag}",
]);
}
return $this;
}
}
@@ -0,0 +1,75 @@
<?php
namespace Spatie\LaravelPackageTools\Commands\Concerns;
use Illuminate\Support\Str;
trait SupportsServiceProviderInApp
{
protected bool $copyServiceProviderInApp = false;
public function copyAndRegisterServiceProviderInApp(): self
{
$this->copyServiceProviderInApp = true;
return $this;
}
protected function processCopyServiceProviderInApp(): self
{
if ($this->copyServiceProviderInApp) {
$this->comment('Publishing service provider...');
$this->copyServiceProviderInApp();
}
return $this;
}
protected function copyServiceProviderInApp(): self
{
$providerName = $this->package->publishableProviderName;
if (! $providerName) {
return $this;
}
$this->callSilent('vendor:publish', ['--tag' => $this->package->shortName() . '-provider']);
$namespace = Str::replaceLast('\\', '', $this->laravel->getNamespace());
if (intval(app()->version()) < 11 || ! file_exists(base_path('bootstrap/providers.php'))) {
$appConfig = file_get_contents(config_path('app.php'));
} else {
$appConfig = file_get_contents(base_path('bootstrap/providers.php'));
}
$class = '\\Providers\\' . Str::replace('/', '\\', $providerName) . '::class';
if (Str::contains($appConfig, $namespace . $class)) {
return $this;
}
if (intval(app()->version()) < 11 || ! file_exists(base_path('bootstrap/providers.php'))) {
file_put_contents(config_path('app.php'), str_replace(
"{$namespace}\\Providers\\BroadcastServiceProvider::class,",
"{$namespace}\\Providers\\BroadcastServiceProvider::class," . PHP_EOL . " {$namespace}{$class},",
$appConfig
));
} else {
file_put_contents(base_path('bootstrap/providers.php'), str_replace(
"{$namespace}\\Providers\\AppServiceProvider::class,",
"{$namespace}\\Providers\\AppServiceProvider::class," . PHP_EOL . " {$namespace}{$class},",
$appConfig
));
}
file_put_contents(app_path('Providers/' . $providerName . '.php'), str_replace(
"namespace App\Providers;",
"namespace {$namespace}\Providers;",
file_get_contents(app_path('Providers/' . $providerName . '.php'))
));
return $this;
}
}
@@ -0,0 +1,43 @@
<?php
namespace Spatie\LaravelPackageTools\Commands\Concerns;
use Closure;
trait SupportsStartWithEndWith
{
public ?Closure $startWith = null;
public ?Closure $endWith = null;
public function startWith(callable $callable): self
{
$this->startWith = $callable;
return $this;
}
public function endWith(callable $callable): self
{
$this->endWith = $callable;
return $this;
}
protected function processStartWith(): self
{
if ($this->startWith) {
($this->startWith)($this);
}
return $this;
}
protected function processEndWith(): self
{
if ($this->endWith) {
($this->endWith)($this);
}
return $this;
}
}
@@ -0,0 +1,48 @@
<?php
namespace Spatie\LaravelPackageTools\Commands;
use Illuminate\Console\Command;
use Spatie\LaravelPackageTools\Commands\Concerns\AskToRunMigrations;
use Spatie\LaravelPackageTools\Commands\Concerns\AskToStarRepoOnGitHub;
use Spatie\LaravelPackageTools\Commands\Concerns\PublishesResources;
use Spatie\LaravelPackageTools\Commands\Concerns\SupportsServiceProviderInApp;
use Spatie\LaravelPackageTools\Commands\Concerns\SupportsStartWithEndWith;
use Spatie\LaravelPackageTools\Package;
class InstallCommand extends Command
{
use AskToRunMigrations;
use AskToStarRepoOnGitHub;
use PublishesResources;
use SupportsServiceProviderInApp;
use SupportsStartWithEndWith;
protected Package $package;
public function __construct(Package $package)
{
$this->signature = $package->shortName() . ':install';
$this->description = 'Install ' . $package->name;
$this->package = $package;
$this->hidden = true;
parent::__construct();
}
public function handle()
{
$this
->processStartWith()
->processPublishes()
->processAskToRunMigrations()
->processCopyServiceProviderInApp()
->processStarRepo()
->processEndWith();
$this->info("{$this->package->shortName()} has been installed!");
}
}
@@ -0,0 +1,15 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasAssets
{
public bool $hasAssets = false;
public function hasAssets(): static
{
$this->hasAssets = true;
return $this;
}
}
@@ -0,0 +1,24 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasBladeComponents
{
public array $viewComponents = [];
public function hasViewComponent(string $prefix, string $viewComponentName): static
{
$this->viewComponents[$viewComponentName] = $prefix;
return $this;
}
public function hasViewComponents(string $prefix, ...$viewComponentNames): static
{
foreach ($viewComponentNames as $componentName) {
$this->viewComponents[$componentName] = $prefix;
}
return $this;
}
}
@@ -0,0 +1,43 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasCommands
{
public array $commands = [];
public array $consoleCommands = [];
public function hasCommand(string $commandClassName): static
{
$this->commands[] = $commandClassName;
return $this;
}
public function hasCommands(...$commandClassNames): static
{
$this->commands = array_merge(
$this->commands,
collect($commandClassNames)->flatten()->toArray()
);
return $this;
}
public function hasConsoleCommand(string $commandClassName): static
{
$this->consoleCommands[] = $commandClassName;
return $this;
}
public function hasConsoleCommands(...$commandClassNames): static
{
$this->consoleCommands = array_merge(
$this->consoleCommands,
collect($commandClassNames)->flatten()->toArray()
);
return $this;
}
}
@@ -0,0 +1,21 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasConfigs
{
public array $configFileNames = [];
public function hasConfigFile($configFileName = null): static
{
$configFileName ??= $this->shortName();
if (! is_array($configFileName)) {
$configFileName = [$configFileName];
}
$this->configFileNames = $configFileName;
return $this;
}
}
@@ -0,0 +1,17 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasInertia
{
public bool $hasInertiaComponents = false;
public function hasInertiaComponents(?string $namespace = null): static
{
$this->hasInertiaComponents = true;
$this->viewNamespace = $namespace;
return $this;
}
}
@@ -0,0 +1,19 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
use Spatie\LaravelPackageTools\Commands\InstallCommand;
trait HasInstallCommand
{
public function hasInstallCommand($callable): static
{
$installCommand = new InstallCommand($this);
$callable($installCommand);
$this->consoleCommands[] = $installCommand;
return $this;
}
}
@@ -0,0 +1,46 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasMigrations
{
public bool $runsMigrations = false;
public bool $discoversMigrations = false;
public ?string $migrationsPath = null;
public array $migrationFileNames = [];
public function runsMigrations(bool $runsMigrations = true): static
{
$this->runsMigrations = $runsMigrations;
return $this;
}
public function hasMigration(string $migrationFileName): static
{
$this->migrationFileNames[] = $migrationFileName;
return $this;
}
public function hasMigrations(...$migrationFileNames): static
{
$this->migrationFileNames = array_merge(
$this->migrationFileNames,
collect($migrationFileNames)->flatten()->toArray()
);
return $this;
}
public function discoversMigrations(bool $discoversMigrations = true, string $path = '/database/migrations'): static
{
$this->discoversMigrations = $discoversMigrations;
$this->migrationsPath = $path;
return $this;
}
}
@@ -0,0 +1,25 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasRoutes
{
public array $routeFileNames = [];
public function hasRoute(string $routeFileName): static
{
$this->routeFileNames[] = $routeFileName;
return $this;
}
public function hasRoutes(...$routeFileNames): static
{
$this->routeFileNames = array_merge(
$this->routeFileNames,
collect($routeFileNames)->flatten()->toArray()
);
return $this;
}
}
@@ -0,0 +1,15 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasServiceProviders
{
public ?string $publishableProviderName = null;
public function publishesServiceProvider(string $providerName): static
{
$this->publishableProviderName = $providerName;
return $this;
}
}
@@ -0,0 +1,15 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasTranslations
{
public bool $hasTranslations = false;
public function hasTranslations(): static
{
$this->hasTranslations = true;
return $this;
}
}
@@ -0,0 +1,21 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasViewComposers
{
public array $viewComposers = [];
public function hasViewComposer($view, $viewComposer): static
{
if (! is_array($view)) {
$view = [$view];
}
foreach ($view as $viewName) {
$this->viewComposers[$viewName] = $viewComposer;
}
return $this;
}
}
@@ -0,0 +1,15 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasViewSharedData
{
public array $sharedViewData = [];
public function sharesDataWithAllViews(string $name, $value): static
{
$this->sharedViewData[$name] = $value;
return $this;
}
}
@@ -0,0 +1,24 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\Package;
trait HasViews
{
public bool $hasViews = false;
public ?string $viewNamespace = null;
public function hasViews(?string $namespace = null): static
{
$this->hasViews = true;
$this->viewNamespace = $namespace;
return $this;
}
public function viewNamespace(): string
{
return $this->viewNamespace ?? $this->shortName();
}
}
@@ -0,0 +1,20 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
trait ProcessAssets
{
protected function bootPackageAssets(): static
{
if (! $this->package->hasAssets || ! $this->app->runningInConsole()) {
return $this;
}
$vendorAssets = $this->package->basePath('/../resources/dist');
$appAssets = public_path("vendor/{$this->package->shortName()}");
$this->publishes([$vendorAssets => $appAssets], "{$this->package->shortName()}-assets");
return $this;
}
}
@@ -0,0 +1,26 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
trait ProcessBladeComponents
{
protected function bootPackageBladeComponents(): self
{
if (empty($this->package->viewComponents)) {
return $this;
}
foreach ($this->package->viewComponents as $componentClass => $prefix) {
$this->loadViewComponentsAs($prefix, [$componentClass]);
}
if ($this->app->runningInConsole()) {
$vendorComponents = $this->package->basePath('/Components');
$appComponents = base_path("app/View/Components/vendor/{$this->package->shortName()}");
$this->publishes([$vendorComponents => $appComponents], "{$this->package->name}-components");
}
return $this;
}
}
@@ -0,0 +1,28 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
trait ProcessCommands
{
protected function bootPackageCommands(): self
{
if (empty($this->package->commands)) {
return $this;
}
$this->commands($this->package->commands);
return $this;
}
protected function bootPackageConsoleCommands(): self
{
if (empty($this->package->consoleCommands) || ! $this->app->runningInConsole()) {
return $this;
}
$this->commands($this->package->consoleCommands);
return $this;
}
}
@@ -0,0 +1,65 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
trait ProcessConfigs
{
public function registerPackageConfigs(): self
{
if (empty($this->package->configFileNames)) {
return $this;
}
foreach ($this->package->configFileNames as $configFileName) {
$configFilePath = $this->normalizeConfigPath($configFileName);
$vendorConfig = $this->package->basePath("/../config/{$configFilePath}.php");
// Only mergeConfigFile if a .php file and not if a stub file
if (! is_file($vendorConfig)) {
continue;
}
$normalizedConfigKey = $this->normalizeConfigKey($configFileName);
$this->mergeConfigFrom($vendorConfig, $normalizedConfigKey);
}
return $this;
}
protected function bootPackageConfigs(): self
{
if (empty($this->package->configFileNames) || ! $this->app->runningInConsole()) {
return $this;
}
foreach ($this->package->configFileNames as $configFileName) {
$configFilePath = $this->normalizeConfigPath($configFileName);
$vendorConfig ;
if (
! is_file($vendorConfig = $this->package->basePath("/../config/{$configFilePath}.php"))
&&
! is_file($vendorConfig = $this->package->basePath("/../config/{$configFilePath}.php.stub"))
) {
continue;
}
$this->publishes(
[$vendorConfig => config_path("{$configFilePath}.php")],
"{$this->package->shortName()}-config"
);
}
return $this;
}
protected function normalizeConfigKey(string $configFileName): string
{
return str_replace(['/', '\\'], '.', $configFileName);
}
protected function normalizeConfigPath(string $configFileName): string
{
return str_replace(['/', '\\'], DIRECTORY_SEPARATOR, $configFileName);
}
}
@@ -0,0 +1,29 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
use Illuminate\Support\Str;
trait ProcessInertia
{
protected function bootPackageInertia(): self
{
if (! $this->package->hasInertiaComponents) {
return $this;
}
$namespace = $this->package->viewNamespace;
$directoryName = Str::of($this->packageView($namespace))->studly()->remove('-')->value();
$vendorComponents = $this->package->basePath('/../resources/js/Pages');
$appComponents = base_path("resources/js/Pages/{$directoryName}");
if ($this->app->runningInConsole()) {
$this->publishes(
[$vendorComponents => $appComponents],
"{$this->packageView($namespace)}-inertia-components"
);
}
return $this;
}
}
@@ -0,0 +1,108 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Str;
trait ProcessMigrations
{
protected function bootPackageMigrations(): self
{
if ($this->package->discoversMigrations) {
$this->discoverPackageMigrations();
return $this;
}
$now = Carbon::now();
foreach ($this->package->migrationFileNames as $migrationFileName) {
$vendorMigration = $this->package->basePath("/../database/migrations/{$migrationFileName}.php");
// Support for the .stub file extension
if (! file_exists($vendorMigration)) {
$vendorMigration .= '.stub';
}
if ($this->app->runningInConsole()) {
$appMigration = $this->generateMigrationName($migrationFileName, $now->addSecond());
$this->publishes(
[$vendorMigration => $appMigration],
"{$this->package->shortName()}-migrations"
);
}
if ($this->package->runsMigrations) {
$this->loadMigrationsFrom($vendorMigration);
}
}
return $this;
}
protected function discoverPackageMigrations(): void
{
$now = Carbon::now();
$migrationsPath = trim($this->package->migrationsPath, '/');
$files = (new Filesystem())->files($this->package->basePath("/../{$migrationsPath}"));
foreach ($files as $file) {
$filePath = $file->getPathname();
$migrationFileName = Str::replace(['.stub', '.php'], '', $file->getFilename());
// Publish but do not add timestamp to non migration files
if (Str::endsWith($filePath, [".php", ".php.stub"])) {
$appMigration = $this->generateMigrationName($migrationFileName, $now->addSecond());
} else {
$appMigration = database_path("migrations/{$file->getFilename()}");
}
if ($this->app->runningInConsole()) {
$this->publishes(
[$filePath => $appMigration],
"{$this->package->shortName()}-migrations"
);
}
// Do not load non migration files
if ($this->package->runsMigrations && Str::endsWith($filePath, [".php", ".php.stub"])) {
$this->loadMigrationsFrom($filePath);
}
}
}
protected function generateMigrationName(string $migrationFileName, Carbon|CarbonImmutable $now): string
{
$migrationsPath = 'migrations/' . dirname($migrationFileName) . '/';
$migrationFileName = basename($migrationFileName);
$len = strlen($migrationFileName) + 4;
if (Str::contains($migrationFileName, '/')) {
$migrationsPath .= Str::of($migrationFileName)->beforeLast('/')->finish('/');
$migrationFileName = Str::of($migrationFileName)->afterLast('/');
}
foreach (glob(database_path("{$migrationsPath}*.php")) as $filename) {
if ((substr($filename, -$len) === $migrationFileName . '.php')) {
return $filename;
}
}
$migrationFileName = self::stripTimestampPrefix($migrationFileName);
$timestamp = $now->format('Y_m_d_His');
$formattedFileName = Str::of($migrationFileName)->snake()->finish('.php');
return database_path("{$migrationsPath}{$timestamp}_{$formattedFileName}");
}
private static function stripTimestampPrefix(string $filename): string
{
return preg_replace('/^\d{4}_\d{2}_\d{2}_\d{6}_/', '', $filename);
}
}
@@ -0,0 +1,19 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
trait ProcessRoutes
{
protected function bootPackageRoutes(): self
{
if (empty($this->package->routeFileNames)) {
return $this;
}
foreach ($this->package->routeFileNames as $routeFileName) {
$this->loadRoutesFrom("{$this->package->basePath('/../routes/')}{$routeFileName}.php");
}
return $this;
}
}
@@ -0,0 +1,21 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
trait ProcessServiceProviders
{
protected function bootPackageServiceProviders(): self
{
if (! $this->package->publishableProviderName || ! $this->app->runningInConsole()) {
return $this;
}
$providerName = $this->package->publishableProviderName;
$vendorProvider = $this->package->basePath("/../resources/stubs/{$providerName}.php.stub");
$appProvider = base_path("app/Providers/{$providerName}.php");
$this->publishes([$vendorProvider => $appProvider], "{$this->package->shortName()}-provider");
return $this;
}
}
@@ -0,0 +1,32 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
trait ProcessTranslations
{
protected function bootPackageTranslations(): self
{
if (! $this->package->hasTranslations) {
return $this;
}
$vendorTranslations = $this->package->basePath('/../resources/lang');
$appTranslations = (function_exists('lang_path'))
? lang_path("vendor/{$this->package->shortName()}")
: resource_path("lang/vendor/{$this->package->shortName()}");
$this->loadTranslationsFrom($vendorTranslations, $this->package->shortName());
$this->loadJsonTranslationsFrom($vendorTranslations);
$this->loadJsonTranslationsFrom($appTranslations);
if ($this->app->runningInConsole()) {
$this->publishes(
[$vendorTranslations => $appTranslations],
"{$this->package->shortName()}-translations"
);
}
return $this;
}
}
@@ -0,0 +1,21 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
use Illuminate\Support\Facades\View;
trait ProcessViewComposers
{
protected function bootPackageViewComposers(): self
{
if (empty($this->package->viewComposers)) {
return $this;
}
foreach ($this->package->viewComposers as $viewName => $viewComposer) {
View::composer($viewName, $viewComposer);
}
return $this;
}
}
@@ -0,0 +1,21 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
use Illuminate\Support\Facades\View;
trait ProcessViewSharedData
{
protected function bootPackageViewSharedData(): self
{
if (empty($this->package->sharedViewData)) {
return $this;
}
foreach ($this->package->sharedViewData as $name => $value) {
View::share($name, $value);
}
return $this;
}
}
@@ -0,0 +1,26 @@
<?php
namespace Spatie\LaravelPackageTools\Concerns\PackageServiceProvider;
trait ProcessViews
{
protected function bootPackageViews(): self
{
if (! $this->package->hasViews) {
return $this;
}
$namespace = $this->package->viewNamespace;
$viewsPath = $this->package->basePath('/../resources/views');
$vendorViews = realpath($viewsPath) ?: $viewsPath;
$appViews = base_path("resources/views/vendor/{$this->packageView($namespace)}");
$this->loadViewsFrom($vendorViews, $this->package->viewNamespace());
if ($this->app->runningInConsole()) {
$this->publishes([$vendorViews => $appViews], "{$this->packageView($namespace)}-views");
}
return $this;
}
}
@@ -0,0 +1,13 @@
<?php
namespace Spatie\LaravelPackageTools\Exceptions;
use Exception;
class InvalidPackage extends Exception
{
public static function nameIsRequired(): self
{
return new static('This package does not have a name. You can set one with `$package->name("yourName")`');
}
}
+67
View File
@@ -0,0 +1,67 @@
<?php
namespace Spatie\LaravelPackageTools;
use Illuminate\Support\Str;
use Spatie\LaravelPackageTools\Concerns\Package\HasAssets;
use Spatie\LaravelPackageTools\Concerns\Package\HasBladeComponents;
use Spatie\LaravelPackageTools\Concerns\Package\HasCommands;
use Spatie\LaravelPackageTools\Concerns\Package\HasConfigs;
use Spatie\LaravelPackageTools\Concerns\Package\HasInertia;
use Spatie\LaravelPackageTools\Concerns\Package\HasInstallCommand;
use Spatie\LaravelPackageTools\Concerns\Package\HasMigrations;
use Spatie\LaravelPackageTools\Concerns\Package\HasRoutes;
use Spatie\LaravelPackageTools\Concerns\Package\HasServiceProviders;
use Spatie\LaravelPackageTools\Concerns\Package\HasTranslations;
use Spatie\LaravelPackageTools\Concerns\Package\HasViewComposers;
use Spatie\LaravelPackageTools\Concerns\Package\HasViews;
use Spatie\LaravelPackageTools\Concerns\Package\HasViewSharedData;
class Package
{
use HasAssets;
use HasBladeComponents;
use HasCommands;
use HasConfigs;
use HasInertia;
use HasInstallCommand;
use HasMigrations;
use HasRoutes;
use HasServiceProviders;
use HasTranslations;
use HasViewComposers;
use HasViews;
use HasViewSharedData;
public string $name;
public string $basePath;
public function name(string $name): static
{
$this->name = $name;
return $this;
}
public function shortName(): string
{
return Str::after($this->name, 'laravel-');
}
public function basePath(?string $directory = null): string
{
if ($directory === null) {
return $this->basePath;
}
return $this->basePath . DIRECTORY_SEPARATOR . ltrim($directory, DIRECTORY_SEPARATOR);
}
public function setBasePath(string $path): static
{
$this->basePath = $path;
return $this;
}
}
@@ -0,0 +1,126 @@
<?php
namespace Spatie\LaravelPackageTools;
use Illuminate\Support\ServiceProvider;
use ReflectionClass;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessAssets;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessBladeComponents;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessCommands;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessConfigs;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessInertia;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessMigrations;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessRoutes;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessServiceProviders;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessTranslations;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessViewComposers;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessViews;
use Spatie\LaravelPackageTools\Concerns\PackageServiceProvider\ProcessViewSharedData;
use Spatie\LaravelPackageTools\Exceptions\InvalidPackage;
abstract class PackageServiceProvider extends ServiceProvider
{
use ProcessAssets;
use ProcessBladeComponents;
use ProcessCommands;
use ProcessConfigs;
use ProcessInertia;
use ProcessMigrations;
use ProcessRoutes;
use ProcessServiceProviders;
use ProcessTranslations;
use ProcessViewComposers;
use ProcessViews;
use ProcessViewSharedData;
protected Package $package;
abstract public function configurePackage(Package $package): void;
/** @throws InvalidPackage */
public function register()
{
$this->registeringPackage();
$this->package = $this->newPackage();
$this->package->setBasePath($this->getPackageBaseDir());
$this->configurePackage($this->package);
if (empty($this->package->name)) {
throw InvalidPackage::nameIsRequired();
}
$this->registerPackageConfigs();
$this->packageRegistered();
return $this;
}
public function registeringPackage()
{
}
public function newPackage(): Package
{
return new Package();
}
public function packageRegistered()
{
}
public function boot()
{
$this->bootingPackage();
$this
->bootPackageAssets()
->bootPackageBladeComponents()
->bootPackageCommands()
->bootPackageConsoleCommands()
->bootPackageConfigs()
->bootPackageInertia()
->bootPackageMigrations()
->bootPackageRoutes()
->bootPackageServiceProviders()
->bootPackageTranslations()
->bootPackageViews()
->bootPackageViewComposers()
->bootPackageViewSharedData()
->packageBooted();
return $this;
}
public function bootingPackage()
{
}
public function packageBooted()
{
}
protected function getPackageBaseDir(): string
{
$reflector = new ReflectionClass(get_class($this));
$packageBaseDir = dirname($reflector->getFileName());
// Some packages like to keep Laravels directory structure and place
// the service providers in a Providers folder.
// move up a level when this is the case.
if (str_ends_with($packageBaseDir, DIRECTORY_SEPARATOR.'Providers')) {
$packageBaseDir = dirname($packageBaseDir);
}
return $packageBaseDir;
}
public function packageView(?string $namespace): ?string
{
return is_null($namespace)
? $this->package->shortName()
: $this->package->viewNamespace;
}
}