### Example Installation Instructions in README Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/publishing.md Provide clear installation steps in your README, including Composer installation, configuration publishing, and migration commands. ```markdown ## Installation Install via Composer: ```bash composer require vendor/package-name ``` Publish configuration and assets: ```bash php artisan vendor:publish --provider="Vendor\Package\PackageServiceProvider" ``` Run migrations: ```bash php artisan migrate ``` ``` -------------------------------- ### Example Configuration File Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/views-and-assets.md This is an example of a configuration file that can be customized after being published. ```php return [ 'option' => 'custom_value', ]; ``` -------------------------------- ### Example Migration File Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/views-and-assets.md This is an example of a migration file that can be customized after being published. ```php public function up() { Schema::create('custom_table', function (Blueprint $table) { // Customize columns }); } ``` -------------------------------- ### Example View File Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/views-and-assets.md This is an example of a view file that can be customized after being published. ```blade ``` -------------------------------- ### Install and Publish Configuration Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/README.md Installs the package via Composer and publishes its configuration file using an Artisan command. ```bash composer require vendor/package-name php artisan vendor:publish --tag=skeleton-config php artisan migrate ``` -------------------------------- ### User Installation via Composer Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/publishing.md Instruct users to install your published package using the Composer require command. ```bash composer require vendor/package-name ``` -------------------------------- ### Basic Usage Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/README.md Demonstrates how to instantiate and use a basic method from the package. ```php $:variable = new VendorName\Skeleton(); echo $:variable->echoPhrase('Hello, VendorName!'); ``` -------------------------------- ### Install Dependencies for Development Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/quick-start.md Install project dependencies using Composer after cloning the repository. ```bash composer install ``` -------------------------------- ### Basic Facade Usage Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/facades.md Demonstrates how to use the Skeleton facade to call a method on the proxied service instance. ```php use VendorName\Skeleton\Facades\Skeleton; Skeleton::someMethod(); ``` -------------------------------- ### Published Configuration Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/README.md This is the content of the published configuration file. ```php return [ ]; ``` -------------------------------- ### Package Configuration File Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Defines a configuration file for a package, including default values and environment variable fallback. ```php // config/skeleton.php return [ 'option_one' => 'default_value', 'option_two' => env('SKELETON_OPTION_TWO', 'fallback'), ]; ``` -------------------------------- ### Composer.json Registration Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/facades.md Example of how the facade is registered via Laravel's package auto-discovery in composer.json. ```json "extra": { "laravel": { "aliases": { "Skeleton": "VendorName\\Skeleton\\Facades\\Skeleton" } } } ``` -------------------------------- ### Example Blade View File Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/views-and-assets.md Create Blade template files in the resources/views directory. This example shows a simple view with dynamic content. ```blade

{{ $title }}

{{ $content }}

``` -------------------------------- ### Service Provider Configuration Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/configuration.md Configure package assets like config files, views, migrations, and commands within the `configurePackage` method of your service provider. ```php public function configurePackage(Package $package): void { $package ->name('skeleton') ->hasConfigFile() ->hasViews() ->hasMigration('create_migration_table_name_table') ->hasCommand(SkeletonCommand::class); } ``` -------------------------------- ### Laravel Migration Indexing Examples Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/migration-and-models.md Provides examples of adding different types of indexes (regular, unique, full-text, spatial) to columns for query performance optimization. ```php $table->index('email'); // Regular index $table->unique('email'); // Unique index $table->fullText('description'); // Full-text index $table->spatialIndex('location'); // Spatial index ``` -------------------------------- ### Database Structure Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md Shows a common directory structure for database-related files, including migrations, factories, and seeders. ```tree database/ ├── migrations/ │ └── create_skeleton_table.php.stub ├── factories/ │ └── ModelFactory.php └── seeders/ # (optional) └── SkeletonSeeder.php ``` -------------------------------- ### Install Package via Composer Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/quick-start.md Install the package using Composer. Replace 'vendor/package' with the actual package name. ```bash composer require vendor/package ``` -------------------------------- ### Check Package Installation Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/publishing.md Verify that your package can be installed in a fresh Laravel project and that its service provider and commands are correctly registered. ```bash # In a fresh Laravel project composer require vendor/package-name # Verify service provider is auto-loaded php artisan package:discover # Verify command is available php artisan # Should list your custom command ``` -------------------------------- ### Unit Test Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md A basic unit test to verify the instantiation of a class. ```php // tests/Unit/SkeletonTest.php it('can instantiate skeleton', function () { $skeleton = new Skeleton(); expect($skeleton)->toBeInstanceOf(Skeleton::class); }); ``` -------------------------------- ### Facade Documentation Reference Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/facades.md Example of the @see PHPDoc comment used to link the facade to its underlying class. ```php /** * @see \VendorName\Skeleton\Skeleton */ class Skeleton extends Facade ``` -------------------------------- ### Views Structure Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md Illustrates a typical directory structure for view files, including layouts, components, pages, and email templates. ```tree resources/views/ ├── layouts/ # Layout templates ├── components/ # Reusable components ├── pages/ # Page templates └── emails/ # Email templates ``` -------------------------------- ### Example CHANGELOG.md Format Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/publishing.md Structure your changelog using the Keep a Changelog format, detailing added, changed, and fixed items for each version. ```markdown # Changelog ## [1.1.0] - 2024-06-04 ### Added - New feature description ### Changed - Changed behavior description ### Fixed - Bug fix description ## [1.0.0] - 2024-05-20 ### Added - Initial release - Package skeleton structure ``` -------------------------------- ### Dependency Injection Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/README.md Illustrates the preferred method of using dependency injection, specifically constructor injection, for accessing package services. This approach enhances testability compared to using facades. ```php class MyController { public function __construct(private Skeleton $skeleton) { $this->skeleton->methodName(); } } ``` -------------------------------- ### Testing Structure Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md Presents a standard directory structure for test files, categorizing them into Feature and Unit tests, and including configuration files. ```tree tests/ ├── Feature/ # Feature/integration tests ├── Unit/ # Unit tests ├── Pest.php # Pest configuration ├── TestCase.php # Base test class └── ArchTest.php # Architecture tests ``` -------------------------------- ### Unit Test Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md A basic unit test for a package method, asserting the return type. ```php it('can process data', function () { $skeleton = new Skeleton(); $result = $skeleton->process(['data']); expect($result)->toBeArray(); }); ``` -------------------------------- ### Example Unit Test for Skeleton Creation Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/testing.md A unit test demonstrating the creation of a 'Skeleton' object and verifying its instance type. This is a fundamental example of unit testing individual components. ```php it('can create a skeleton', function () { $skeleton = new Skeleton(); expect($skeleton)->toBeInstanceOf(Skeleton::class); }); ``` -------------------------------- ### Static Analysis Configuration (phpstan.neon.dist) Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md Example configuration for Larastan (PHPStan). Customize the analysis level, paths to scan, and files to exclude. ```neon parameters: level: 5 paths: - src excludePaths: - src/some-excluded-file.php ``` -------------------------------- ### Install Package via Composer Source: https://github.com/spatie/package-skeleton-laravel/blob/main/README.md Use this command to add the package to your Laravel project. ```bash composer require :vendor_slug/:package_slug ``` -------------------------------- ### Example: Create Users Table Migration Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/migration-and-models.md Demonstrates how to create a 'users' table with common fields like ID, name, email, password, and timestamps using Laravel's Schema builder. ```php return new class extends Migration { public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } }; ``` -------------------------------- ### Source Code Organization Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md Illustrates a typical directory structure for the source code of a Laravel package, including common subdirectories like Models, Commands, Http, and Services. ```tree src/ ├── Models/ # Eloquent models (optional) ├── Commands/ # Artisan commands ├── Events/ # Event classes (optional) ├── Exceptions/ # Custom exception classes ├── Facades/ # Facade classes ├── Http/ │ ├── Controllers/ # HTTP controllers (if package provides routes) │ ├── Middleware/ # HTTP middleware (optional) │ └── Requests/ # Form request classes (optional) ├── Listeners/ # Event listeners (optional) ├── Mail/ # Mailable classes (optional) ├── Notifications/ # Notification classes (optional) ├── Resources/ # API resource classes (optional) ├── Rules/ # Custom validation rules ├── Services/ # Service classes ├── Support/ # Helper functions and support classes ├── Traits/ # Reusable traits ├── Jobs/ # Queueable jobs (optional) ├── SkeletonServiceProvider.php └── Skeleton.php # Main service class ``` -------------------------------- ### Feature Test Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md A feature test to verify the successful execution of an artisan command. ```php // tests/Feature/CommandTest.php it('can run skeleton command', function () { $this->artisan('skeleton') ->assertSuccessful(); }); ``` -------------------------------- ### Feature Test Example for Artisan Command Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Tests the successful execution of a custom Artisan command provided by the package. ```php it('command succeeds', function () { $this->artisan('skeleton') ->assertSuccessful(); }); ``` -------------------------------- ### Configure Package Migration Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/database.md Register the package's migration using the `hasMigration` method in the `SkeletonServiceProvider`. This ensures the migration is published when the package is installed. ```php public function configurePackage(Package $package): void { $package ->name('skeleton') ->hasMigration('create_migration_table_name_table'); } ``` -------------------------------- ### Register View Composer Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/views-and-assets.md Bind data to specific views or a group of views using view composers. This example binds data to all views within the 'skeleton' namespace. ```php View::composer('skeleton::*', function ($view) { $view->with('sharedData', 'value'); }); ``` -------------------------------- ### Example Architecture Test for Directory Structure Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/testing.md An architecture test that validates that classes within the 'App\Models' namespace are indeed classes and are defined. This helps enforce code organization and structure. ```php arch('classes are in proper directories') ->expect('App\Models') ->toBeClasses() ->toBeDefined(); ``` -------------------------------- ### Example Pest Unit Test Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/testing.md A basic unit test using Pest's expressive syntax to validate a boolean condition. This serves as a starting point for writing unit tests. ```php toBeTrue(); }); ``` -------------------------------- ### Example Feature Test for Artisan Command Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/testing.md A feature test that verifies the successful execution of an Artisan command named 'skeleton'. This demonstrates testing application workflows and command-line interactions. ```php it('can run artisan command', function () { $this->artisan('skeleton') ->assertSuccessful(); }); ``` -------------------------------- ### Publish and Run Migrations Source: https://github.com/spatie/package-skeleton-laravel/blob/main/README.md Commands to publish package migrations and then run them to set up the database. ```bash php artisan vendor:publish --tag=":package_slug-migrations" php artisan migrate ``` -------------------------------- ### Access Skeleton via Facade Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/api-reference.md Shows how to use the package's facade for convenient static access to the Skeleton service methods. ```php use VendorName\Skeleton\Facades\Skeleton; // Use facade methods Skeleton::someMethod(); ``` -------------------------------- ### Prepare Package for Testing Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md Run the `prepare` script to discover packages within the testbench environment, which is necessary for testing purposes. ```bash composer run prepare ``` ```bash vendor/bin/testbench package:discover --ansi ``` -------------------------------- ### Dependency Injection vs. Service Locator vs. Facade Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Demonstrates preferred dependency injection pattern over service locator and facade for better testability. ```php namespace VendorName\Skeleton\Http\Controllers; use VendorName\Skeleton\Skeleton; use Illuminate\Support\Facades\Log; // Good: Dependency injection class MyController { public function __construct(private Skeleton $skeleton) { } public function index() { $this->skeleton->methodName(); } } // Acceptable: Service locator class MyController { public function index() { app(Skeleton::class)->methodName(); } } // Avoid: Facade in tests class MyController { public function index() { Skeleton::methodName(); // Hard to test } } ``` -------------------------------- ### Configuration Access Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Shows how to access package configuration values directly or via environment variables. ```php config('skeleton.key') // Direct access env('SKELETON_KEY') // Environment variable ``` -------------------------------- ### Architecture Test Example Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md An architecture test to ensure that specific debugging functions are not used in the codebase. ```php // tests/ArchTest.php arch('no debugging functions') ->expect(['dd', 'dump']) ->each->not->toBeUsed(); ``` -------------------------------- ### Publish Configuration File Source: https://github.com/spatie/package-skeleton-laravel/blob/main/README.md Command to publish the package's configuration file for customization. ```bash php artisan vendor:publish --tag=":package_slug-config" ``` -------------------------------- ### Facade Pattern Usage Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/README.md Demonstrates how to use the Facade pattern to access package functionality statically. This is useful for quick access but constructor injection is preferred for testability. ```php use VendorName\Skeleton\Facades\Skeleton; Skeleton::methodName(); // Static call ``` -------------------------------- ### Facade Usage in Service Classes Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/facades.md Illustrates the integration of the Skeleton facade within a custom service class. ```php namespace App\Services; use VendorName\Skeleton\Facades\Skeleton; class MyService { public function process() { Skeleton::someMethod(); } } ``` -------------------------------- ### Create and Edit Source File Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md Use vim to create and edit the main source file for your package feature. ```bash vim src/MyFeature.php ``` -------------------------------- ### Publish Package Views Source: https://github.com/spatie/package-skeleton-laravel/blob/main/README.md Command to publish the package's view files. ```bash php artisan vendor:publish --tag=":package_slug-views" ``` -------------------------------- ### Run Tests Source: https://github.com/spatie/package-skeleton-laravel/blob/main/README.md Command to execute the package's test suite. ```bash composer test ``` -------------------------------- ### Create and Edit Test File Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md Use vim to create and edit the corresponding test file for your package feature. ```bash vim tests/Unit/MyFeatureTest.php ``` -------------------------------- ### Mocking Facades in Tests Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/facades.md Illustrates how to mock the Skeleton facade for testing purposes using shouldReceive. ```php use VendorName\Skeleton\Facades\Skeleton as SkeletonFacade; public function test_something() { SkeletonFacade::shouldReceive('someMethod')->andReturn('value'); // Test code that uses Skeleton::someMethod() } ``` -------------------------------- ### Run Database Seeders Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/migration-and-models.md Command to execute the database seeders to populate the database. ```bash php artisan db:seed ``` -------------------------------- ### Using the Facade Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/service-provider.md Access package methods directly using the facade after it's registered by Laravel. ```php use VendorName\Skeleton\Facades\Skeleton; Skeleton::methodName(); ``` -------------------------------- ### Skeleton Artisan Command Invocation Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/commands.md How to execute the main package command from the terminal. ```bash php artisan skeleton ``` -------------------------------- ### Run Package Configuration Script Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md Execute the `configure.php` script to replace placeholder tokens throughout the package. This script updates `composer.json`, `README.md`, `LICENSE.md`, source code namespaces, and configuration file paths. ```bash php ./configure.php ``` -------------------------------- ### Injecting Configuration into a Service Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Demonstrates injecting configuration array into a service class constructor. ```php namespace VendorName\Skeleton; class Skeleton { public function __construct(private array $config) { } public function process() { $option = $this->config['option_one'] ?? 'default'; } } ``` -------------------------------- ### Publish Views Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/service-provider.md Command to publish the package's view templates to the application's resources/views directory. ```bash php artisan vendor:publish --tag=skeleton-views ``` -------------------------------- ### Database Seeder Implementation Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/migration-and-models.md Create a seeder class to populate the database with initial data using model factories. ```php namespace VendorName\Skeleton\Database\Seeders; use Illuminate\Database\Seeder; use VendorName\Skeleton\Models\User; class DatabaseSeeder extends Seeder { public function run() { User::factory(100)->create(); } } ``` -------------------------------- ### Add Service Class and Register in Provider Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/extending.md Create dedicated service classes for specific functionalities and register them in the package's service provider using dependency injection. This promotes modularity and testability. ```php namespace VendorName\Skeleton\Services; use VendorName\Skeleton\Models\Item; class ItemService { public function create(array $data): Item { return Item::create($data); } public function update(Item $item, array $data): Item { $item->update($data); return $item; } public function delete(Item $item): void { $item->delete(); } public function getActive() { return Item::active()->get(); } } ``` ```php use VendorName\Skeleton\Services\ItemService; public function register() { $this->app->singleton(ItemService::class); } ``` ```php use VendorName\Skeleton\Services\ItemService; class MyController { public function __construct(private ItemService $itemService) { } public function store(Request $request) { $item = $this->itemService->create($request->validated()); return response()->json($item); } } ``` -------------------------------- ### Catching Specific and General Exceptions Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Shows how to catch specific custom exceptions and then more general package exceptions. ```php use VendorName\Skeleton\Exceptions\InvalidConfigurationException; use VendorName\Skeleton\Exceptions\SkeletonException; use Illuminate\Support\Facades\Log; try { $skeleton->process($data); } catch (InvalidConfigurationException $e) { // Handle specific exception Log::error('Configuration invalid', ['error' => $e->getMessage()]); } catch (SkeletonException $e) { // Handle all package exceptions throw $e; } ``` -------------------------------- ### Create a User with Factory Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/migration-and-models.md Use the model factory to create a new user record in the database and assert its properties. ```php // tests/Feature/UserTest.php use VendorName\Skeleton\Models\User; it('can create a user', function () { $user = User::factory()->create([ 'name' => 'John Doe' ]); expect($user->name)->toBe('John Doe'); expect($user->exists)->toBeTrue(); }); ``` -------------------------------- ### Dependency Injection of Skeleton Service Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/facades.md Demonstrates accessing the Skeleton service via constructor injection in a controller. ```php namespace App\Http\Controllers; use VendorName\Skeleton\Skeleton; class MyController { public function __construct(private Skeleton $skeleton) { } public function index() { $this->skeleton->someMethod(); } } ``` -------------------------------- ### Run Quality Checks with Composer Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/publishing.md Execute tests, check code coverage, perform static analysis, and format code using Composer scripts before releasing your package. ```bash # Run tests composer test # Check code coverage composer test-coverage # Run static analysis composer analyse # Format code composer format ``` -------------------------------- ### Create Git Tag and Push for Release Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/publishing.md Commit your changes, create a git tag for the new version, and push both the main branch and the tag to your remote repository. ```bash # Commit your changes git add . git commit -m "Release version 1.0.0" # Create a tag git tag v1.0.0 # Push to GitHub git push origin main git push origin v1.0.0 ``` -------------------------------- ### Using the Service Container Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/service-provider.md Retrieve and use package methods via the service container when direct facade access is not preferred. ```php use VendorName\Skeleton\Skeleton; app(Skeleton::class)->methodName(); ``` -------------------------------- ### Project Structure Overview Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/README.md Displays the directory structure of the Laravel Package Skeleton, outlining the location of source code, configuration, database files, views, tests, and package definition. ```bash package-skeleton-laravel/ ├── src/ # Source code │ ├── Commands/ │ │ └── SkeletonCommand.php # Artisan command │ ├── Facades/ │ │ └── Skeleton.php # Static facade │ ├── Skeleton.php # Main service class │ └── SkeletonServiceProvider.php # Service provider ├── config/ │ └── skeleton.php # Configuration template ├── database/ │ ├── migrations/ │ │ └── create_skeleton_table.php.stub │ └── factories/ │ └── ModelFactory.php ├── resources/views/ # Blade templates ├── tests/ # Test suite │ ├── ExampleTest.php │ ├── ArchTest.php │ ├── Pest.php │ └── TestCase.php ├── composer.json # Package definition ├── phpstan.neon.dist # Static analysis config ├── phpunit.xml.dist # Test configuration └── configure.php # Setup script ``` -------------------------------- ### Add Eloquent Model and Migration Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/extending.md Create Eloquent models within the package's Models directory and define their corresponding database migrations. Ensure the model's table name and fillable attributes are correctly set. ```php namespace VendorName\Skeleton\Models; use Illuminate\Database\Eloquent\Model; class Item extends Model { protected $table = 'skeleton_items'; protected $fillable = ['name', 'description']; public function scopeActive($query) { return $query->where('active', true); } } ``` ```php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up() { Schema::create('skeleton_items', function (Blueprint $table) { $table->id(); $table->string('name'); $table->text('description')->nullable(); $table->boolean('active')->default(true); $table->timestamps(); }); } }; ``` -------------------------------- ### Stage and Commit Changes Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md Stage all changes in the src/ and tests/ directories and commit them with a descriptive message. ```bash git add src/ tests/ git commit -m "Add feature description" ``` -------------------------------- ### Running Package Tests and Analysis Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/README.md Provides commands for executing the package's test suite, generating coverage reports, performing static analysis, and formatting code. ```bash composer test # Run all tests composer test-coverage # With coverage report composer analyse # Static analysis composer format # Format code ``` -------------------------------- ### Run Tests with Coverage Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/quick-start.md Execute the package's test suite and generate a code coverage report. ```bash composer test-coverage ``` -------------------------------- ### Use Package Service Container Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/quick-start.md Retrieve and use the package's main service class via the application's service container. ```php use VendorName\Skeleton\Skeleton; $service = app(Skeleton::class); $service->methodName(); ``` -------------------------------- ### Facade Usage in Controllers Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/facades.md Shows how to utilize the Skeleton facade within a Laravel controller class. ```php namespace App\Http\Controllers; use VendorName\Skeleton\Facades\Skeleton; class MyController { public function index() { Skeleton::someMethod(); } } ``` -------------------------------- ### Skeleton Command Registration in Service Provider Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/commands.md Shows how the 'skeleton' command is registered within the package's service provider using the 'hasCommand' method. ```php public function configurePackage(Package $package): void { $package ->name('skeleton') ->hasCommand(SkeletonCommand::class); } ``` -------------------------------- ### Laravel Eloquent Factory Usage Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/migration-and-models.md Demonstrates how to create single or multiple model instances using Eloquent factories, including creating records with specific attributes. ```php // Create a single user $user = User::factory()->create(); // Create multiple users $users = User::factory(10)->create(); // Create with specific attributes $user = User::factory()->create([ 'name' => 'John Doe' ]); ``` -------------------------------- ### Accessing Package Configuration Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/configuration.md Retrieve configuration values using the `config()` helper function. Access the entire configuration array or specific keys. ```php $config = config('skeleton'); ``` ```php $value = config('skeleton.key_name'); ``` -------------------------------- ### Format Code Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/quick-start.md Format the package's code according to project standards using Composer. ```bash composer format ``` -------------------------------- ### Run All Tests Command Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/testing.md Executes all tests in the project using the Composer script 'test'. This is a convenient shortcut for running the Pest test suite. ```bash composer test ``` -------------------------------- ### Accessing Configuration Values Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/views-and-assets.md Retrieve configuration values from the published configuration file using the `config()` helper function. Provide a default value if the key does not exist. ```php $config = config('skeleton'); $value = config('skeleton.key_name', 'default'); ``` -------------------------------- ### Caching Configuration Files Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Command to cache configuration files in production for improved performance. ```bash php artisan config:cache ``` -------------------------------- ### Register Package Routes Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/extending.md Register routes for your package's HTTP endpoints within the service provider's boot method. Use a group to define a common prefix. ```php public function boot() { $this->registerRoutes(); } protected function registerRoutes() { Route::group(['prefix' => 'api/skeleton'], function () { Route::get('items', 'ItemController@index'); Route::post('items', 'ItemController@store'); }); } ``` -------------------------------- ### Add Configuration Options to Package Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/extending.md Extend the package's configuration file with custom options, often using environment variables for flexibility. Access these options within your services using the config helper. ```php // config/skeleton.php return [ 'enabled' => env('SKELETON_ENABLED', true), 'features' => [ 'feature_one' => env('SKELETON_FEATURE_ONE', true), 'feature_two' => env('SKELETON_FEATURE_TWO', false), ], 'database' => [ 'prefix' => env('SKELETON_DB_PREFIX', 'skeleton_'), ], 'cache' => [ 'ttl' => env('SKELETON_CACHE_TTL', 3600), ], ]; ``` ```php use Illuminate\Config\Repository as Config; class Skeleton { public function __construct(private Config $config) { } public function process() { if (!config('skeleton.enabled')) { return; } $ttl = config('skeleton.cache.ttl'); } } ``` -------------------------------- ### Migration Stub for Table Creation Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/database.md This is the default migration stub file used to create a database table. Developers should customize the table name and add specific columns as needed. ```php id(); // add fields $table->timestamps(); }); } }; ``` -------------------------------- ### Create Multiple Users with Factory Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/migration-and-models.md Generate multiple user records using the factory and verify the total count. ```php it('can query users', function () { User::factory(5)->create(); expect(User::count())->toBe(5); }); ``` -------------------------------- ### Service Provider Registration Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md The SkeletonServiceProvider extends PackageServiceProvider to configure package details like name, config files, views, migrations, and commands. ```php class SkeletonServiceProvider extends PackageServiceProvider { public function configurePackage(Package $package): void { $package ->name('skeleton') ->hasConfigFile() ->hasViews() ->hasMigration('create_migration_table_name_table') ->hasCommand(SkeletonCommand::class); } } ``` -------------------------------- ### Access Skeleton via Service Container Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/api-reference.md Demonstrates how to retrieve the Skeleton service instance from the Laravel service container using dependency injection. ```php use VendorName\Skeleton\Skeleton; $skeleton = app(Skeleton::class); ``` -------------------------------- ### Skeleton Service Provider Class Definition Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/service-provider.md Defines the SkeletonServiceProvider, extending Spatie's PackageServiceProvider to configure package assets and commands. ```php namespace VendorName\Skeleton; use Spatie\LaravelPackageTools\Package; use Spatie\LaravelPackageTools\PackageServiceProvider; use VendorName\Skeleton\Commands\SkeletonCommand; class SkeletonServiceProvider extends PackageServiceProvider { public function configurePackage(Package $package): void { $package ->name('skeleton') ->hasConfigFile() ->hasViews() ->hasMigration('create_migration_table_name_table') ->hasCommand(SkeletonCommand::class); } } ``` -------------------------------- ### Default Package Configuration File Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/configuration.md The default configuration file for the package, located at `config/skeleton.php`. It returns an empty array, serving as a template for custom options. ```php group(function () { // Protected routes }); } ``` -------------------------------- ### Use Package via Dependency Injection Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/quick-start.md Inject the package's main service class into a controller constructor. ```php namespace App\Http\Controllers; use VendorName\Skeleton\Skeleton; class MyController { public function __construct(private Skeleton $skeleton) { } public function index() { $this->skeleton->methodName(); } } ``` -------------------------------- ### Directory Structure of Laravel Package Skeleton Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/package-manifest.md Illustrates the standard directory layout for a Laravel package created with the skeleton. It includes directories for source code, configuration, database files, resources, and tests. ```text package-skeleton-laravel/ ├── src/ # Source code │ ├── Skeleton.php # Main service class │ ├── SkeletonServiceProvider.php # Service provider │ ├── Commands/ │ │ └── SkeletonCommand.php # Artisan command │ └── Facades/ │ └── Skeleton.php # Static facade ├── config/ │ └── skeleton.php # Configuration template ├── database/ │ ├── migrations/ │ │ └── create_skeleton_table.php.stub # Migration stub │ └── factories/ │ └── ModelFactory.php # Model factory template ├── resources/ │ └── views/ # View files directory ├── tests/ # Test files │ ├── ExampleTest.php # Example test │ ├── ArchTest.php # Architecture tests │ ├── Pest.php # Pest configuration │ └── TestCase.php # Test base class ├── composer.json # Package definition ├── phpstan.neon.dist # Static analysis config ├── phpunit.xml.dist # Test configuration └── configure.php # Setup script ``` -------------------------------- ### Accessing Skeleton Service via Service Container or Facade Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/service-provider.md Demonstrates how to retrieve the registered Skeleton service from Laravel's service container or by using its facade. ```php // Access via service container $skeleton = app(Skeleton::class); // Access via facade Skeleton::methodName(); ``` -------------------------------- ### Git Ignore Configuration Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md A sample .gitignore file for common Laravel and PHP project files to exclude them from version control. ```gitignore /vendor /node_modules /.env /.env.local /storage/logs /bootstrap/cache .phpunit.result.cache ``` -------------------------------- ### Register Model Observer Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/extending.md Register your model observers in the service provider's boot method to activate them. Use the Model::observe() method. ```php public function boot() { Item::observe(ItemObserver::class); } ``` -------------------------------- ### Reset and Reseed Database Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/migration-and-models.md Command to drop all tables, re-run migrations, and execute seeders to reset the database to a fresh state. ```bash # Drop all tables and re-run migrations php artisan migrate:fresh # Same but also run seeders php artisan migrate:fresh --seed ``` -------------------------------- ### Run Static Analysis Command Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/testing.md Executes static analysis on the codebase using Larastan via the Composer script 'analyse'. This helps catch type errors and potential bugs before runtime. ```bash composer analyse ``` -------------------------------- ### Publish Package Configuration Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/configuration.md Use this command to publish the package's configuration file to your Laravel application. This allows for easy customization of package settings. ```bash php artisan vendor:publish --tag=skeleton-config ``` -------------------------------- ### Force Publish Assets Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/views-and-assets.md Use the `vendor:publish` Artisan command with the `--force` flag to re-publish assets, overwriting existing files. ```bash php artisan vendor:publish --tag=skeleton-views --force ``` ```bash php artisan vendor:publish --tag=skeleton-config --force ``` -------------------------------- ### Throwing Custom Exceptions Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Illustrates how to throw custom exceptions when specific error conditions are met. ```php namespace VendorName\Skeleton; use VendorName\Skeleton\Exceptions\InvalidConfigurationException; class Skeleton { public function process($data) { if (!$this->isValid($data)) { throw new InvalidConfigurationException('Invalid data provided'); } } } ``` -------------------------------- ### Submit Package to Packagist via Command Line Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/publishing.md Use the Packagist API with a curl command to submit your package by providing the GitHub repository URL. ```bash curl -F "repository_url=https://github.com/vendor/package-name" \ https://packagist.org/packages/submit ``` -------------------------------- ### Configure Auto-Discovery in composer.json Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/publishing.md Enable automatic discovery of your package's service provider and facade by including them in the 'extra.laravel' section of composer.json. ```json "extra": { "laravel": { "providers": [ "Vendor\Package\PackageServiceProvider" ], "aliases": { "Package": "Vendor\Package\Facades\Package" } } } ``` -------------------------------- ### Migration Status Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/migration-and-models.md Command to display the status of all database migrations. ```bash php artisan migrate:status ``` -------------------------------- ### Registering Package Views Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/views-and-assets.md Register views from the package's resources/views directory as publishable assets. This method also sets the publish tag and the destination for published views. ```php public function configurePackage(Package $package): void { $package ->name('skeleton') ->hasViews(); } ``` -------------------------------- ### Service Container Access Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Demonstrates accessing the Skeleton service via the application container or its facade after the service provider has registered it. ```php // Laravel auto-discovery loads the service provider app(SkeletonServiceProvider::class); // The provider registers the Skeleton service $skeleton = app(Skeleton::class); // Or access via facade Skeleton::methodName(); ``` -------------------------------- ### Run Tests with Coverage Report Command Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/testing.md Executes all tests and generates a code coverage report using the Composer script 'test-coverage'. This helps identify areas of the codebase not covered by tests. ```bash composer test-coverage ``` -------------------------------- ### Database Query Optimization with Eager Loading Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Compares inefficient N+1 query pattern with efficient eager loading using Eloquent. ```php // Avoid N+1 queries $users = User::all(); foreach ($users as $user) { echo $user->posts->count(); // N+1 query } // Use eager loading $users = User::with('posts')->all(); foreach ($users as $user) { echo $user->posts->count(); // No additional queries } ``` -------------------------------- ### Caching Service Container and Routes Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Commands to cache the service container bindings and application routes for performance optimization. ```bash php artisan config:cache php artisan route:cache php artisan package:discover --force ``` -------------------------------- ### Direct Service Container Access Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/facades.md Shows how to access the underlying Skeleton service directly from the service container. ```php use VendorName\Skeleton\Skeleton; $skeleton = app(Skeleton::class); $skeleton->someMethod(); ``` -------------------------------- ### Use Package Views in Controllers Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/quick-start.md Render package views from within your controllers, passing data. ```php return view('package::view-name', [ 'data' => 'value' ]); ``` -------------------------------- ### Validate Composer Configuration Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/publishing.md Check your composer.json file for syntax errors before publishing your package. ```bash composer validate ``` -------------------------------- ### Run All Tests Directly Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/testing.md Executes all tests in the project directly using the Pest executable. This command is equivalent to 'composer test'. ```bash vendor/bin/pest ``` -------------------------------- ### Add Controller for HTTP Endpoints Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/extending.md Define a controller to handle HTTP requests for package-specific endpoints. Ensure it uses the correct namespace and injects necessary services. ```php namespace VendorName\Skeleton\Http\Controllers; use Illuminate\Http\JsonResponse; use VendorName\Skeleton\Services\ItemService; class ItemController { public function __construct(private ItemService $itemService) { } public function index(): JsonResponse { $items = $this->itemService->getActive(); return response()->json($items); } public function store(Request $request): JsonResponse { $item = $this->itemService->create($request->validated()); return response()->json($item, 201); } } ``` -------------------------------- ### Run Laravel Migrations Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/database.md Execute all published database migrations in your Laravel application using the Artisan command. ```bash php artisan migrate ``` -------------------------------- ### Facade Definition Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md The Skeleton facade provides a static interface to the package's service class, enabling convenient access and testability. ```php class Skeleton extends Facade { protected static function getFacadeAccessor(): string { return \VendorName\Skeleton\Skeleton::class; } } ``` -------------------------------- ### Code Style Configuration (pint.json) Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/developer-setup.md Optional configuration for Laravel Pint, allowing customization of code style presets and specific rules. ```json { "preset": "laravel", "rules": { "custom_rule": true } } ``` -------------------------------- ### Update Package Metadata in composer.json Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/publishing.md Configure essential package details like name, description, keywords, repository URL, license, author information, and dependencies. ```json { "name": "vendor/package-name", "description": "Clear description of what the package does", "keywords": ["laravel", "package", "relevant-keyword"], "homepage": "https://github.com/vendor/package-name", "license": "MIT", "authors": [ { "name": "Your Name", "email": "your.email@example.com", "role": "Developer" } ], "require": { "php": "^8.4", "spatie/laravel-package-tools": "^1.16", "illuminate/contracts": "^11.0||^12.0||^13.0" } } ``` -------------------------------- ### Format Code Style Command Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/testing.md Formats the entire codebase according to Laravel and PSR standards using Laravel Pint via the Composer script 'format'. This ensures consistent code style across the project. ```bash composer format ``` -------------------------------- ### Skeleton Artisan Command Output Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/commands.md The expected output when the 'skeleton' Artisan command is executed. ```text All done ``` -------------------------------- ### Add Artisan Command to Package Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/extending.md Create and register a new Artisan command for your package. Ensure the command is registered in the service provider to be available in the console. ```php namespace VendorName\Skeleton\Commands; use Illuminate\Console\Command; class ProcessCommand extends Command { public $signature = 'skeleton:process {file}'; public $description = 'Process a file'; public function handle(): int { $file = $this->argument('file'); $this->info("Processing {$file}..."); // Implementation $this->comment('Processing complete'); return self::SUCCESS; } } ``` ```php use Spatie\PackageTools\Package; use VendorName\Skeleton\Commands\SkeletonCommand; use VendorName\Skeleton\Commands\ProcessCommand; public function configurePackage(Package $package): void { $package ->name('skeleton') ->hasCommand(SkeletonCommand::class) ->hasCommand(ProcessCommand::class); // Add new command } ``` -------------------------------- ### Environment-Specific Configuration Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/configuration.md Override package configuration values per environment by defining them in your `.env` file and referencing them in the published `config/skeleton.php`. ```php // config/skeleton.php return [ 'option_name' => env('SKELETON_OPTION_NAME', 'default_value'), ]; ``` -------------------------------- ### Write Feature Tests for Item Creation Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/extending.md Use these tests to verify that items can be created via the API and that the correct HTTP status code is returned. Ensure the item count is updated correctly. ```php // tests/Feature/ItemTest.php it('can create an item', function () { $response = $this->post('/api/skeleton/items', [ 'name' => 'Test Item', ]); $response->assertStatus(201); expect(Item::count())->toBe(1); }); it('emits event when item created', function () { Event::fake(); $this->post('/api/skeleton/items', [ 'name' => 'Test Item', ]); Event::assertDispatched(ItemCreated::class); }); ``` -------------------------------- ### Add Index Migration Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/migration-and-models.md Use the Schema facade to add an index to a column in a database table. ```php // Add index public function up() { Schema::table('users', function (Blueprint $table) { $table->index('email'); }); } ``` -------------------------------- ### SkeletonCommand Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/api-reference.md The `SkeletonCommand` is an Artisan console command provided by the package. It can be invoked from the command line using `php artisan skeleton` and has a description 'My command'. The `handle` method executes the command's logic and returns an integer exit status code. ```APIDOC ## SkeletonCommand ### Description Artisan console command for the package. ### Class Definition ```php namespace VendorName Skeleton\Commands; use Illuminate\Console\Command; class SkeletonCommand extends Command { public $signature = 'skeleton'; public $description = 'My command'; public function handle(): int } ``` ### Properties | Property | Type | Value | Description | |----------|------|-------|-------------| | `signature` | string | `'skeleton'` | The command name as invoked from the console | | `description` | string | `'My command'` | The help text displayed for this command | ### Methods #### `handle(): int` Executes the command logic when invoked. **Returns**: `int` — The exit status code. Returns `Command::SUCCESS` (0) on successful completion. **Example Usage**: ```bash php artisan skeleton ``` Output: ``` All done ``` The command outputs a comment message "All done" and returns `Command::SUCCESS` (0) to indicate successful execution. ``` -------------------------------- ### Skeleton Artisan Command Signature Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/commands.md Defines the signature for the main package command. ```bash skeleton ``` -------------------------------- ### Artisan Command Implementation Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/architecture.md Custom artisan commands like SkeletonCommand extend the base Command class to provide CLI functionality, including signature, description, and handler logic. ```php class SkeletonCommand extends Command { public $signature = 'skeleton'; public $description = 'My command'; public function handle(): int { $this->comment('All done'); return self::SUCCESS; } } ``` -------------------------------- ### Create Reusable Blade Component Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/extending.md Define reusable UI elements as Blade components. Place them in the resources/views/components directory with a relevant namespace. ```blade
{{ $item->name }}

{{ $item->description }}

``` -------------------------------- ### Run Tests with Coverage Report Directly Source: https://github.com/spatie/package-skeleton-laravel/blob/main/_autodocs/testing.md Executes all tests and generates a code coverage report directly using the Pest executable with the --coverage flag. This command is equivalent to 'composer test-coverage'. ```bash vendor/bin/pest --coverage ```