[增添]添加了datasource的setting数据库以及默认值

This commit is contained in:
makotocc0107
2024-08-27 09:57:44 +08:00
parent d111dfaea4
commit 72eb990970
10955 changed files with 978898 additions and 0 deletions

View File

@@ -0,0 +1,21 @@
The MIT License (MIT)
Copyright (c) spatie <freek@spatie.be>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

View File

@@ -0,0 +1,553 @@
# Tools for creating Laravel packages
[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/laravel-package-tools.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-package-tools)
![Tests](https://github.com/spatie/laravel-package-tools/workflows/Tests/badge.svg)
[![Total Downloads](https://img.shields.io/packagist/dt/spatie/laravel-package-tools.svg?style=flat-square)](https://packagist.org/packages/spatie/laravel-package-tools)
This package contains a `PackageServiceProvider` that you can use in your packages to easily register config files,
migrations, and more.
Here's an example of how it can be used.
```php
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Spatie\LaravelPackageTools\Package;
use MyPackage\ViewComponents\Alert;
use Spatie\LaravelPackageTools\Commands\InstallCommand;
class YourPackageServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package
->name('your-package-name')
->hasConfigFile()
->hasViews()
->hasViewComponent('spatie', Alert::class)
->hasViewComposer('*', MyViewComposer::class)
->sharesDataWithAllViews('downloads', 3)
->hasTranslations()
->hasAssets()
->publishesServiceProvider('MyProviderName')
->hasRoute('web')
->hasMigration('create_package_tables')
->hasCommand(YourCoolPackageCommand::class)
->hasInstallCommand(function(InstallCommand $command) {
$command
->publishConfigFile()
->publishAssets()
->publishMigrations()
->copyAndRegisterServiceProviderInApp()
->askToStarRepoOnGitHub();
});
}
}
```
Under the hood it will do the necessary work to register the necessary things and make all sorts of files publishable.
## Support us
[<img src="https://github-ads.s3.eu-central-1.amazonaws.com/laravel-package-tools.jpg?t=1" width="419px" />](https://spatie.be/github-ad-click/laravel-package-tools)
We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can
support us by [buying one of our paid products](https://spatie.be/open-source/support-us).
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.
You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards
on [our virtual postcard wall](https://spatie.be/open-source/postcards).
## Getting started
This package is opinionated on how you should structure your package. To get started easily, consider
using [our package-skeleton repo](https://github.com/spatie/package-skeleton-laravel) to start your package. The
skeleton is structured perfectly to work perfectly with the `PackageServiceProvider` in this package.
## Usage
In your package you should let your service provider extend `Spatie\LaravelPackageTools\PackageServiceProvider`.
```php
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Spatie\LaravelPackageTools\Package;
class YourPackageServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package) : void
{
$package->name('your-package-name');
}
}
```
Passing the package name to `name` is mandatory.
### Working with a config file
To register a config file, you should create a php file with your package name in the `config` directory of your
package. In this example it should be at `<package root>/config/your-package-name.php`.
If your package name starts with `laravel-`, we expect that your config file does not contain that prefix. So if your
package name is `laravel-cool-package`, the config file should be named `cool-package.php`.
To register that config file, call `hasConfigFile()` on `$package` in the `configurePackage` method.
```php
$package
->name('your-package-name')
->hasConfigFile();
```
The `hasConfigFile` method will also make the config file publishable. Users of your package will be able to publish the
config file with this command.
```bash
php artisan vendor:publish --tag=your-package-name-config
```
Should your package have multiple config files, you can pass their names as an array to `hasConfigFile`
```php
$package
->name('your-package-name')
->hasConfigFile(['my-config-file', 'another-config-file']);
```
### Working with views
Any views your package provides, should be placed in the `<package root>/resources/views` directory.
You can register these views with the `hasViews` command.
```php
$package
->name('your-package-name')
->hasViews();
```
This will register your views with Laravel.
If you have a view `<package root>/resources/views/myView.blade.php`, you can use it like
this: `view('your-package-name::myView')`. Of course, you can also use subdirectories to organise your views. A view
located at `<package root>/resources/views/subdirectory/myOtherView.blade.php` can be used
with `view('your-package-name::subdirectory.myOtherView')`.
#### Using a custom view namespace
You can pass a custom view namespace to the `hasViews` method.
```php
$package
->name('your-package-name')
->hasViews('custom-view-namespace');
```
You can now use the views of the package like this:
```php
view('custom-view-namespace::myView');
```
#### Publishing the views
Calling `hasViews` will also make views publishable. Users of your package will be able to publish the views with this
command:
```bash
php artisan vendor:publish --tag=your-package-name-views
```
> **Note:**
>
> If you use custom view namespace then you should change your publish command like this:
```bash
php artisan vendor:publish --tag=custom-view-namespace-views
```
### Sharing global data with views
You can share data with all views using the `sharesDataWithAllViews` method. This will make the shared variable
available to all views.
```php
$package
->name('your-package-name')
->sharesDataWithAllViews('companyName', 'Spatie');
```
### Working with Blade view components
Any Blade view components that your package provides should be placed in the `<package root>/src/Components` directory.
You can register these views with the `hasViewComponents` command.
```php
$package
->name('your-package-name')
->hasViewComponents('spatie', Alert::class);
```
This will register your view components with Laravel. In the case of `Alert::class`, it can be referenced in views
as `<x-spatie-alert />`, where `spatie` is the prefix you provided during registration.
Calling `hasViewComponents` will also make view components publishable, and will be published
to `app/Views/Components/vendor/<package name>`.
Users of your package will be able to publish the view components with this command:
```bash
php artisan vendor:publish --tag=your-package-name-components
```
### Working with view composers
You can register any view composers that your project uses with the `hasViewComposers` method. You may also register a
callback that receives a `$view` argument instead of a classname.
To register a view composer with all views, use an asterisk as the view name `'*'`.
```php
$package
->name('your-package-name')
->hasViewComposer('viewName', MyViewComposer::class)
->hasViewComposer('*', function($view) {
$view->with('sharedVariable', 123);
});
```
### Working with inertia components
Any `.vue` or `.jsx` files your package provides, should be placed in the `<package root>/resources/js/Pages` directory.
You can register these components with the `hasInertiaComponents` command.
```php
$package
->name('your-package-name')
->hasInertiaComponents();
```
This will register your components with Laravel.
The user should publish the inertia components manually or using the [installer-command](#adding-an-installer-command) in order to use them.
If you have an inertia component `<package root>/resources/js/Pages/myComponent.vue`, you can use it like
this: `Inertia::render('YourPackageName/myComponent')`. Of course, you can also use subdirectories to organise your components.
#### Publishing inertia components
Calling `hasInertiaComponents` will also make inertia components publishable. Users of your package will be able to publish the views with this
command:
```bash
php artisan vendor:publish --tag=your-package-name-inertia-components
```
Also, the inertia components are available in a convenient way with your package [installer-command](#adding-an-installer-command)
### Working with translations
Any translations your package provides, should be placed in the `<package root>/resources/lang/<language-code>`
directory.
You can register these translations with the `hasTranslations` command.
```php
$package
->name('your-package-name')
->hasTranslations();
```
This will register the translations with Laravel.
Assuming you save this translation file at `<package root>/resources/lang/en/translations.php`...
```php
return [
'translatable' => 'translation',
];
```
... your package and users will be able to retrieve the translation with:
```php
trans('your-package-name::translations.translatable'); // returns 'translation'
```
If your package name starts with `laravel-` then you should leave that off in the example above.
Coding with translation strings as keys, you should create JSON files
in `<package root>/resources/lang/<language-code>.json`.
For example, creating `<package root>/resources/lang/it.json` file like so:
```json
{
"Hello!": "Ciao!"
}
```
...the output of...
```php
trans('Hello!');
```
...will be `Ciao!` if the application uses the Italian language.
Calling `hasTranslations` will also make translations publishable. Users of your package will be able to publish the
translations with this command:
```bash
php artisan vendor:publish --tag=your-package-name-translations
```
### Working with assets
Any assets your package provides, should be placed in the `<package root>/resources/dist/` directory.
You can make these assets publishable the `hasAssets` method.
```php
$package
->name('your-package-name')
->hasAssets();
```
Users of your package will be able to publish the assets with this command:
```bash
php artisan vendor:publish --tag=your-package-name-assets
```
This will copy over the assets to the `public/vendor/<your-package-name>` directory in the app where your package is
installed in.
### Working with migrations
The `PackageServiceProvider` assumes that any migrations are placed in this
directory: `<package root>/database/migrations`. Inside that directory you can put any migrations.
To register your migration, you should pass its name without the extension to the `hasMigration` table.
If your migration file is called `create_my_package_tables.php.stub` you can register them like this:
```php
$package
->name('your-package-name')
->hasMigration('create_my_package_tables');
```
Should your package contain multiple migration files, you can just call `hasMigration` multiple times or
use `hasMigrations`.
```php
$package
->name('your-package-name')
->hasMigrations(['my_package_tables', 'some_other_migration']);
```
Calling `hasMigration` will also make migrations publishable. Users of your package will be able to publish the
migrations with this command:
```bash
php artisan vendor:publish --tag=your-package-name-migrations
```
Like you might expect, published migration files will be prefixed with the current datetime.
You can also enable the migrations to be registered without needing the users of your package to publish them:
```php
$package
->name('your-package-name')
->hasMigrations(['my_package_tables', 'some_other_migration'])
->runsMigrations();
```
### Working with a publishable service provider
Some packages need an example service provider to be copied into the `app\Providers` directory of the Laravel app. Think
of for instance, the `laravel/horizon` package that copies an `HorizonServiceProvider` into your app with some sensible
defaults.
```php
$package
->name('your-package-name')
->publishesServiceProvider($nameOfYourServiceProvider);
```
The file that will be copied to the app should be stored in your package
in `/resources/stubs/{$nameOfYourServiceProvider}.php.stub`.
When your package is installed into an app, running this command...
```bash
php artisan vendor:publish --tag=your-package-name-provider
```
... will copy `/resources/stubs/{$nameOfYourServiceProvider}.php.stub` in your package
to `app/Providers/{$nameOfYourServiceProvider}.php` in the app of the user.
### Registering commands
You can register any command you package provides with the `hasCommand` function.
```php
$package
->name('your-package-name')
->hasCommand(YourCoolPackageCommand::class);
````
If your package provides multiple commands, you can either use `hasCommand` multiple times, or pass an array
to `hasCommands`
```php
$package
->name('your-package-name')
->hasCommands([
YourCoolPackageCommand::class,
YourOtherCoolPackageCommand::class,
]);
```
### Adding an installer command
Instead of letting your users manually publishing config files, migrations, and other files manually, you could opt to
add an install command that does all this work in one go. Packages like Laravel Horizon and Livewire provide such
commands.
When using Laravel Package Tools, you don't have to write an `InstallCommand` yourself. Instead, you can simply
call, `hasInstallCommand` and configure it using a closure. Here's an example.
```php
use Spatie\LaravelPackageTools\PackageServiceProvider;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\Commands\InstallCommand;
class YourPackageServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package
->name('your-package-name')
->hasConfigFile()
->hasMigration('create_package_tables')
->publishesServiceProvider('MyServiceProviderName')
->hasInstallCommand(function(InstallCommand $command) {
$command
->publishConfigFile()
->publishAssets()
->publishMigrations()
->askToRunMigrations()
->copyAndRegisterServiceProviderInApp()
->askToStarRepoOnGitHub('your-vendor/your-repo-name')
});
}
}
```
With this in place, the package user can call this command:
```bash
php artisan your-package-name:install
```
Using the code above, that command will:
- publish the config file
- publish the assets
- publish the migrations
- copy the `/resources/stubs/MyProviderName.php.stub` from your package to `app/Providers/MyServiceProviderName.php`, and also register that
provider in `config/app.php`
- ask if migrations should be run now
- prompt the user to open up `https://github.com/'your-vendor/your-repo-name'` in the browser in order to star it
You can also call `startWith` and `endWith` on the `InstallCommand`. They will respectively be executed at the start and
end when running `php artisan your-package-name:install`. You can use this to perform extra work or display extra
output.
```php
use Spatie\LaravelPackageTools\Commands\InstallCommand;
public function configurePackage(Package $package): void
{
$package
// ... configure package
->hasInstallCommand(function(InstallCommand $command) {
$command
->startWith(function(InstallCommand $command) {
$command->info('Hello, and welcome to my great new package!');
})
->publishConfigFile()
->publishAssets()
->publishMigrations()
->askToRunMigrations()
->copyAndRegisterServiceProviderInApp()
->askToStarRepoOnGitHub('your-vendor/your-repo-name')
->endWith(function(InstallCommand $command) {
$command->info('Have a great day!');
})
});
}
```
### Working with routes
The `PackageServiceProvider` assumes that any route files are placed in this directory: `<package root>/routes`. Inside
that directory you can put any route files.
To register your route, you should pass its name without the extension to the `hasRoute` method.
If your route file is called `web.php` you can register them like this:
```php
$package
->name('your-package-name')
->hasRoute('web');
```
Should your package contain multiple route files, you can just call `hasRoute` multiple times or use `hasRoutes`.
```php
$package
->name('your-package-name')
->hasRoutes(['web', 'admin']);
```
### Using lifecycle hooks
You can put any custom logic your package needs while starting up in one of these methods:
- `registeringPackage`: will be called at the start of the `register` method of `PackageServiceProvider`
- `packageRegistered`: will be called at the end of the `register` method of `PackageServiceProvider`
- `bootingPackage`: will be called at the start of the `boot` method of `PackageServiceProvider`
- `packageBooted`: will be called at the end of the `boot` method of `PackageServiceProvider`
## Testing
```bash
composer test
```
## Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.
## Contributing
Please see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.
## Security Vulnerabilities
Please review [our security policy](../../security/policy) on how to report security vulnerabilities.
## Credits
- [Freek Van der Herten](https://github.com/freekmurze)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

View File

@@ -0,0 +1,50 @@
{
"name": "spatie/laravel-package-tools",
"description": "Tools for creating Laravel packages",
"keywords": [
"spatie",
"laravel-package-tools"
],
"homepage": "https://github.com/spatie/laravel-package-tools",
"license": "MIT",
"authors": [
{
"name": "Freek Van der Herten",
"email": "freek@spatie.be",
"role": "Developer"
}
],
"require": {
"php": "^8.0",
"illuminate/contracts": "^9.28|^10.0|^11.0"
},
"require-dev": {
"mockery/mockery": "^1.5",
"orchestra/testbench": "^7.7|^8.0",
"pestphp/pest": "^1.22",
"phpunit/phpunit": "^9.5.24",
"spatie/pest-plugin-test-time": "^1.1"
},
"autoload": {
"psr-4": {
"Spatie\\LaravelPackageTools\\": "src"
}
},
"autoload-dev": {
"psr-4": {
"Spatie\\LaravelPackageTools\\Tests\\": "tests"
}
},
"scripts": {
"test": "vendor/bin/pest",
"test-coverage": "vendor/bin/pest --coverage"
},
"config": {
"sort-packages": true,
"allow-plugins": {
"pestphp/pest-plugin": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
}

View File

@@ -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;
}
}

View File

@@ -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")`');
}
}

View File

@@ -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;
}
}

View File

@@ -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;
}
}