├── LICENSE
├── README.md
├── composer.json
└── src
├── Facades
└── LaravelStub.php
├── LaravelStub.php
└── Providers
└── LaravelStubServiceProvider.php
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2013 Oliver Vogel
4 |
5 | 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:
6 |
7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8 |
9 | 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.
10 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | ## Laravel Stub
2 |
3 |
4 |
5 | [](https://packagist.org/packages/binafy/laravel-stub)
6 | [](https://packagist.org/packages/binafy/laravel-stub)
7 | [](https://packagist.org/packages/binafy/laravel-stub)
8 | [](https://packagist.org/packages/binafy/laravel-stub)
9 | [](https://github.com/binafy/laravel-stub/actions/workflows/tests.yml)
10 |
11 | - [Introduction](#introduction)
12 | - [Requirements](#requirements)
13 | - [Installation](#installation)
14 | - [Usage](#usage)
15 | - [Create a stub file](#create-a-stub-file)
16 | - [How to use Laravel Stub](#how-using-laravel-stub)
17 | - [`from`](#from)
18 | - [`to`](#to)
19 | - [`name`](#name)
20 | - [`ext`](#ext)
21 | - [`replace`](#replace)
22 | - [`replaces`](#replaces)
23 | - [`moveStub`](#move-stub)
24 | - [`conditions`](#conditions)
25 | - [`download`](#download)
26 | - [`generate`](#generate)
27 | - [`generateForce`](#generate-force)
28 | - [`generateIf`](#generate-if)
29 | - [`generateUnless`](#generate-unless)
30 | - [Contributors](#contributors)
31 | - [Security](#security)
32 | - [Changelog](#changelog)
33 | - [License](#license)
34 | - [Donate](#donate)
35 |
36 |
37 | ## Introduction
38 |
39 | The Laravel-Stub package enhances the development workflow in Laravel by providing a set of customizable stubs. Stubs are templates used to scaffold code snippets for various components like models, controllers, and migrations. With Laravel-Stub, developers can easily tailor these stubs to match their project's coding standards and conventions. This package aims to streamline the code generation process, fostering consistency and efficiency in Laravel projects. Explore the customization options and boost your development speed with Laravel-Stub.
40 |
41 |
42 | ## Requirements
43 |
44 | ***
45 | - ```PHP >= 8.0```
46 | - ```Laravel >= 9.0```
47 |
48 |
49 | | Version/Laravel | L9 | L10 | L11 |
50 | |-----------------|--------------------|--------------------|--------------------|
51 | | 1.0 | :white_check_mark: | :white_check_mark: | :white_check_mark: |
52 |
53 |
54 | ## Installation
55 |
56 | You can install the package with Composer:
57 |
58 | ```bash
59 | composer require binafy/laravel-stub
60 | ```
61 |
62 | You don't need to publish anything.
63 |
64 |
65 | ## Usage
66 |
67 |
68 | ### Create a stub file
69 | First of all, create a stub file called `model.stub`:
70 |
71 | ```bash
72 | touch model.stub
73 | ```
74 |
75 | Add some code to that, like this:
76 |
77 | ```php
78 |
89 | ### How to use Laravel Stub
90 |
91 | You may use Laravel Stub, you need to use the `LaravelStub` facade:
92 |
93 | ```php
94 | use Binafy\LaravelStub\Facades\LaravelStub;
95 |
96 | LaravelStub::class;
97 | ```
98 |
99 |
100 | ### `from`
101 |
102 | First thing, you need to use the `from` method to give the stub path:
103 |
104 | ```php
105 | LaravelStub::from(__DIR__ . 'model.stub');
106 | ```
107 |
108 |
109 | ### `to`
110 |
111 | So, you need to determine the destination path of the stub file:
112 |
113 | ```php
114 | LaravelStub::from(__DIR__ . 'model.stub')
115 | ->to(__DIR__ . '/App');
116 | ```
117 |
118 |
119 | ### `name`
120 |
121 | You can determine the stub file but also attention don't write the stub extension:
122 |
123 | ```php
124 | LaravelStub::from(__DIR__ . 'model.stub')
125 | ->to(__DIR__ . '/App')
126 | ->name('new-model');
127 | ```
128 |
129 |
130 | ### `ext`
131 |
132 | You can determine the stub extension:
133 |
134 | ```php
135 | LaravelStub::from(__DIR__ . 'model.stub')
136 | ->to(__DIR__ . '/App')
137 | ->name('new-model')
138 | ->ext('php');
139 | ```
140 |
141 |
142 | ### `replace`
143 |
144 | The `replace` method takes two parameters, the first one is the key (variable) and the second one is the value. The value will be replaced with the variable:
145 |
146 | ```php
147 | LaravelStub::from(__DIR__ . 'model.stub')
148 | ->to(__DIR__ . '/App')
149 | ->name('new-model')
150 | ->ext('php')
151 | ->replace('NAMESPACE', 'App');
152 | ```
153 |
154 |
155 | ### `replaces`
156 |
157 | The `replaces` method takes an array. If you want to replace multiple variables you can use this method:
158 |
159 | ```php
160 | LaravelStub::from(__DIR__ . 'model.stub')
161 | ->to(__DIR__ . '/App')
162 | ->name('new-model')
163 | ->ext('php')
164 | ->replaces([
165 | 'NAMESPACE' => 'App',
166 | 'CLASS' => 'Milwad'
167 | ]);
168 | ```
169 |
170 |
171 | ### `moveStub`
172 |
173 | By default, `Laravel Stub` creates a copy from your stub file and moves it to the destination path. If you want to move the current stub file, you can use the `moveStub` method:
174 |
175 | ```php
176 | LaravelStub::from(__DIR__ . 'model.stub')
177 | ->to(__DIR__ . '/App')
178 | ->name('new-model')
179 | ->ext('php')
180 | ->replaces([
181 | 'NAMESPACE' => 'App',
182 | 'CLASS' => 'Milwad'
183 | ])
184 | ->moveStub();
185 | ```
186 |
187 | After running this code, the `model.stub` didn't exist.
188 |
189 |
190 | ### `conditions`
191 |
192 | The `conditions` method allows you to define conditional blocks within your stub files.
193 | You can specify conditions that determine whether certain parts of the stub should be included or excluded based on provided values or closures.
194 |
195 | ```php
196 | LaravelStub::from(__DIR__ . 'model.stub')
197 | ->to(__DIR__ . '/App')
198 | ->name('new-model')
199 | ->ext('php')
200 | ->replaces([
201 | 'NAMESPACE' => 'App',
202 | 'CLASS' => 'Milwad'
203 | ])
204 | ->conditions([
205 | 'hasController' => true,
206 | 'hasController' => fn() => false, // or with closure
207 | ])
208 | ->generate();
209 | ```
210 |
211 | > NOTE: Ensure that your stub files contain the appropriate conditional placeholders (e.g., {{ if isEnabled }}) to utilize this method effectively.
212 |
213 | Your stub code should looks like this:
214 |
215 | ```php
216 |
232 | ### `download`
233 |
234 | If you want to download the stub file, you can use the `download` method:
235 |
236 | ```php
237 | LaravelStub::from(__DIR__ . 'model.stub')
238 | ->to(__DIR__ . '/App')
239 | ->name('new-model')
240 | ->ext('php')
241 | ->replaces([
242 | 'NAMESPACE' => 'App',
243 | 'CLASS' => 'Milwad'
244 | ])
245 | ->download(); // Return download response
246 | ```
247 |
248 |
249 | ### `generate`
250 |
251 | The important method is `generate`. To generate the stub file at the end you need to use the `generate` method to generate stub file:
252 |
253 | ```php
254 | LaravelStub::from(__DIR__ . 'model.stub')
255 | ->to(__DIR__ . '/App')
256 | ->name('new-model')
257 | ->ext('php')
258 | ->replaces([
259 | 'NAMESPACE' => 'App',
260 | 'CLASS' => 'Milwad'
261 | ])
262 | ->generate();
263 | ```
264 |
265 | > **_NOTE:_** Don't use the `download` and `generate` methods in one chain.
266 |
267 | The final file will be like this (`new-model.php`):
268 |
269 | ```php
270 |
281 | ### `generateForce`
282 |
283 | If you want to generate a stub file and overwrite it if it exists, you can use the `generateForce` method:
284 |
285 | ```php
286 | LaravelStub::from(__DIR__ . 'model.stub')
287 | ->to(__DIR__ . '/App')
288 | ->name('new-model')
289 | ->ext('php')
290 | ->replaces([
291 | 'NAMESPACE' => 'App',
292 | 'CLASS' => 'Milwad'
293 | ])
294 | ->generateForce();
295 | ```
296 |
297 |
298 | ### `generateIf`
299 |
300 | If you want to generate a stub file if given boolean expression evaluates to `true`, you can use the `generateIf` method:
301 |
302 | ```php
303 | LaravelStub::from(__DIR__ . 'model.stub')
304 | ->to(__DIR__ . '/App')
305 | ->name('new-model')
306 | ->ext('php')
307 | ->replaces([
308 | 'NAMESPACE' => 'App',
309 | 'CLASS' => 'Milwad'
310 | ])
311 | ->generateIf(true);
312 | ```
313 |
314 |
315 | ### `generateUnless`
316 |
317 | If you want to generate a stub file if given boolean expression evaluates to `false`, you can use the `generateIf` method:
318 |
319 | ```php
320 | LaravelStub::from(__DIR__ . 'model.stub')
321 | ->to(__DIR__ . '/App')
322 | ->name('new-model')
323 | ->ext('php')
324 | ->replaces([
325 | 'NAMESPACE' => 'App',
326 | 'CLASS' => 'Milwad'
327 | ])
328 | ->generateUnless(true);
329 | ```
330 |
331 |
332 | ## Contributors
333 |
334 | Thanks to all the people who contributed. [Contributors](https://github.com/binafy/laravel-stub/graphs/contributors).
335 |
336 |
337 |
338 |
339 | ## Security
340 |
341 | If you discover any security-related issues, please email `binafy23@gmail.com` instead of using the issue tracker.
342 |
343 |
344 | ## Changelog
345 |
346 | The changelog can be found in the `CHANGELOG.md` file of the GitHub repository. It lists the changes, bug fixes, and improvements made to each version of the Laravel User Monitoring package.
347 |
348 |
349 | ## License
350 |
351 | The MIT License (MIT). Please see [License File](https://github.com/binafy/laravel-stub/blob/1.x/LICENSE) for more information.
352 |
353 | ## Donate
354 |
355 | If this package is helpful for you, you can buy a coffee for me :) ❤️
356 |
357 | - Iraninan Gateway: https://daramet.com/milwad_khosravi
358 | - Paypal Gateway: SOON
359 | - MetaMask Address: `0xf208a562c5a93DEf8450b656c3dbc1d0a53BDE58`
360 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "binafy/laravel-stub",
3 | "description": "Generate stub files easy",
4 | "keywords": [
5 | "binafy",
6 | "milwad",
7 | "laravel",
8 | "framework",
9 | "stub",
10 | "filesystem",
11 | "generate stub",
12 | "laravel stub package",
13 | "laravel package",
14 | "laravel-stub",
15 | "laravel stub",
16 | "laravel stub package",
17 | "laravel stub binafy",
18 | "laravel stub generator"
19 | ],
20 | "homepage": "https://github.com/binafy/laravel-stub",
21 | "type": "library",
22 | "license": "MIT",
23 | "minimum-stability": "stable",
24 | "require": {
25 | "php": "^8.0",
26 | "illuminate/support": "^9.0|^10.0|^11.0|^12.0"
27 | },
28 | "require-dev": {
29 | "pestphp/pest-plugin-laravel": "1.*|2.*|3.*",
30 | "orchestra/testbench": "8.*|9.*|10.*"
31 | },
32 | "autoload": {
33 | "psr-4": {
34 | "Binafy\\LaravelStub\\": "src/"
35 | }
36 | },
37 | "autoload-dev": {
38 | "psr-4": {
39 | "Tests\\": "tests/"
40 | }
41 | },
42 | "authors": [
43 | {
44 | "name": "Milwad Khosravi",
45 | "email": "milwad.dev@gmail.com",
46 | "role": "author",
47 | "homepage": "https://github.com/milwad-dev"
48 | },
49 | {
50 | "name": "binafy",
51 | "role": "owner",
52 | "homepage": "https://github.com/binafy"
53 | }
54 | ],
55 | "config": {
56 | "sort-packages": true,
57 | "allow-plugins": {
58 | "pestphp/pest-plugin": true
59 | }
60 | },
61 | "scripts": {
62 | "test": "vendor/bin/pest",
63 | "test-coverage": "vendor/bin/pest --coverage-html ./coverage"
64 | },
65 | "extra": {
66 | "laravel": {
67 | "providers": [
68 | "Binafy\\LaravelStub\\Providers\\LaravelStubServiceProvider"
69 | ]
70 | }
71 | }
72 | }
73 |
--------------------------------------------------------------------------------
/src/Facades/LaravelStub.php:
--------------------------------------------------------------------------------
1 | $conditions)
17 | *
18 | * @see \Binafy\LaravelStub\LaravelStub
19 | */
20 | class LaravelStub extends Facade
21 | {
22 | /**
23 | * Get facade accessor.
24 | */
25 | protected static function getFacadeAccessor(): string
26 | {
27 | return 'laravel-stub';
28 | }
29 | }
30 |
--------------------------------------------------------------------------------
/src/LaravelStub.php:
--------------------------------------------------------------------------------
1 | from = $path;
67 |
68 | return $this;
69 | }
70 |
71 | /**
72 | * Set stub destination path.
73 | */
74 | public function to(string $to): static
75 | {
76 | $this->to = $to;
77 |
78 | return $this;
79 | }
80 |
81 | /**
82 | * Set new stub name.
83 | */
84 | public function name(string $name): static
85 | {
86 | $this->name = $name;
87 |
88 | return $this;
89 | }
90 |
91 | /**
92 | * Set stub extension.
93 | */
94 | public function ext(string $ext): static
95 | {
96 | $this->ext = $ext;
97 |
98 | return $this;
99 | }
100 |
101 | /**
102 | * Set new replace with key and value.
103 | */
104 | public function replace(string $key, mixed $value): static
105 | {
106 | $this->replaces[$key] = $value;
107 |
108 | return $this;
109 | }
110 |
111 | /**
112 | * Set new replace with key and value.
113 | */
114 | public function replaces(array $replaces): static
115 | {
116 | foreach ($replaces as $key => $value) {
117 | $this->replaces[$key] = $value;
118 | }
119 |
120 | return $this;
121 | }
122 |
123 | /**
124 | * Set stub file move without any copy.
125 | */
126 | public function moveStub(): static
127 | {
128 | $this->moveStub = true;
129 |
130 | return $this;
131 | }
132 |
133 | /**
134 | * Set conditions.
135 | *
136 | * @param array $conditions
137 | */
138 | public function conditions(array $conditions): static
139 | {
140 | $this->conditions = $conditions;
141 |
142 | return $this;
143 | }
144 |
145 | /**
146 | * Download the stub file.
147 | */
148 | public function download()
149 | {
150 | $this->generate();
151 |
152 | return Response::download($this->getPath());
153 | }
154 |
155 | /**
156 | * Set stub file move without any copy.
157 | */
158 | public function generateForce(): bool
159 | {
160 | return $this->generate(true);
161 | }
162 |
163 | /**
164 | * Generate stub if condition pass.
165 | */
166 | public function generateIf(bool $condition): bool
167 | {
168 | if ($condition) {
169 | return $this->generate();
170 | }
171 |
172 | return false;
173 | }
174 |
175 | /**
176 | * Generate stub if condition pass (reversed).
177 | */
178 | public function generateUnless(bool $condition): bool
179 | {
180 | if (! $condition) {
181 | return $this->generate();
182 | }
183 |
184 | return false;
185 | }
186 |
187 | /**
188 | * Generate stub file.
189 | */
190 | public function generate(bool $force = false): bool
191 | {
192 | // Check path is valid
193 | if (! File::exists($this->from)) {
194 | throw new RuntimeException("The {$this->from} stub file does not exist, please enter a valid path.");
195 | }
196 |
197 | // Check destination path is valid
198 | if (! File::isDirectory($this->to)) {
199 | throw new RuntimeException('The given folder path is not valid.');
200 | }
201 |
202 | // Check if files exists and it not force throw exception
203 | if (! File::exists($this->to) && !$force) {
204 | throw new RuntimeException('The destination file does not exist, please enter a valid path.');
205 | }
206 |
207 | // Get file content
208 | $content = File::get($this->from);
209 |
210 | // Replace variables
211 | foreach ($this->replaces as $search => $value) {
212 | $content = str_replace("{{ $search }}", $value, $content);
213 | }
214 |
215 | // Process conditions
216 | if (count($this->conditions) !== 0) {
217 | foreach ($this->conditions as $condition => $value) {
218 | if ($value instanceof Closure) {
219 | $value = $value();
220 | }
221 |
222 | if ($value) {
223 | // Replace placeholders for conditions that are true
224 | $content = preg_replace(
225 | "/^[ \t]*{{ if $condition }}\s*\n(.*?)(?=^[ \t]*{{ endif }}\s*\n)/ms",
226 | "$1",
227 | $content
228 | );
229 | } else {
230 | // Remove the entire block for conditions that are false
231 | $content = preg_replace(
232 | "/^[ \t]*{{ if $condition }}\s*\n.*?^[ \t]*{{ endif }}\s*\n/ms",
233 | '',
234 | $content
235 | );
236 | }
237 | }
238 |
239 | // Finally, clean up any remaining conditional tags and extra newlines
240 | $content = preg_replace("/^[ \t]*{{ if .*? }}\s*\n|^[ \t]*{{ endif }}\s*\n/m", "\n", $content);
241 | $content = preg_replace("/^[ \t]*\n/m", "\n", $content);
242 | }
243 |
244 | // Get correct path
245 | $path = $this->getPath();
246 |
247 | if ($this->moveStub) {
248 | File::move($this->from, $path); // Move the file
249 | } else {
250 | File::copy($this->from, $path); // Copy the file
251 | }
252 |
253 | // Put content and write on file
254 | File::put($path, $content);
255 |
256 | return true;
257 | }
258 |
259 | /**
260 | * Get final path.
261 | */
262 | private function getPath(): string
263 | {
264 | $path = "{$this->to}/{$this->name}";
265 |
266 | // Add extension
267 | if (! is_null($this->ext)) {
268 | $path .= ".$this->ext";
269 | }
270 |
271 | return $path;
272 | }
273 | }
274 |
--------------------------------------------------------------------------------
/src/Providers/LaravelStubServiceProvider.php:
--------------------------------------------------------------------------------
1 | app->bind('laravel-stub', fn ($app) => new LaravelStub);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------