├── .styleci.yml
├── CHANGELOG.md
├── src
└── Commands
│ ├── MakeRule.php
│ ├── MakeEvent.php
│ ├── MakeRequest.php
│ ├── MakeNotification.php
│ ├── MakeJob.php
│ ├── stubs
│ ├── job.stub
│ ├── request.stub
│ ├── job-queued.stub
│ ├── rule.stub
│ ├── console.stub
│ ├── event.stub
│ └── notification.stub
│ ├── Composer.php
│ ├── MakeCommand.php
│ └── GeneratorCommand.php
├── pkg-tools
├── LICENSE.md
├── composer.json
├── README.md
└── CONTRIBUTING.md
/.styleci.yml:
--------------------------------------------------------------------------------
1 | preset: laravel
2 |
3 | disabled:
4 | - single_class_element_per_statement
5 |
--------------------------------------------------------------------------------
/CHANGELOG.md:
--------------------------------------------------------------------------------
1 | # Changelog
2 |
3 | All notable changes to `laravel-package-tools` will be documented in this file
4 |
5 | ## 1.0.0 - 201X-XX-XX
6 |
7 | - initial release
8 |
--------------------------------------------------------------------------------
/src/Commands/MakeRule.php:
--------------------------------------------------------------------------------
1 | option('sync')
12 | ? __DIR__.'/stubs/job.stub'
13 | : __DIR__.'/stubs/job-queued.stub';
14 | }
15 |
16 | protected function getDefaultNamespace($rootNamespace): string
17 | {
18 | return $rootNamespace.'\Jobs';
19 | }
20 | }
21 |
--------------------------------------------------------------------------------
/src/Commands/stubs/job.stub:
--------------------------------------------------------------------------------
1 | parse(), $key, $default);
12 | }
13 |
14 | protected function parse(): array
15 | {
16 | return json_decode($this->getComposerJson(), true);
17 | }
18 |
19 | protected function getComposerJson(): string
20 | {
21 | $composerJson = getcwd().'/composer.json';
22 |
23 | if (! file_exists($composerJson)) {
24 | }
25 |
26 | return file_get_contents($composerJson);
27 | }
28 | }
29 |
--------------------------------------------------------------------------------
/src/Commands/MakeCommand.php:
--------------------------------------------------------------------------------
1 | option('command', 'command:name'), $stub);
14 | }
15 |
16 | protected function getStub()
17 | {
18 | return __DIR__.'/stubs/console.stub';
19 | }
20 |
21 | protected function getDefaultNamespace($rootNamespace): string
22 | {
23 | return $rootNamespace.'\Console\Commands';
24 | }
25 | }
26 |
--------------------------------------------------------------------------------
/src/Commands/stubs/job-queued.stub:
--------------------------------------------------------------------------------
1 | command('make:command name [--command=] [--force]', new MakeCommand);
19 | $app->command('make:request name [--force]', new MakeRequest);
20 | $app->command('make:job name [--sync] [--force]', new MakeJob);
21 | $app->command('make:event name [--force]', new MakeEvent);
22 | $app->command('make:notification name [--force]', new MakeNotification);
23 | $app->command('make:rule name [--force]', new MakeRule);
24 |
25 | $app->run();
--------------------------------------------------------------------------------
/LICENSE.md:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) Marcel Pociot
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.
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "beyondcode/laravel-package-tools",
3 | "description": "Useful tools to help you with your next Laravel package.",
4 | "keywords": [
5 | "beyondcode",
6 | "laravel-package-tools"
7 | ],
8 | "homepage": "https://github.com/beyondcode/laravel-package-tools",
9 | "license": "MIT",
10 | "type": "library",
11 | "authors": [
12 | {
13 | "name": "Marcel Pociot",
14 | "email": "m.pociot@gmail.com",
15 | "role": "Developer"
16 | }
17 | ],
18 | "require": {
19 | "php": "^7.2",
20 | "illuminate/support": "5.6.*|5.7.*|5.8.*",
21 | "mnapoli/silly": "^1.7"
22 | },
23 | "require-dev": {
24 | "phpunit/phpunit": "^8.0",
25 | "spatie/phpunit-snapshot-assertions": "^2.1"
26 | },
27 | "autoload": {
28 | "psr-4": {
29 | "BeyondCode\\LaravelPackageTools\\": "src"
30 | }
31 | },
32 | "autoload-dev": {
33 | "psr-4": {
34 | "BeyondCode\\LaravelPackageTools\\Tests\\": "tests"
35 | }
36 | },
37 | "scripts": {
38 | "test": "vendor/bin/phpunit",
39 | "test-coverage": "vendor/bin/phpunit --coverage-html coverage"
40 |
41 | },
42 | "config": {
43 | "sort-packages": true
44 | },
45 | "bin": [
46 | "pkg-tools"
47 | ]
48 | }
49 |
--------------------------------------------------------------------------------
/src/Commands/stubs/notification.stub:
--------------------------------------------------------------------------------
1 | line('The introduction to the notification.')
45 | ->action('Notification Action', url('/'))
46 | ->line('Thank you for using our application!');
47 | }
48 |
49 | /**
50 | * Get the array representation of the notification.
51 | *
52 | * @param mixed $notifiable
53 | * @return array
54 | */
55 | public function toArray($notifiable)
56 | {
57 | return [
58 | //
59 | ];
60 | }
61 | }
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Laravel Package Tools
2 |
3 | [](https://packagist.org/packages/beyondcode/laravel-package-tools)
4 | [](https://travis-ci.org/beyondcode/laravel-package-tools)
5 | [](https://scrutinizer-ci.com/g/beyondcode/laravel-package-tools)
6 | [](https://packagist.org/packages/beyondcode/laravel-package-tools)
7 |
8 | Gives you the `make:` commands that you know and love from Laravel - outside of Laravel. Ready to use in your next package.
9 |
10 | [](https://phppackagedevelopment.com)
11 |
12 | If you want to learn how to create reusable PHP packages yourself, take a look at my upcoming [PHP Package Development](https://phppackagedevelopment.com) video course.
13 |
14 |
15 | ## Installation
16 |
17 | You can install the package via composer:
18 |
19 | ```bash
20 | composer require --dev beyondcode/laravel-package-tools
21 | ```
22 |
23 | ## Usage
24 |
25 | You can use this package from the root of the package that you are developing. You can use the `pkg-tools` binary to create and scaffold new classes.
26 |
27 | The package will automatically detect your namespace from your `composer.json` autoload configuration and apply it to the generated files.
28 |
29 | ## Available commands
30 |
31 | ```
32 | ./vendor/bin/pkg-tools make:command name [--command=] [--force]
33 |
34 | ./vendor/bin/pkg-tools make:request name [--force]
35 |
36 | ./vendor/bin/pkg-tools make:job name [--sync] [--force]
37 |
38 | ./vendor/bin/pkg-tools make:event name [--force]
39 |
40 | ./vendor/bin/pkg-tools make:notification name [--force]
41 |
42 | ./vendor/bin/pkg-tools make:rule name [--force]
43 | ```
44 |
45 | ### Testing
46 |
47 | ``` bash
48 | composer test
49 | ```
50 |
51 | ### Changelog
52 |
53 | Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
54 |
55 | ## Contributing
56 |
57 | Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
58 |
59 | ### Security
60 |
61 | If you discover any security related issues, please email marcel@beyondco.de instead of using the issue tracker.
62 |
63 | ## Credits
64 |
65 | - [Marcel Pociot](https://github.com/beyondcode)
66 | - [All Contributors](../../contributors)
67 |
68 | ## License
69 |
70 | The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
71 |
72 | ## Laravel Package Boilerplate
73 |
74 | This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).
--------------------------------------------------------------------------------
/CONTRIBUTING.md:
--------------------------------------------------------------------------------
1 | # Contributing
2 |
3 | Contributions are **welcome** and will be fully **credited**.
4 |
5 | Please read and understand the contribution guide before creating an issue or pull request.
6 |
7 | ## Etiquette
8 |
9 | This project is open source, and as such, the maintainers give their free time to build and maintain the source code
10 | held within. They make the code freely available in the hope that it will be of use to other developers. It would be
11 | extremely unfair for them to suffer abuse or anger for their hard work.
12 |
13 | Please be considerate towards maintainers when raising issues or presenting pull requests. Let's show the
14 | world that developers are civilized and selfless people.
15 |
16 | It's the duty of the maintainer to ensure that all submissions to the project are of sufficient
17 | quality to benefit the project. Many developers have different skillsets, strengths, and weaknesses. Respect the maintainer's decision, and do not be upset or abusive if your submission is not used.
18 |
19 | ## Viability
20 |
21 | When requesting or submitting new features, first consider whether it might be useful to others. Open
22 | source projects are used by many developers, who may have entirely different needs to your own. Think about
23 | whether or not your feature is likely to be used by other users of the project.
24 |
25 | ## Procedure
26 |
27 | Before filing an issue:
28 |
29 | - Attempt to replicate the problem, to ensure that it wasn't a coincidental incident.
30 | - Check to make sure your feature suggestion isn't already present within the project.
31 | - Check the pull requests tab to ensure that the bug doesn't have a fix in progress.
32 | - Check the pull requests tab to ensure that the feature isn't already in progress.
33 |
34 | Before submitting a pull request:
35 |
36 | - Check the codebase to ensure that your feature doesn't already exist.
37 | - Check the pull requests to ensure that another person hasn't already submitted the feature or fix.
38 |
39 | ## Requirements
40 |
41 | If the project maintainer has any additional requirements, you will find them listed here.
42 |
43 | - **[PSR-2 Coding Standard](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-2-coding-style-guide.md)** - The easiest way to apply the conventions is to install [PHP Code Sniffer](https://pear.php.net/package/PHP_CodeSniffer).
44 |
45 | - **Add tests!** - Your patch won't be accepted if it doesn't have tests.
46 |
47 | - **Document any change in behaviour** - Make sure the `README.md` and any other relevant documentation are kept up-to-date.
48 |
49 | - **Consider our release cycle** - We try to follow [SemVer v2.0.0](https://semver.org/). Randomly breaking public APIs is not an option.
50 |
51 | - **One pull request per feature** - If you want to do more than one thing, send multiple pull requests.
52 |
53 | - **Send coherent history** - Make sure each individual commit in your pull request is meaningful. If you had to make multiple intermediate commits while developing, please [squash them](https://www.git-scm.com/book/en/v2/Git-Tools-Rewriting-History#Changing-Multiple-Commit-Messages) before submitting.
54 |
55 | **Happy coding**!
56 |
--------------------------------------------------------------------------------
/src/Commands/GeneratorCommand.php:
--------------------------------------------------------------------------------
1 | composer = new Composer();
33 | }
34 |
35 | /**
36 | * Get the stub file for the generator.
37 | *
38 | * @return string
39 | */
40 | abstract protected function getStub();
41 |
42 | /**
43 | * Execute the console command.
44 | *
45 | * @param InputInterface $input
46 | * @param OutputInterface $output
47 | * @return bool|null
48 | */
49 | public function __invoke(InputInterface $input, OutputInterface $output)
50 | {
51 | $this->input = $input;
52 | $this->output = $output;
53 |
54 | $name = $this->qualifyClass($this->getNameInput());
55 |
56 | $path = $this->getPath($name);
57 |
58 | // First we will check to see if the class already exists. If it does, we don't want
59 | // to create the class and overwrite the user's code. So, we will bail out so the
60 | // code is untouched. Otherwise, we will continue generating this class' files.
61 | if (! $this->input->getOption('force') && $this->alreadyExists($this->getNameInput())) {
62 | $this->error($this->type.' already exists!');
63 |
64 | return false;
65 | }
66 |
67 | // Next, we will generate the path to the location where this class' file should get
68 | // written. Then, we will build the class and make the proper replacements on the
69 | // stub files so that it gets the correctly formatted namespace and class name.
70 | $this->makeDirectory(Str::before($path, basename($path)));
71 |
72 | file_put_contents($path, $this->buildClass($name));
73 |
74 | $this->info($this->type.' created successfully.');
75 | }
76 |
77 | /**
78 | * Parse the class name and format according to the root namespace.
79 | *
80 | * @param string $name
81 | * @return string
82 | */
83 | protected function qualifyClass($name)
84 | {
85 | $name = ltrim($name, '\\/');
86 |
87 | $rootNamespace = $this->rootNamespace();
88 |
89 | if (Str::startsWith($name, $rootNamespace)) {
90 | return $name;
91 | }
92 |
93 | $name = str_replace('/', '\\', $name);
94 |
95 | return $this->qualifyClass(
96 | $this->getDefaultNamespace(trim($rootNamespace, '\\')).'\\'.$name
97 | );
98 | }
99 |
100 | /**
101 | * Determine if the class already exists.
102 | *
103 | * @param string $rawName
104 | * @return bool
105 | */
106 | protected function alreadyExists($rawName)
107 | {
108 | return file_exists($this->getPath($this->qualifyClass($rawName)));
109 | }
110 |
111 | public function info($text)
112 | {
113 | $this->output->write(''.$text.'');
114 | }
115 |
116 | public function error($text)
117 | {
118 | $this->output->write(''.$text.'');
119 | }
120 |
121 | protected function getPath($name)
122 | {
123 | $name = Str::replaceFirst($this->rootNamespace(), '', $name);
124 |
125 | return $this->getOutputPath().str_replace('\\', '/', $name).'.php';
126 | }
127 |
128 | protected function getOutputPath(): string
129 | {
130 | return $this->outputPath ?? getcwd().'/src/';
131 | }
132 |
133 | protected function getNameInput(): string
134 | {
135 | return trim($this->input->getArgument('name'));
136 | }
137 |
138 | protected function rootNamespace()
139 | {
140 | $autoload = $this->composer->get('autoload.psr-4');
141 |
142 | return array_keys($autoload)[0];
143 | }
144 |
145 | protected function buildClass($name)
146 | {
147 | $stub = file_get_contents($this->getStub());
148 |
149 | return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
150 | }
151 |
152 | protected function replaceNamespace(&$stub, $name)
153 | {
154 | $stub = str_replace(
155 | ['DummyNamespace', 'DummyRootNamespace'],
156 | [$this->getNamespace($name), $this->rootNamespace()],
157 | $stub
158 | );
159 |
160 | return $this;
161 | }
162 |
163 | protected function getNamespace($name)
164 | {
165 | return trim(implode('\\', array_slice(explode('\\', $name), 0, -1)), '\\');
166 | }
167 |
168 | protected function replaceClass($stub, $name)
169 | {
170 | $class = str_replace($this->getNamespace($name).'\\', '', $name);
171 |
172 | return str_replace('DummyClass', $class, $stub);
173 | }
174 |
175 | protected function getDefaultNamespace(string $rootNamespace)
176 | {
177 | return $rootNamespace;
178 | }
179 |
180 | protected function makeDirectory(string $path)
181 | {
182 | @mkdir($path, 0777, true);
183 | }
184 |
185 | public function option($name, $default = null)
186 | {
187 | if ($this->input->hasOption($name)) {
188 | return $this->input->getOption($name) ?? $default;
189 | }
190 |
191 | return $default;
192 | }
193 | }
194 |
--------------------------------------------------------------------------------