vendor and env first commit
This commit is contained in:
@@ -0,0 +1,199 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\LaravelPackageTools\Commands;
|
||||
|
||||
use Closure;
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\LaravelPackageTools\Package;
|
||||
|
||||
class InstallCommand extends Command
|
||||
{
|
||||
protected Package $package;
|
||||
|
||||
public ?Closure $startWith = null;
|
||||
|
||||
protected array $publishes = [];
|
||||
|
||||
protected bool $askToRunMigrations = false;
|
||||
|
||||
protected bool $copyServiceProviderInApp = false;
|
||||
|
||||
protected ?string $starRepo = null;
|
||||
|
||||
public ?Closure $endWith = null;
|
||||
|
||||
public $hidden = true;
|
||||
|
||||
public function __construct(Package $package)
|
||||
{
|
||||
$this->signature = $package->shortName() . ':install';
|
||||
|
||||
$this->description = 'Install ' . $package->name;
|
||||
|
||||
$this->package = $package;
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
public function handle()
|
||||
{
|
||||
if ($this->startWith) {
|
||||
($this->startWith)($this);
|
||||
}
|
||||
|
||||
foreach ($this->publishes as $tag) {
|
||||
$name = str_replace('-', ' ', $tag);
|
||||
$this->comment("Publishing {$name}...");
|
||||
|
||||
$this->callSilently("vendor:publish", [
|
||||
'--tag' => "{$this->package->shortName()}-{$tag}",
|
||||
]);
|
||||
}
|
||||
|
||||
if ($this->askToRunMigrations) {
|
||||
if ($this->confirm('Would you like to run the migrations now?')) {
|
||||
$this->comment('Running migrations...');
|
||||
|
||||
$this->call('migrate');
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->copyServiceProviderInApp) {
|
||||
$this->comment('Publishing service provider...');
|
||||
|
||||
$this->copyServiceProviderInApp();
|
||||
}
|
||||
|
||||
if ($this->starRepo) {
|
||||
if ($this->confirm('Would you like to star our repo on GitHub?')) {
|
||||
$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}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->info("{$this->package->shortName()} has been installed!");
|
||||
|
||||
if ($this->endWith) {
|
||||
($this->endWith)($this);
|
||||
}
|
||||
}
|
||||
|
||||
public function publish(string ...$tag): self
|
||||
{
|
||||
$this->publishes = array_merge($this->publishes, $tag);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function publishConfigFile(): self
|
||||
{
|
||||
return $this->publish('config');
|
||||
}
|
||||
|
||||
public function publishAssets(): self
|
||||
{
|
||||
return $this->publish('assets');
|
||||
}
|
||||
|
||||
public function publishInertiaComponents(): self
|
||||
{
|
||||
return $this->publish('inertia-components');
|
||||
}
|
||||
|
||||
public function publishMigrations(): self
|
||||
{
|
||||
return $this->publish('migrations');
|
||||
}
|
||||
|
||||
public function askToRunMigrations(): self
|
||||
{
|
||||
$this->askToRunMigrations = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function copyAndRegisterServiceProviderInApp(): self
|
||||
{
|
||||
$this->copyServiceProviderInApp = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function askToStarRepoOnGitHub($vendorSlashRepoName): self
|
||||
{
|
||||
$this->starRepo = $vendorSlashRepoName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function startWith($callable): self
|
||||
{
|
||||
$this->startWith = $callable;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function endWith($callable): self
|
||||
{
|
||||
$this->endWith = $callable;
|
||||
|
||||
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\\' . $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,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")`');
|
||||
}
|
||||
}
|
||||
+241
@@ -0,0 +1,241 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\LaravelPackageTools;
|
||||
|
||||
use Illuminate\Support\Str;
|
||||
use Spatie\LaravelPackageTools\Commands\InstallCommand;
|
||||
|
||||
class Package
|
||||
{
|
||||
public string $name;
|
||||
|
||||
public array $configFileNames = [];
|
||||
|
||||
public bool $hasViews = false;
|
||||
|
||||
public bool $hasInertiaComponents = false;
|
||||
|
||||
public ?string $viewNamespace = null;
|
||||
|
||||
public bool $hasTranslations = false;
|
||||
|
||||
public bool $hasAssets = false;
|
||||
|
||||
public bool $runsMigrations = false;
|
||||
|
||||
public array $migrationFileNames = [];
|
||||
|
||||
public array $routeFileNames = [];
|
||||
|
||||
public array $commands = [];
|
||||
|
||||
public array $consoleCommands = [];
|
||||
|
||||
public array $viewComponents = [];
|
||||
|
||||
public array $sharedViewData = [];
|
||||
|
||||
public array $viewComposers = [];
|
||||
|
||||
public string $basePath;
|
||||
|
||||
public ?string $publishableProviderName = null;
|
||||
|
||||
public function name(string $name): static
|
||||
{
|
||||
$this->name = $name;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasConfigFile($configFileName = null): static
|
||||
{
|
||||
$configFileName ??= $this->shortName();
|
||||
|
||||
if (! is_array($configFileName)) {
|
||||
$configFileName = [$configFileName];
|
||||
}
|
||||
|
||||
$this->configFileNames = $configFileName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function publishesServiceProvider(string $providerName): static
|
||||
{
|
||||
$this->publishableProviderName = $providerName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasInstallCommand($callable): static
|
||||
{
|
||||
$installCommand = new InstallCommand($this);
|
||||
|
||||
$callable($installCommand);
|
||||
|
||||
$this->consoleCommands[] = $installCommand;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function shortName(): string
|
||||
{
|
||||
return Str::after($this->name, 'laravel-');
|
||||
}
|
||||
|
||||
public function hasViews(string $namespace = null): static
|
||||
{
|
||||
$this->hasViews = true;
|
||||
|
||||
$this->viewNamespace = $namespace;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasInertiaComponents(string $namespace = null): static
|
||||
{
|
||||
$this->hasInertiaComponents = true;
|
||||
|
||||
$this->viewNamespace = $namespace;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public function sharesDataWithAllViews(string $name, $value): static
|
||||
{
|
||||
$this->sharedViewData[$name] = $value;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasViewComposer($view, $viewComposer): static
|
||||
{
|
||||
if (! is_array($view)) {
|
||||
$view = [$view];
|
||||
}
|
||||
|
||||
foreach ($view as $viewName) {
|
||||
$this->viewComposers[$viewName] = $viewComposer;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasTranslations(): static
|
||||
{
|
||||
$this->hasTranslations = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasAssets(): static
|
||||
{
|
||||
$this->hasAssets = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
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 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;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public function basePath(string $directory = null): string
|
||||
{
|
||||
if ($directory === null) {
|
||||
return $this->basePath;
|
||||
}
|
||||
|
||||
return $this->basePath . DIRECTORY_SEPARATOR . ltrim($directory, DIRECTORY_SEPARATOR);
|
||||
}
|
||||
|
||||
public function viewNamespace(): string
|
||||
{
|
||||
return $this->viewNamespace ?? $this->shortName();
|
||||
}
|
||||
|
||||
public function setBasePath(string $path): static
|
||||
{
|
||||
$this->basePath = $path;
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,216 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\LaravelPackageTools;
|
||||
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Illuminate\Support\Str;
|
||||
use ReflectionClass;
|
||||
use Spatie\LaravelPackageTools\Exceptions\InvalidPackage;
|
||||
|
||||
abstract class PackageServiceProvider extends ServiceProvider
|
||||
{
|
||||
protected Package $package;
|
||||
|
||||
abstract public function configurePackage(Package $package): void;
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
foreach ($this->package->configFileNames as $configFileName) {
|
||||
$this->mergeConfigFrom($this->package->basePath("/../config/{$configFileName}.php"), $configFileName);
|
||||
}
|
||||
|
||||
$this->packageRegistered();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function newPackage(): Package
|
||||
{
|
||||
return new Package();
|
||||
}
|
||||
|
||||
public function boot()
|
||||
{
|
||||
$this->bootingPackage();
|
||||
|
||||
if ($this->package->hasTranslations) {
|
||||
$langPath = 'vendor/' . $this->package->shortName();
|
||||
|
||||
$langPath = (function_exists('lang_path'))
|
||||
? lang_path($langPath)
|
||||
: resource_path('lang/' . $langPath);
|
||||
}
|
||||
|
||||
if ($this->app->runningInConsole()) {
|
||||
foreach ($this->package->configFileNames as $configFileName) {
|
||||
$this->publishes([
|
||||
$this->package->basePath("/../config/{$configFileName}.php") => config_path("{$configFileName}.php"),
|
||||
], "{$this->package->shortName()}-config");
|
||||
}
|
||||
|
||||
if ($this->package->hasViews) {
|
||||
$this->publishes([
|
||||
$this->package->basePath('/../resources/views') => base_path("resources/views/vendor/{$this->packageView($this->package->viewNamespace)}"),
|
||||
], "{$this->packageView($this->package->viewNamespace)}-views");
|
||||
}
|
||||
|
||||
if ($this->package->hasInertiaComponents) {
|
||||
$packageDirectoryName = Str::of($this->packageView($this->package->viewNamespace))->studly()->remove('-')->value();
|
||||
|
||||
$this->publishes([
|
||||
$this->package->basePath('/../resources/js/Pages') => base_path("resources/js/Pages/{$packageDirectoryName}"),
|
||||
], "{$this->packageView($this->package->viewNamespace)}-inertia-components");
|
||||
}
|
||||
|
||||
$now = Carbon::now();
|
||||
foreach ($this->package->migrationFileNames as $migrationFileName) {
|
||||
$filePath = $this->package->basePath("/../database/migrations/{$migrationFileName}.php");
|
||||
if (! file_exists($filePath)) {
|
||||
// Support for the .stub file extension
|
||||
$filePath .= '.stub';
|
||||
}
|
||||
|
||||
$this->publishes([
|
||||
$filePath => $this->generateMigrationName(
|
||||
$migrationFileName,
|
||||
$now->addSecond()
|
||||
), ], "{$this->package->shortName()}-migrations");
|
||||
|
||||
if ($this->package->runsMigrations) {
|
||||
$this->loadMigrationsFrom($filePath);
|
||||
}
|
||||
}
|
||||
|
||||
if ($this->package->hasTranslations) {
|
||||
$this->publishes([
|
||||
$this->package->basePath('/../resources/lang') => $langPath,
|
||||
], "{$this->package->shortName()}-translations");
|
||||
}
|
||||
|
||||
if ($this->package->hasAssets) {
|
||||
$this->publishes([
|
||||
$this->package->basePath('/../resources/dist') => public_path("vendor/{$this->package->shortName()}"),
|
||||
], "{$this->package->shortName()}-assets");
|
||||
}
|
||||
}
|
||||
|
||||
if (! empty($this->package->commands)) {
|
||||
$this->commands($this->package->commands);
|
||||
}
|
||||
|
||||
if (! empty($this->package->consoleCommands) && $this->app->runningInConsole()) {
|
||||
$this->commands($this->package->consoleCommands);
|
||||
}
|
||||
|
||||
if ($this->package->hasTranslations) {
|
||||
$this->loadTranslationsFrom(
|
||||
$this->package->basePath('/../resources/lang/'),
|
||||
$this->package->shortName()
|
||||
);
|
||||
|
||||
$this->loadJsonTranslationsFrom($this->package->basePath('/../resources/lang/'));
|
||||
|
||||
$this->loadJsonTranslationsFrom($langPath);
|
||||
}
|
||||
|
||||
if ($this->package->hasViews) {
|
||||
$this->loadViewsFrom($this->package->basePath('/../resources/views'), $this->package->viewNamespace());
|
||||
}
|
||||
|
||||
foreach ($this->package->viewComponents as $componentClass => $prefix) {
|
||||
$this->loadViewComponentsAs($prefix, [$componentClass]);
|
||||
}
|
||||
|
||||
if (count($this->package->viewComponents)) {
|
||||
$this->publishes([
|
||||
$this->package->basePath('/Components') => base_path("app/View/Components/vendor/{$this->package->shortName()}"),
|
||||
], "{$this->package->name}-components");
|
||||
}
|
||||
|
||||
if ($this->package->publishableProviderName) {
|
||||
$this->publishes([
|
||||
$this->package->basePath("/../resources/stubs/{$this->package->publishableProviderName}.php.stub") => base_path("app/Providers/{$this->package->publishableProviderName}.php"),
|
||||
], "{$this->package->shortName()}-provider");
|
||||
}
|
||||
|
||||
|
||||
foreach ($this->package->routeFileNames as $routeFileName) {
|
||||
$this->loadRoutesFrom("{$this->package->basePath('/../routes/')}{$routeFileName}.php");
|
||||
}
|
||||
|
||||
foreach ($this->package->sharedViewData as $name => $value) {
|
||||
View::share($name, $value);
|
||||
}
|
||||
|
||||
foreach ($this->package->viewComposers as $viewName => $viewComposer) {
|
||||
View::composer($viewName, $viewComposer);
|
||||
}
|
||||
|
||||
$this->packageBooted();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public static function generateMigrationName(string $migrationFileName, Carbon $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;
|
||||
}
|
||||
}
|
||||
|
||||
return database_path($migrationsPath . $now->format('Y_m_d_His') . '_' . Str::of($migrationFileName)->snake()->finish('.php'));
|
||||
}
|
||||
|
||||
public function registeringPackage()
|
||||
{
|
||||
}
|
||||
|
||||
public function packageRegistered()
|
||||
{
|
||||
}
|
||||
|
||||
public function bootingPackage()
|
||||
{
|
||||
}
|
||||
|
||||
public function packageBooted()
|
||||
{
|
||||
}
|
||||
|
||||
protected function getPackageBaseDir(): string
|
||||
{
|
||||
$reflector = new ReflectionClass(get_class($this));
|
||||
|
||||
return dirname($reflector->getFileName());
|
||||
}
|
||||
|
||||
public function packageView(?string $namespace)
|
||||
{
|
||||
return is_null($namespace) ? $this->package->shortName() : $this->package->viewNamespace;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user