├── .github
└── workflows
│ └── quality.yml
├── LICENSE
├── README.md
├── composer.json
├── config
└── dev-booter.php
├── phpunit.xml.dist
├── pint.json
└── src
└── ServiceProvider.php
/.github/workflows/quality.yml:
--------------------------------------------------------------------------------
1 | name: Quality
2 |
3 | on:
4 | pull_request:
5 |
6 | jobs:
7 | tests:
8 | runs-on: ubuntu-latest
9 |
10 | strategy:
11 | fail-fast: true
12 | matrix:
13 | php: [ 8.2, 8.3 ]
14 |
15 | name: Testing on PHP ${{ matrix.php }}
16 |
17 | steps:
18 | - name: Checkout Code
19 | uses: actions/checkout@v4
20 |
21 | - name: Setup PHP
22 | uses: shivammathur/setup-php@v2
23 | with:
24 | php-version: ${{ matrix.php }}
25 | extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite
26 | coverage: none
27 | tools: composer:v2
28 |
29 | - name: Install Composer dependencies
30 | run: composer install --prefer-dist --no-interaction --no-progress
31 |
32 | - name: Checks Code styles via PHPCsFixer
33 | run: composer test:lint
34 |
35 | - name: Execute tests
36 | run: vendor/bin/phpunit
37 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2017 Percy Mamedy
4 |
5 | Permission is hereby granted, free of charge, to any person obtaining a copy
6 | of this software and associated documentation files (the "Software"), to deal
7 | in the Software without restriction, including without limitation the rights
8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 | copies of the Software, and to permit persons to whom the Software is
10 | furnished to do so, subject to the following conditions:
11 |
12 | The above copyright notice and this permission notice shall be included in all
13 | copies or substantial portions of the Software.
14 |
15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 | SOFTWARE.
22 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
2 | Laravel dev booter
3 |
4 |
5 |
6 |
7 |
8 |
9 |
10 |
11 |
12 | ## Introduction
13 | During development of a Laravel App; some Service Providers are very helpful. These services helps us with debugging and coding.
14 | But registering these providers on production is usually not a good idea. They can slow down our App or even expose sensitive information.
15 |
16 | Laravel Dev Booter helps end this problem. The package consists of a single ```Service Provider```.
17 | This provider will exclude unwanted providers from your production environment.
18 |
19 | ## License
20 | Laravel Dev Booter is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)
21 |
22 | ## Version Compatibility
23 |
24 | Laravel | DevBooter
25 | :---------|:----------
26 | 5.x | 0.2.x
27 | 6.x | 1.0.x
28 | 7.x | 2.0.x
29 | 8.x | 3.0.x
30 | 9.x | 3.0.x
31 | 10.x | 4.0.x
32 | 11.x | 5.0.x
33 |
34 | ### Installation
35 | Install Laravel Dev Booter as you would with any other dependency managed by Composer:
36 |
37 | ```bash
38 | $ composer require percymamedy/laravel-dev-booter
39 | ```
40 |
41 | ### Configuration
42 | > If you are using Laravel >= 5.5, you can skip service registration
43 | > and aliases registration thanks to Laravel auto package discovery
44 | > feature.
45 |
46 | After installing Laravel Dev Booter all you need is to register the ```PercyMamedy\LaravelDevBooter\ServiceProvider```
47 | in your `config/app.php` configuration file:
48 |
49 | ```php
50 | 'providers' => [
51 | // Other service providers...
52 |
53 | PercyMamedy\LaravelDevBooter\ServiceProvider::class,
54 | ],
55 | ```
56 |
57 | ### Usage
58 | First use the ```vendor:publish``` command to copy the configuration file to your application:
59 |
60 | ```bash
61 | $ php artisan vendor:publish --provider="PercyMamedy\LaravelDevBooter\ServiceProvider" --tag="config"
62 | ```
63 |
64 | This will create the file ```config/dev-booter.php```.
65 |
66 | ### Defining your development environments
67 | In the file ```config/dev-booter.php``` you will find the ```dev_environments``` section. Use this section
68 | to define your app's development environments:
69 |
70 | ```php
71 | 'dev_environments' => [
72 | 'local',
73 | 'dev',
74 | 'testing'
75 | ],
76 | ```
77 |
78 | These are the only environments where Dev Booter will act; and try to register any Service providers.
79 |
80 | ### Development Service providers locations
81 | The next section in the ```config/dev-booter.php``` file is the ```dev_providers_config_keys```. The array contains an entry
82 | for each of your development environments specified above. Feel free to add and edit this section.
83 |
84 | ```php
85 | 'dev_providers_config_keys' => [
86 | 'dev' => ['app.dev_providers', 'app.local_providers'],
87 | 'local' => 'app.local_providers',
88 | 'testing' => 'app.testing_providers',
89 | ]
90 | ```
91 |
92 | The value for each of these entries can be an array or a string. The values represents the locations of a Service Provider array
93 | for each enviroment. You can even specify many locations for an environment, Dev Booter will take care of loading providers in
94 | each of these locations.
95 |
96 | You may now define these Service Providers array in the ```config/app.php``` file as you would for any regular Service Provider:
97 |
98 | ```php
99 | 'dev_providers' => [
100 | Foo\Bar\ServiceProvider::class,
101 | ],
102 |
103 | 'local_providers' => [
104 | Bar\Baz\ServiceProvider::class,
105 | ],
106 |
107 | 'testing_providers' => [
108 | Foo\Baz\ServiceProvider::class,
109 | ],
110 | ```
111 |
112 | ### Development Aliases locations
113 | The last section of the ```config/dev-booter.php``` file is the ```dev_aliases_config_keys```. As with Service Providers, it works
114 | on the same principle.
115 |
116 | You first specify the location of your aliases per environment:
117 |
118 | ```php
119 | 'dev_aliases_config_keys' => [
120 | 'dev' => ['app.dev_aliases', 'app.local_aliases'],
121 | 'local' => 'app.local_aliases',
122 | 'testing' => 'app.testing_aliases',
123 | ]
124 | ```
125 |
126 | Then define these aliases array in the ```config/app.php``` file:
127 |
128 | ```php
129 | 'dev_aliases' => [
130 | 'Foo' => Foo\Bar\Facade::class,
131 | ],
132 |
133 | 'local_aliases' => [
134 | 'Bar' => Bar\Baz\Facade::class,
135 | ],
136 |
137 | 'testing_aliases' => [
138 | 'Baz' => Foo\Baz\Facade::class,
139 | ],
140 | ```
141 |
142 | ### Going Further
143 | In the above examples, providers and aliases array were define in ```config/app.php```, but this does not have to be the case. You
144 | can define these array in any config files provided by Laravel or even create your own. Simply make sure you adjust your
145 | ```dev_providers_config_keys``` and ```dev_aliases_config_keys```.
146 |
147 | For example:
148 |
149 | ```php
150 | 'dev_providers_config_keys' => [
151 | 'dev' => ['app_dev.dev_providers', 'app_dev.local_providers'],
152 | // ...
153 | ]
154 | ```
155 |
156 | Then in ```config/app_dev.php```
157 |
158 | ```php
159 | 'dev_providers' => [
160 | Foo\Bar\ServiceProvider::class,
161 | ],
162 |
163 | 'local_providers' => [
164 | Bar\Baz\ServiceProvider::class,
165 | ],
166 | ```
167 |
168 | ### Credits
169 | Big Thanks to all developers who worked hard to create something amazing!
170 |
171 | ### Creator
172 | [](https://twitter.com/PercyMamedy)
173 |
174 | Twitter: [@PercyMamedy](https://twitter.com/PercyMamedy)
175 |
176 | GitHub: [percymamedy](https://github.com/percymamedy)
177 |
178 |
179 |
180 |
181 |
182 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "percymamedy/laravel-dev-booter",
3 | "description": "Boost your Laravel app by registering Prod services only on Prod.",
4 | "keywords": [
5 | "php",
6 | "Laravel",
7 | "dev-tools"
8 | ],
9 | "license": "MIT",
10 | "authors": [
11 | {
12 | "name": "Percy Mamedy",
13 | "email": "percymamedy@gmail.com"
14 | }
15 | ],
16 | "require": {
17 | "php": "^8.2",
18 | "illuminate/support": "^11.0"
19 | },
20 | "require-dev": {
21 | "orchestra/testbench": "^9.0",
22 | "phpunit/phpunit": "^10.0",
23 | "laravel/pint": "^1.16"
24 | },
25 | "autoload": {
26 | "psr-4": {
27 | "PercyMamedy\\LaravelDevBooter\\": "src/"
28 | }
29 | },
30 | "autoload-dev": {
31 | "psr-4": {
32 | "PercyMamedy\\LaravelDevBooter\\Tests\\": "tests/"
33 | }
34 | },
35 | "scripts": {
36 | "lint": "vendor/bin/pint -v",
37 | "test:lint": "vendor/bin/pint --test"
38 | },
39 | "extra": {
40 | "branch-alias": {
41 | "dev-main": "5.x-dev"
42 | },
43 | "laravel": {
44 | "providers": [
45 | "PercyMamedy\\LaravelDevBooter\\ServiceProvider"
46 | ]
47 | }
48 | },
49 | "minimum-stability": "dev",
50 | "prefer-stable": true
51 | }
52 |
--------------------------------------------------------------------------------
/config/dev-booter.php:
--------------------------------------------------------------------------------
1 | [
19 | 'local',
20 | 'dev',
21 | 'testing',
22 | ],
23 |
24 | /*
25 | |--------------------------------------------------------------------------
26 | | Development Providers config key
27 | |--------------------------------------------------------------------------
28 | |
29 | | Here you may define the config key where you placed all your dev
30 | | providers. You may define a key for each environment.
31 | |
32 | */
33 |
34 | 'dev_providers_config_keys' => [
35 | 'dev' => 'app.dev_providers',
36 | 'local' => 'app.local_providers',
37 | 'testing' => 'app.testing_providers',
38 | ],
39 |
40 | /*
41 | |--------------------------------------------------------------------------
42 | | Development Class Aliases
43 | |--------------------------------------------------------------------------
44 | |
45 | | Here you may define the config key where you placed akl your dev class
46 | | aliases. You may define a key for each environment.
47 | |
48 | */
49 |
50 | 'dev_aliases_config_keys' => [
51 | 'dev' => 'app.dev_aliases',
52 | 'local' => 'app.local_aliases',
53 | 'testing' => 'app.testing_aliases',
54 | ],
55 | ];
56 |
--------------------------------------------------------------------------------
/phpunit.xml.dist:
--------------------------------------------------------------------------------
1 |
2 |
18 |
19 |
20 | tests
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 | ./src
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/pint.json:
--------------------------------------------------------------------------------
1 | {
2 | "preset": "laravel"
3 | }
4 |
--------------------------------------------------------------------------------
/src/ServiceProvider.php:
--------------------------------------------------------------------------------
1 | publishes([__DIR__.'/../config/dev-booter.php' => config_path('dev-booter.php')], 'config');
20 |
21 | if ($this->isOnADevEnvironment()) {
22 | $this->bootDevAliases();
23 | }
24 | }
25 |
26 | /**
27 | * Register the application services.
28 | */
29 | public function register(): void
30 | {
31 | $this->mergeConfigFrom(__DIR__.'/../config/dev-booter.php', 'dev-booter');
32 |
33 | if ($this->isOnADevEnvironment()) {
34 | $this->registerDevProviders();
35 | }
36 | }
37 |
38 | /**
39 | * Register all dev providers.
40 | */
41 | protected function registerDevProviders(): void
42 | {
43 | $devProviderKey = 'dev-booter.dev_providers_config_keys.'.$this->app->environment();
44 |
45 | $this
46 | ->collectDevServiceProviders($devProviderKey)
47 | ->each(fn ($devServiceProviders) => $this->app->register($devServiceProviders));
48 | }
49 |
50 | /**
51 | * Boot all dev class aliases.
52 | */
53 | protected function bootDevAliases(): void
54 | {
55 | $loader = AliasLoader::getInstance();
56 |
57 | $devProviderKey = 'dev-booter.dev_aliases_config_keys.'.$this->app->environment();
58 |
59 | $this
60 | ->collectDevAliases($devProviderKey)
61 | ->each(fn ($facade, $alias) => $loader->alias($alias, $facade));
62 | }
63 |
64 | /**
65 | * Check if we are on a dev environment.
66 | */
67 | protected function isOnADevEnvironment(): bool
68 | {
69 | return $this->collectDevEnvironments()->search($this->app->environment()) !== false;
70 | }
71 |
72 | /**
73 | * Return Collection of Dev providers.
74 | */
75 | protected function collectDevServiceProviders(string $configKey): Collection
76 | {
77 | $devProvidersConfigLocation = Config::get($configKey);
78 |
79 | $keys = is_string($devProvidersConfigLocation) ? [$devProvidersConfigLocation] : $devProvidersConfigLocation;
80 |
81 | return collect($keys)
82 | ->transform(fn (string $location) => Config::get($location))
83 | ->filter()
84 | ->flatten()
85 | ->unique()
86 | ->values();
87 | }
88 |
89 | /**
90 | * Return Collection of Dev aliases.
91 | */
92 | protected function collectDevAliases(string $configKey): Collection
93 | {
94 | $devAliasesConfigLocation = Config::get($configKey);
95 |
96 | $keys = is_string($devAliasesConfigLocation) ? [$devAliasesConfigLocation] : $devAliasesConfigLocation;
97 |
98 | return collect($keys)
99 | ->transform(fn (string $location) => Config::get($location))
100 | ->filter()
101 | ->flatMap(fn (mixed $values) => $values)
102 | ->unique();
103 | }
104 |
105 | /**
106 | * Get a Collection of dev environments.
107 | */
108 | protected function collectDevEnvironments(): Collection
109 | {
110 | return collect(Config::get('dev-booter.dev_environments'));
111 | }
112 | }
113 |
--------------------------------------------------------------------------------