├── .gitignore
├── src
├── stubs
│ ├── config.php
│ ├── jQuery.js
│ ├── es6-module.js
│ ├── blade.blade.php
│ ├── vue-component.js
│ ├── react-component.js
│ ├── Component.vue
│ └── basic-test.php
├── StubsServiceProvider.php
├── StubsCommand.php
├── config
│ └── template_stubs.php
└── StubsManager.php
├── composer.json
└── README.md
/.gitignore:
--------------------------------------------------------------------------------
1 | /vendor
2 | composer.phar
3 | composer.lock
4 | .DS_Store
5 |
--------------------------------------------------------------------------------
/src/stubs/config.php:
--------------------------------------------------------------------------------
1 | ''
5 | ];
6 |
--------------------------------------------------------------------------------
/src/stubs/jQuery.js:
--------------------------------------------------------------------------------
1 | /*
2 | * @placeholder
3 | */
4 |
5 | jQuery(function ($) {
6 |
7 | });
8 |
--------------------------------------------------------------------------------
/src/stubs/es6-module.js:
--------------------------------------------------------------------------------
1 | const @placeholder = function @placeholder {
2 |
3 | }
4 |
5 | module.exports = @placeholder;
6 |
--------------------------------------------------------------------------------
/src/stubs/blade.blade.php:
--------------------------------------------------------------------------------
1 | @extends('layouts.app')
2 |
3 | @section('content')
4 |
5 | {{--- @placeholder --}}
6 |
7 |
8 |
9 |
10 |
11 | @endsection
--------------------------------------------------------------------------------
/src/stubs/vue-component.js:
--------------------------------------------------------------------------------
1 | Vue.component('@placeholder', {
2 |
3 | props: [],
4 |
5 | data() {
6 | return {
7 |
8 | }
9 | },
10 |
11 | computed: {
12 |
13 | },
14 |
15 | methods: {
16 |
17 | },
18 |
19 | });
20 |
--------------------------------------------------------------------------------
/src/stubs/react-component.js:
--------------------------------------------------------------------------------
1 | import React, { Component } from 'react';
2 |
3 | class @placeholder extends Component {
4 | render() {
5 | return (
6 |
7 | @placeholder
8 |
9 | );
10 | }
11 | }
12 |
13 | export default @placeholder;
14 |
--------------------------------------------------------------------------------
/src/stubs/Component.vue:
--------------------------------------------------------------------------------
1 |
2 |
3 | @placeholder
4 |
5 |
6 |
7 |
26 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "owenmelbz/laravel-stubs",
3 | "description": "Easily store stubs for your project to reuse whenever needed.",
4 | "keywords": ["laravel", "stubs", "templates"],
5 | "license": "MIT",
6 | "authors": [
7 | {
8 | "name": "Owen Melbourne",
9 | "email": "owenmelbz@gmail.com"
10 | }
11 | ],
12 | "require-dev": {
13 | "phpunit/phpunit": "~4.5"
14 | },
15 | "autoload": {
16 | "psr-4": {
17 | "OwenMelbz\\LaravelStubs\\": "src/"
18 | }
19 | },
20 | "extra": {
21 | "laravel": {
22 | "providers": [
23 | "OwenMelbz\\LaravelStubs\\StubsServiceProvider"
24 | ]
25 | }
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/src/StubsServiceProvider.php:
--------------------------------------------------------------------------------
1 | app->runningInConsole()) {
18 | return false;
19 | }
20 |
21 | $this->commands([StubsCommand::class]);
22 |
23 | $this->publishes([
24 | __DIR__.'/config/template_stubs.php' => config_path('template_stubs.php'),
25 | ], 'stubs-config');
26 |
27 | $this->publishes([
28 | __DIR__.'/stubs' => resource_path('stubs'),
29 | ], 'stubs-stubs');
30 | }
31 |
32 | /**
33 | * Register any application services.
34 | *
35 | * @return void
36 | */
37 | public function register()
38 | {
39 | if (!$this->app->runningInConsole()) {
40 | return false;
41 | }
42 |
43 | $this->mergeConfigFrom(
44 | __DIR__.'/config/template_stubs.php', 'template_stubs'
45 | );
46 | }
47 |
48 | }
49 |
--------------------------------------------------------------------------------
/src/StubsCommand.php:
--------------------------------------------------------------------------------
1 | setType($this->argument('type'))
41 | ->setName($this->argument('name'))
42 | ->setParams($this->argument('params') ?? '')
43 | ->convertStubs($this->option('force'));
44 |
45 | return $this->info('File created at ' . $path);
46 | } catch (Exception $e) {
47 | return $this->error($e->getMessage());
48 | }
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/config/template_stubs.php:
--------------------------------------------------------------------------------
1 | [
9 | 'stub' => 'vue-component.js',
10 | 'output_path' => resource_path('assets/js/components'),
11 | ],
12 |
13 | /**
14 | * Creates a basic react component
15 | */
16 | 'react' => [
17 | 'stub' => 'react-component.js',
18 | 'output_path' => resource_path('assets/js/components'),
19 | ],
20 |
21 | /**
22 | * Creates a .vue component
23 | */
24 | '.vue' => [
25 | 'stub' => 'Component.vue',
26 | 'output_path' => resource_path('assets/js/components'),
27 | ],
28 |
29 | /**
30 | * Creates a basic jQuery encapsulation
31 | */
32 | 'jquery' => [
33 | 'stub' => 'jQuery.js',
34 | 'output_path' => resource_path('assets/js/components'),
35 | ],
36 |
37 | /**
38 | * Creates a basic es6 module
39 | */
40 | 'es6-module' => [
41 | 'stub' => 'es6-module.js',
42 | 'output_path' => resource_path('assets/js/components'),
43 | ],
44 |
45 | /**
46 | * Creates a basic blade view
47 | */
48 | 'blade' => [
49 | 'stub' => 'blade.blade.php',
50 | 'output_path' => resource_path('views'),
51 | ],
52 |
53 | /**
54 | * Creates a basic config file
55 | */
56 | 'config' => [
57 | 'stub' => 'config.php',
58 | 'output_path' => config_path(),
59 | ],
60 |
61 | /**
62 | * Creates a basic test file
63 | */
64 | 'basic-test' => [
65 | 'stub' => 'basic-test.php',
66 | 'output_path' => base_path('tests'),
67 | ],
68 | ];
69 |
--------------------------------------------------------------------------------
/src/stubs/basic-test.php:
--------------------------------------------------------------------------------
1 | create();
15 | $response = $this->json('GET', 'api/auth/{URL}');
16 | $expect = $response->decodeResponseJson();
17 | $response
18 | ->assertOk()
19 | ->assertExactJson($expect);
20 | }
21 |
22 | /** @test */
23 | public function testShow()
24 | {
25 | factory({MODEL}::class, 10)->create();
26 | $random_id = random_int(1, 10);
27 | $response = $this->json('GET', 'api/auth/{URL}/' . $random_id);
28 | $expect = $response->decodeResponseJson();
29 | $response
30 | ->assertOk()
31 | ->assertExactJson($expect);
32 | }
33 |
34 | /** @test */
35 | public function testStore()
36 | {
37 | factory({MODEL}::class, 10)->create();
38 | $data = ['name' => 'Category 1'];
39 | $response = $this->json('POST', 'api/auth/{URL}', $data);
40 | $expect = $response->decodeResponseJson();
41 | $response
42 | ->assertOk()
43 | ->assertExactJson($expect);
44 | }
45 |
46 | /** @test */
47 | public function testUpdate()
48 | {
49 | factory({MODEL}::class, 10)->create();
50 | $random_id = random_int(1, 10);
51 | $data = ['nome' => 'Category updated'];
52 | $response = $this->json('PUT', 'api/auth/{URL}/' . $random_id, $data);
53 | $expect = $response->decodeResponseJson();
54 | $response
55 | ->assertOk()
56 | ->assertExactJson($expect);
57 | }
58 |
59 | /** @test */
60 | public function testDelete()
61 | {
62 | factory({MODEL}::class, 10)->create();
63 | $random_id = random_int(1, 10);
64 | $response = $this->json('DELETE', 'api/auth/{URL}/' . $random_id);
65 | $expect = $response->decodeResponseJson();
66 | $response
67 | ->assertOk()
68 | ->assertExactJson($expect);
69 | }
70 | }
71 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Laravel Stub Helper
2 |
3 | We often find outselves having to repeat the same boilerplating when creating new files such as javascript components, blade templates etc.
4 |
5 | This package aims to help save your brain cells for the more important things in life (like pizza) by enabling you scaffold the things you're always repeating within a project by taking influence from the other `make:` commands.
6 |
7 |
8 | ## Installation
9 |
10 | 1- Install via composer `composer require owenmelbz/laravel-stubs`.
11 |
12 | 2- The package should use laravels new auto discovery, if not however you can manually register the service provider - typically done inside the `app.php` providers array e.g `OwenMelbz\LaravelStubs\StubsServiceProvider::class`.
13 |
14 | 3- If you're going to be modifying the default stubs, or adding your own stubs, you'll need to publish the config using `php artisan vendor:publish --tag=stubs-config` which will generate `config/template_stubs.php`
15 |
16 | ## Usage
17 |
18 | Run `php artisan make:a {stub} {name}` this will generate you a new file, based off the defined stub
19 |
20 | The `{name}` will become the filename and typically the class/component name where the default `@placeholder` marker is used within the files.
21 |
22 | ## Default stubs
23 |
24 | You can view a full list of the default stubs from the config file -> https://github.com/OwenMelbz/Laravel-Stubs/blob/master/src/config/template_stubs.php
25 |
26 | ## Modifying existing stubs
27 |
28 | If you want to modify a default stub you can do this in 2 different ways.
29 |
30 | The stubs are loaded in a waterfall style - so if you have your own file inside your `resources/stubs` folder we will use this file instead.
31 |
32 | You can either use the same filename that is defined within the `stub` key of the config e.g `vue-component.js` and we'll use that. Or you can create a file of any name and change the `stub` key in a published config file.
33 |
34 | If you want to modify every stub in the package, you can publish all the stubs in 1 go using `php artisan vendor:publish --tag=stubs-stubs` which will place them all in your `resources/stubs` folder.
35 |
36 | ## Adding custom stubs
37 |
38 | You can either publish the default config and modify it, or create your own called `template_stubs.php` - if you wanted you could even use `php artisan make:a config template_stubs` and start adding your own.
39 |
40 | Creating a new stub requires at minimum, a stub file, an output path, and a name. Below is an example structure
41 |
42 | ```php
43 | 'my-stub' => [
44 | 'stub' => 'my-stub.php',
45 | 'output_path' => resource_path('some-folder')
46 | ]
47 | ```
48 |
49 | It's worth noting that the file extension of the outputted file will match that of the stub file. So in this instance we do not recommend using `.stub` as your file extension.
50 |
51 | During the generation process we parse a token called `@placeholder` anywhere this marker is within your stub, will get replaced by the name you passed into the command. e.g
52 |
53 | This command `artisan make:a vue my-component` will see a file
54 |
55 | ```js
56 | Vue.component('@placeholder', {
57 |
58 | });
59 | ```
60 |
61 | and generate `my-component.js` with the content
62 |
63 | ```js
64 | Vue.component('my-component', {
65 |
66 | });
67 | ```
68 |
69 | If you want to change `@placeholder` to something else add the `placeholder => '@myothername'` to the stub definition within the config.
70 |
71 | ## Contributing community stubs
72 |
73 | Please feel free to submit pull requests to add custom stubs, make sure there are no naming conflicts, and keep them as simple as possible.
74 |
75 | Typically we adhear to a PSR-2 style for php files, I may modify the code style to make it consistent with submitted stubs.
76 |
77 | ## Notes
78 |
79 | This is a very new package, has not been heavily used in the wild, so please bare with me if you stumble across bugs, always add a PR where possible to fix issues as its massively appreciated.
80 |
81 | Thank :D
--------------------------------------------------------------------------------
/src/StubsManager.php:
--------------------------------------------------------------------------------
1 | stubs = config('template_stubs');
22 | }
23 |
24 | public function getName()
25 | {
26 | return $this->name;
27 | }
28 |
29 | public function setName($name)
30 | {
31 | $this->name = $name;
32 |
33 | return $this;
34 | }
35 |
36 | public function setParams($params = '')
37 | {
38 | $array_params = [];
39 |
40 | if (!empty($params)) {
41 | $explode = explode('|', $params);
42 | foreach ($explode as $row) {
43 | $expl = explode(':', $row);
44 | $array_params[$expl[0]] = $expl[1];
45 | }
46 | }
47 |
48 | $this->params = $array_params;
49 |
50 | return $this;
51 | }
52 |
53 | public function getParams()
54 | {
55 | return $this->params;
56 | }
57 |
58 | public function getType()
59 | {
60 | return $this->type;
61 | }
62 |
63 | public function setType($type)
64 | {
65 | if (!array_key_exists($type, $this->stubs)) {
66 | throw new Exception('Cannot find a stub with the name of ' . $type);
67 | }
68 |
69 | $this->stub = $this->stubs[$type];
70 |
71 | $this->type = $type;
72 |
73 | return $this;
74 | }
75 |
76 | public function convertStubs($forceOverwrite = false)
77 | {
78 | // We see if the user has a custom stub
79 | $userStub = resource_path('stubs/' . $this->stub['stub']);
80 | $vendorStub = __DIR__ . '/stubs/' . $this->stub['stub'];
81 |
82 | $stubPath = file_exists($userStub) ? $userStub : $vendorStub;
83 |
84 | // If we cant find the stub at all simply fail.
85 | if (!file_exists($stubPath)) {
86 | throw new Exception ('Could not find stub in path ' . $stubPath);
87 | }
88 |
89 | // Try be helpful and create the output folder.
90 | if (!file_exists($this->stub['output_path'])) {
91 | mkdir($this->stub['output_path'], 0755, true);
92 | }
93 |
94 | // If the file already exists, sugest the user to delete it first.
95 | $extension = pathinfo($stubPath, PATHINFO_EXTENSION);
96 |
97 | if (str_contains($this->stub['stub'], '.blade.php')) {
98 | $extension = 'blade.php';
99 | }
100 |
101 | $outputFile = rtrim($this->stub['output_path'], '/') . '/' . $this->getName() . '.' . $extension;
102 |
103 | if ($forceOverwrite === false && file_exists($outputFile)) {
104 | throw new Exception($outputFile . ' already exists, consider trying with "--force" to overwrite.');
105 | }
106 |
107 | // Set up the new file
108 | $stubContent = file_get_contents($stubPath);
109 |
110 | if (!isset($this->stub['placeholder'])) {
111 | $this->stub['placeholder'] = '@placeholder';
112 | }
113 |
114 | $newContent = str_replace($this->stub['placeholder'], $this->getName(), $stubContent);
115 |
116 | if (!empty($this->getParams())) {
117 | foreach ($this->getParams() as $key => $stub) {
118 | $newContent = str_replace($key, $stub, $newContent);
119 | }
120 | }
121 |
122 | if (!file_exists($this->stub['output_path'])) {
123 | mkdir($this->stub['output_path'], 0755, true);
124 | }
125 |
126 | if (!file_put_contents($outputFile, $newContent)) {
127 | throw new Exception('Could not write to ' . $outputFile);
128 | }
129 |
130 | return $outputFile;
131 | }
132 |
133 | }
--------------------------------------------------------------------------------