├── .gitignore
├── LICENSE
├── README.md
├── composer.json
├── config
└── laravel-more-command.php
└── src
├── Commands
├── ClearLogCommand.php
├── CommandGenerator.php
├── CreateBladeCommand.php
├── CreateModuleBladeCommand.php
├── CreateModuleRepositoryCommand.php
├── CreateModuleServiceCommand.php
├── CreateModuleTraitCommand.php
├── CreateRepositoryCommand.php
├── CreateServiceCommand.php
├── CreateTraitCommand.php
└── stubs
│ ├── blade.stub
│ ├── interface.stub
│ ├── repository-interface.stub
│ ├── repository.stub
│ ├── service.stub
│ └── traits.stub
├── LaravelMoreCommandProvider.php
└── Support
├── FileGenerator.php
└── GenerateFile.php
/.gitignore:
--------------------------------------------------------------------------------
1 | vendor
2 | .idea/
3 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | MIT License
2 |
3 | Copyright (c) 2021 theanik
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 | 
3 | 
4 | [](https://github.com/theanik/laravel-more-command/blob/master/LICENSE)
5 |
6 | # Laravel More Command
7 | Laravel More Command is a collection of a few `php artisan` commands. You can use it to create a __Repository, Repository with Interface, Service, Trait, View(blade file)__, and __Clear Log__ from the command line using `php artisan` command.\
8 |
9 | [Note : This package also worked for [nWidart/laravel-modules](https://github.com/nWidart/laravel-modules)]
10 |
11 | ## Installation
12 | Require the package with composer using the following command:
13 |
14 | ```
15 | composer require theanik/laravel-more-command --dev
16 | ```
17 |
18 | Or add the following to your composer.json's require-dev section and `composer update`
19 |
20 | ```json
21 | "require-dev": {
22 | "theanik/laravel-more-command": "^1.3.0"
23 | }
24 | ```
25 |
26 | ## Publish Package Configuration
27 | ```shell
28 | php artisan vendor:publish --provider="Theanik\LaravelMoreCommand\LaravelMoreCommandProvider" --tag="config"
29 | ```
30 | ### To Change Default Namespace [config/laravel-more-command.php]
31 | ```php
32 | 'App', // Your Desire Namespace for Repository Classes
35 | 'service-namespace' => 'App', // Your Desire Namespace for Service Classes
36 | ];
37 | ```
38 |
39 | ## Artisan Command List
40 |
41 |
42 |
51 |
52 |
53 |
54 |
55 | ## Make Repository
56 |
57 | __Create a repository Class.__\
58 | `php artisan make:repository your-repository-name`
59 |
60 | Example:
61 | ```
62 | php artisan make:repository UserRepository
63 | ```
64 | or
65 | ```
66 | php artisan make:repository Backend/UserRepository
67 | ```
68 |
69 | The above will create a **Repositories** directory inside the **App** directory.\
70 |
71 | __Create a repository with Interface.__\
72 | `php artisan make:repository your-repository-name -i`
73 |
74 | Example:
75 | ```
76 | php artisan make:repository UserRepository -i
77 | ```
78 | or
79 | ```
80 | php artisan make:repository Backend/UserRepository -i
81 | ```
82 | Here you need to put extra `-i` flag.
83 | The above will create a **Repositories** directory inside the **App** directory.
84 |
85 |
86 | ###### In [nWidart/laravel-modules](https://github.com/nWidart/laravel-modules) Modules
87 |
88 | __Create a repository Class.__\
89 | `php artisan module:make-repository your-repository-name {module-name}`
90 |
91 | Example:
92 | ```
93 | php artisan module:make-repository UserRepository Blog
94 | ```
95 | or
96 | ```
97 | php artisan module:make-repository Backend/UserRepository Blog
98 | ```
99 |
100 | The above will create a **Repositories** directory inside the **{Module}** directory.
101 |
102 | __Create a repository with Interface.__\
103 | `php artisan module:make-repository your-repository-name {module-name} -i`
104 |
105 | Example:
106 | ```
107 | php artisan module:make-repository UserRepository -i Blog
108 | ```
109 | or
110 | ```
111 | php artisan module:make-repository Backend/UserRepository -i Blog
112 | ```
113 | Here you need to put extra `-i` flag.
114 | The above will create a **Repositories** directory inside the **{Module}** directory.
115 | \
116 |
117 | __An Example of created repository class:__
118 |
119 | ```
120 |
136 |
137 |
138 | ## Make Service
139 |
140 | __Create a Service Class.__\
141 | `php artisan make:service your-service-name`
142 |
143 | Example:
144 | ```
145 | php artisan make:service UserService
146 | ```
147 | or
148 | ```
149 | php artisan make:service Backend/UserService
150 | ```
151 | The above will create a **Services** directory inside the **App** directory.
152 |
153 |
154 | ###### In [nWidart/laravel-modules](https://github.com/nWidart/laravel-modules) Modules
155 |
156 | `php artisan module:make-service your-service-name {module-name}`
157 |
158 | Example:
159 | ```
160 | php artisan module:make-service UserService
161 | ```
162 | or
163 | ```
164 | php artisan module:make-service Backend/UserService
165 | ```
166 | The above will create a **Services** directory inside the **{Module}** directory.
167 |
168 |
169 |
170 |
171 | ## Make Trait
172 |
173 | __Create a Trait.__\
174 | `php artisan make:trait your-trait-name`
175 |
176 | Example:
177 | ```
178 | php artisan make:trait HasAuth
179 | ```
180 | or
181 | ```
182 | php artisan make:trait Backend/HasAuth
183 | ```
184 | The above will create a **Traits** directory inside the **App** directory.
185 |
186 | ###### In [nWidart/laravel-modules](https://github.com/nWidart/laravel-modules) Modules
187 |
188 | `php artisan module:make-trait your-trait-name {module-name}`
189 |
190 | Example:
191 | ```
192 | php artisan module:make-trait HasAuth
193 | ```
194 | or
195 | ```
196 | php artisan module:make-trait Backend/HasAuth
197 | ```
198 | The above will create a **Traits** directory inside the **{Module}** directory.
199 |
200 |
201 |
202 |
203 |
204 |
205 | ## Make View
206 | __Create a view.__\
207 | `php artisan make:view your-view-file-name`
208 |
209 | Example:
210 | ```
211 | php artisan make:view index
212 | ```
213 | or
214 | ```
215 | php artisan make:view user/index
216 | ```
217 | The above will create a **blade** file inside the **/resource/views/** directory.
218 |
219 | ###### In [nWidart/laravel-modules](https://github.com/nWidart/laravel-modules) Modules
220 |
221 | `php artisan module:make-view your-view-file-name {module-name}`
222 |
223 | Example:
224 | ```
225 | php artisan module:make-view index
226 | ```
227 | or
228 | ```
229 | php artisan module:make-view user/index
230 | ```
231 | The above will create a **blade** file inside the **{Module}/Resources/views/** directory.
232 |
233 |
234 |
235 |
236 |
237 | ## Log Clear
238 |
239 | `php artisan log:clear`
240 |
241 | The above will deleted all old log data from **/storage/logs/** directory.
242 |
243 |
244 |
245 |
246 | # License
247 |
248 | The MIT License (MIT). Please see [License](LICENSE) for more information.
249 |
--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "theanik/laravel-more-command",
3 | "require": {},
4 | "description": "Create a Repository and Service class and trait using Artisan CLI",
5 | "license": "MIT",
6 | "authors": [
7 | {
8 | "name": "Anwar Hossain Anik",
9 | "email": "anwar.anik33@gmail.com"
10 | }
11 | ],
12 | "minimum-stability": "dev",
13 | "type": "library",
14 | "extra": {
15 | "laravel": {
16 | "providers": [
17 | "Theanik\\LaravelMoreCommand\\LaravelMoreCommandProvider"
18 | ]
19 | }
20 | },
21 | "autoload": {
22 | "psr-4": {
23 | "Theanik\\LaravelMoreCommand\\": "src/"
24 | },
25 | "classmap": ["src/"]
26 | }
27 | }
28 |
--------------------------------------------------------------------------------
/config/laravel-more-command.php:
--------------------------------------------------------------------------------
1 | 'App',
17 |
18 | /*
19 | |--------------------------------------------------------------------------
20 | | Service classes namespace
21 | |--------------------------------------------------------------------------
22 | |
23 | | This value defines the default namespace for created Service classes.
24 | | For example if the value is 'App/Http', it will create repository classes
25 | | inside 'App/Http/Services' and class namespace will
26 | | 'App/Http/Services/{ClassName}'.
27 | |
28 | */
29 | 'service-namespace' => 'App',
30 | ];
31 |
--------------------------------------------------------------------------------
/src/Commands/ClearLogCommand.php:
--------------------------------------------------------------------------------
1 | info("Logs have been cleared!");
44 |
45 | Log::info("Log Cleared at ".date('l jS \of F Y h:i:s A'));
46 | }
47 | }
48 |
--------------------------------------------------------------------------------
/src/Commands/CommandGenerator.php:
--------------------------------------------------------------------------------
1 | argument($this->argumentName));
88 | }
89 |
90 |
91 | /**
92 | * Generate class namespace dynamically
93 | * getClassNamespace
94 | *
95 | * @return string
96 | */
97 | public function getClassNamespace(): string
98 | {
99 | $extra = str_replace(array($this->getClass(), '/'), array('', '\\'), $this->argument($this->argumentName));
100 |
101 | $namespace = $this->getDefaultNamespace();
102 |
103 | $namespace .= '\\' . $extra;
104 |
105 | $namespace = str_replace('/', '\\', $namespace);
106 |
107 | return trim($namespace, '\\');
108 | }
109 |
110 |
111 | /**
112 | * Generate interface namespace dynamically
113 | * getInterfaceNamespace
114 | *
115 | * @return string
116 | */
117 | public function getInterfaceNamespace(): string
118 | {
119 | $extra = str_replace(array($this->getClass() . 'Interface', '/'), array('', '\\'), $this->argument($this->argumentName) . 'Interface');
120 |
121 | $namespace = $this->getDefaultInterfaceNamespace();
122 |
123 | $namespace .= '\\' . $extra;
124 |
125 | $namespace = str_replace('/', '\\', $namespace);
126 |
127 | return trim($namespace, '\\');
128 | }
129 |
130 |
131 | /**
132 | * checkModuleExists
133 | *
134 | * @param mixed $moduleName
135 | * @return bool
136 | */
137 | public function checkModuleExists(string $moduleName): bool
138 | {
139 | if (!in_array($moduleName, scandir(base_path() . "/Modules"))) {
140 | return false;
141 | }
142 | return true;
143 | }
144 |
145 | }
146 |
--------------------------------------------------------------------------------
/src/Commands/CreateBladeCommand.php:
--------------------------------------------------------------------------------
1 | argument('view'));
64 | if (Str::contains(strtolower($view), '.blade.php') === false) {
65 | $view .= '.blade.php';
66 | }
67 | return $view;
68 | }
69 |
70 | /**
71 | * getDestinationFilePath
72 | *
73 | * @return string
74 | */
75 | protected function getDestinationFilePath(): string
76 | {
77 | return base_path()."/resources/views".'/'. $this->getViewName();
78 | }
79 |
80 |
81 | /**
82 | * getStubFilePath
83 | *
84 | * @return string
85 | */
86 | protected function getStubFilePath(): string
87 | {
88 | return '/stubs/blade.stub';
89 | }
90 |
91 | /**
92 | * getTemplateContents
93 | *
94 | * @return string
95 | */
96 | protected function getTemplateContents(): string
97 | {
98 | return (new GenerateFile(__DIR__.$this->getStubFilePath()))->render();
99 | }
100 |
101 | /**
102 | * Execute the console command.
103 | *
104 | * @return int
105 | */
106 | public function handle()
107 | {
108 | $path = str_replace('\\', '/', $this->getDestinationFilePath());
109 |
110 | if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
111 | $this->laravel['files']->makeDirectory($dir, 0777, true);
112 | }
113 |
114 | $contents = $this->getTemplateContents();
115 |
116 | try {
117 | (new FileGenerator($path, $contents))->generate();
118 |
119 | $this->info("Created : {$path}");
120 | } catch (\Exception $e) {
121 |
122 | $this->error("File : {$e->getMessage()}");
123 |
124 | return E_ERROR;
125 | }
126 |
127 | return 0;
128 |
129 | }
130 |
131 | }
132 |
--------------------------------------------------------------------------------
/src/Commands/CreateModuleBladeCommand.php:
--------------------------------------------------------------------------------
1 | argument('view'));
65 | if (Str::contains(strtolower($view), '.blade.php') === false) {
66 | $view .= '.blade.php';
67 | }
68 | return $view;
69 | }
70 |
71 | /**
72 | * getDestinationFilePath
73 | *
74 | * @return string
75 | */
76 | protected function getDestinationFilePath(): string
77 | {
78 | return base_path()."/Modules/{$this->argument('module')}"."/Resources/views".'/'. $this->getViewName();
79 | }
80 |
81 |
82 | /**
83 | * getStubFilePath
84 | *
85 | * @return string
86 | */
87 | protected function getStubFilePath(): string
88 | {
89 | return '/stubs/blade.stub';
90 | }
91 |
92 | /**
93 | * getTemplateContents
94 | *
95 | * @return string
96 | */
97 | protected function getTemplateContents(): string
98 | {
99 | return (new GenerateFile(__DIR__.$this->getStubFilePath()))->render();
100 | }
101 |
102 | /**
103 | * Execute the console command.
104 | *
105 | * @return int
106 | */
107 | public function handle()
108 | {
109 | // Check this module exists or not.
110 | if ($this->checkModuleExists($this->argument('module')) === false) {
111 | $this->error(" Module [{$this->argument('module')}] does not exist!");
112 | return E_ERROR;
113 | exit;
114 | }
115 |
116 | $path = str_replace('\\', '/', $this->getDestinationFilePath());
117 |
118 | if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
119 | $this->laravel['files']->makeDirectory($dir, 0777, true);
120 | }
121 |
122 | $contents = $this->getTemplateContents();
123 |
124 | try {
125 | (new FileGenerator($path, $contents))->generate();
126 |
127 | $this->info("Created : {$path}");
128 | } catch (\Exception $e) {
129 |
130 | $this->error("File : {$e->getMessage()}");
131 |
132 | return E_ERROR;
133 | }
134 |
135 | return 0;
136 | }
137 |
138 | }
139 |
--------------------------------------------------------------------------------
/src/Commands/CreateModuleRepositoryCommand.php:
--------------------------------------------------------------------------------
1 | argument('repository'));
80 |
81 | if (Str::contains(strtolower($repository), 'repository') === false) {
82 | $repository .= 'Repository';
83 | }
84 |
85 | return $repository;
86 | }
87 |
88 | /**
89 | * Return destination path for class file publish
90 | * getDestinationFilePath
91 | *
92 | * @return string
93 | */
94 | protected function getDestinationFilePath(): string
95 | {
96 | return base_path()."/Modules/{$this->argument('module')}"."/Repositories".'/'. $this->getRepositoryName() . '.php';
97 | }
98 |
99 | /**
100 | * Return Inference name for this repository class
101 | * getInterfaceName
102 | *
103 | * @return string
104 | */
105 | protected function getInterfaceName(): string
106 | {
107 | return $this->getRepositoryName()."Interface";
108 | }
109 |
110 | /**
111 | * Return destination path for interface file publish
112 | * interfaceDestinationPath
113 | *
114 | * @return string
115 | */
116 | protected function interfaceDestinationPath(): string
117 | {
118 | return base_path()."/Modules/{$this->argument('module')}"."/Repositories/Interfaces".'/'. $this->getInterfaceName() . '.php';
119 | }
120 |
121 | /**
122 | * Return only repository class name
123 | * getRepositoryNameWithoutNamespace
124 | *
125 | * @return string
126 | */
127 | private function getRepositoryNameWithoutNamespace(): string
128 | {
129 | return class_basename($this->getRepositoryName());
130 | }
131 |
132 | /**
133 | * Set Default Namespace
134 | * Override CommandGenerator class method
135 | * getDefaultNamespace
136 | *
137 | * @return string
138 | */
139 | public function getDefaultNamespace() : string
140 | {
141 | return "Modules\\{$this->argument('module')}\\Repositories";
142 | }
143 |
144 | /**
145 | * Return only repository interface name
146 | * getInterfaceNameWithoutNamespace
147 | *
148 | * @return string
149 | */
150 | private function getInterfaceNameWithoutNamespace(): string
151 | {
152 | return class_basename($this->getInterfaceName());
153 | }
154 |
155 | /**
156 | * Set Default interface Namespace
157 | * Override CommandGenerator class method
158 | * getDefaultInterfaceNamespace
159 | *
160 | * @return string
161 | */
162 | public function getDefaultInterfaceNamespace() : string
163 | {
164 | return "Modules\\{$this->argument('module')}\\Repositories\\Interfaces";
165 | }
166 |
167 |
168 | /**
169 | * Return stub file path
170 | * getStubFilePath
171 | *
172 | * @return string
173 | */
174 | protected function getStubName(): string
175 | {
176 | if ($this->option('interface') === true) {
177 | $stub = '/stubs/repository-interface.stub';
178 | } else {
179 | $stub = '/stubs/repository.stub';
180 | }
181 | return $stub;
182 | }
183 |
184 |
185 | /**
186 | * Generate file content
187 | * getTemplateContents
188 | *
189 | * @return string
190 | */
191 | protected function getTemplateContents(): string
192 | {
193 | return (new GenerateFile(__DIR__.$this->getStubName(), [
194 | 'CLASS_NAMESPACE' => $this->getClassNamespace(),
195 | 'INTERFACE_NAMESPACE' => $this->getInterfaceNamespace().'\\'.$this->getInterfaceNameWithoutNamespace(),
196 | 'CLASS' => $this->getRepositoryNameWithoutNamespace(),
197 | 'INTERFACE' => $this->getInterfaceNameWithoutNamespace()
198 | ]))->render();
199 | }
200 |
201 |
202 | /**
203 | * Generate interface file content
204 | * getInterfaceTemplateContents
205 | *
206 | * @return string
207 | */
208 | protected function getInterfaceTemplateContents(): string
209 | {
210 | return (new GenerateFile(__DIR__."/stubs/interface.stub", [
211 | 'CLASS_NAMESPACE' => $this->getInterfaceNamespace(),
212 | 'INTERFACE' => $this->getInterfaceNameWithoutNamespace()
213 | ]))->render();
214 | }
215 |
216 |
217 | /**
218 | * Execute the console command.
219 | *
220 | * @return int
221 | */
222 | public function handle()
223 | {
224 | // Check this module exists or not.
225 | if ($this->checkModuleExists($this->argument('module')) === false) {
226 | $this->error(" Module [{$this->argument('module')}] does not exist!");
227 | return E_ERROR;
228 | exit;
229 | }
230 |
231 | $path = str_replace('\\', '/', $this->getDestinationFilePath());
232 |
233 | if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
234 | $this->laravel['files']->makeDirectory($dir, 0777, true);
235 | }
236 |
237 | $contents = $this->getTemplateContents();
238 |
239 | // For Interface
240 | if($this->option('interface') === true){
241 | $interfacePath = str_replace('\\', '/', $this->interfaceDestinationPath());
242 |
243 | if (!$this->laravel['files']->isDirectory($dir = dirname($interfacePath))) {
244 | $this->laravel['files']->makeDirectory($dir, 0777, true);
245 | }
246 |
247 | $interfaceContents = $this->getInterfaceTemplateContents();
248 | }
249 |
250 | try {
251 | (new FileGenerator($path, $contents))->generate();
252 |
253 | $this->info("Created : {$path}");
254 |
255 | // For Interface
256 | if($this->option('interface') === true){
257 |
258 | (new FileGenerator($interfacePath, $interfaceContents))->generate();
259 |
260 | $this->info("Created : {$interfacePath}");
261 | }
262 | } catch (\Exception $e) {
263 |
264 | $this->error("File : {$e->getMessage()}");
265 |
266 | return E_ERROR;
267 | }
268 |
269 | return 0;
270 | }
271 |
272 | }
273 |
--------------------------------------------------------------------------------
/src/Commands/CreateModuleServiceCommand.php:
--------------------------------------------------------------------------------
1 | argument('service'));
71 |
72 | if (Str::contains(strtolower($service), 'service') === false) {
73 | $service .= 'Service';
74 | }
75 |
76 | return $service;
77 | }
78 |
79 | /**
80 | * Return destination path for class file publish
81 | * getDestinationFilePath
82 | *
83 | * @return string
84 | */
85 | protected function getDestinationFilePath(): string
86 | {
87 | return base_path()."/Modules/{$this->argument('module')}"."/Services".'/'. $this->getServiceName() . '.php';
88 | }
89 |
90 |
91 | /**
92 | * Return only service class name
93 | * getServiceNameWithoutNamespace
94 | *
95 | * @return string
96 | */
97 | private function getServiceNameWithoutNamespace(): string
98 | {
99 | return class_basename($this->getServiceName());
100 | }
101 |
102 | /**
103 | * Set Default Namespace
104 | * Override CommandGenerator class method
105 | * getDefaultNamespace
106 | *
107 | * @return string
108 | */
109 | public function getDefaultNamespace() : string
110 | {
111 | return "Modules\\{$this->argument('module')}\\Services";
112 | }
113 |
114 |
115 | /**
116 | * Return stub file path
117 | * getStubFilePath
118 | *
119 | * @return string
120 | */
121 | protected function getStubFilePath(): string
122 | {
123 | return '/stubs/service.stub';
124 | }
125 |
126 |
127 | /**
128 | * Generate file content
129 | * getTemplateContents
130 | *
131 | * @return string
132 | */
133 | protected function getTemplateContents(): string
134 | {
135 | return (new GenerateFile(__DIR__.$this->getStubFilePath(), [
136 | 'CLASS_NAMESPACE' => $this->getClassNamespace(),
137 | 'CLASS' => $this->getServiceNameWithoutNamespace()
138 | ]))->render();
139 | }
140 |
141 | /**
142 | * Execute the console command.
143 | *
144 | * @return int
145 | */
146 | public function handle()
147 | {
148 | // Check this module exists or not.
149 | if ($this->checkModuleExists($this->argument('module')) === false) {
150 | $this->error(" Module [{$this->argument('module')}] does not exist!");
151 | return E_ERROR;
152 | exit;
153 | }
154 |
155 | $path = str_replace('\\', '/', $this->getDestinationFilePath());
156 |
157 | if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
158 | $this->laravel['files']->makeDirectory($dir, 0777, true);
159 | }
160 |
161 | $contents = $this->getTemplateContents();
162 |
163 | try {
164 | (new FileGenerator($path, $contents))->generate();
165 |
166 | $this->info("Created : {$path}");
167 | } catch (\Exception $e) {
168 |
169 | $this->error("File : {$e->getMessage()}");
170 |
171 | return E_ERROR;
172 | }
173 |
174 | return 0;
175 |
176 | }
177 |
178 | }
179 |
--------------------------------------------------------------------------------
/src/Commands/CreateModuleTraitCommand.php:
--------------------------------------------------------------------------------
1 | argument('trait'));
65 | }
66 |
67 | /**
68 | * getDestinationFilePath
69 | *
70 | * @return string
71 | */
72 | protected function getDestinationFilePath(): string
73 | {
74 | return base_path()."/Modules/{$this->argument('module')}"."/Traits".'/'. $this->getTraitName() . '.php';
75 | }
76 |
77 |
78 | /**
79 | * getTraitNameWithoutNamespace
80 | *
81 | * @return string
82 | */
83 | private function getTraitNameWithoutNamespace(): string
84 | {
85 | return class_basename($this->getTraitName());
86 | }
87 |
88 | /**
89 | * getDefaultNamespace
90 | *
91 | * @return string
92 | */
93 | public function getDefaultNamespace() : string
94 | {
95 | return "Modules\\{$this->argument('module')}\\Traits";
96 | }
97 |
98 |
99 | /**
100 | * getStubFilePath
101 | *
102 | * @return string
103 | */
104 | protected function getStubFilePath(): string
105 | {
106 | return '/stubs/traits.stub';
107 | }
108 |
109 | /**
110 | * getTemplateContents
111 | *
112 | * @return string
113 | */
114 | protected function getTemplateContents(): string
115 | {
116 | return (new GenerateFile(__DIR__.$this->getStubFilePath(), [
117 | 'CLASS_NAMESPACE' => $this->getClassNamespace(),
118 | 'CLASS' => $this->getTraitNameWithoutNamespace()
119 | ]))->render();
120 | }
121 |
122 | /**
123 | * Execute the console command.
124 | *
125 | * @return int
126 | */
127 | public function handle()
128 | {
129 | // Check this module exists or not.
130 | if ($this->checkModuleExists($this->argument('module')) === false) {
131 | $this->error(" Module [{$this->argument('module')}] does not exist!");
132 | return E_ERROR;
133 | exit;
134 | }
135 |
136 | $path = str_replace('\\', '/', $this->getDestinationFilePath());
137 |
138 |
139 | if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
140 | $this->laravel['files']->makeDirectory($dir, 0777, true);
141 | }
142 |
143 | $contents = $this->getTemplateContents();
144 |
145 | try {
146 | (new FileGenerator($path, $contents))->generate();
147 |
148 | $this->info("Created : {$path}");
149 | } catch (\Exception $e) {
150 |
151 | $this->error("File : {$e->getMessage()}");
152 |
153 | return E_ERROR;
154 | }
155 |
156 | return 0;
157 |
158 | }
159 | }
160 |
--------------------------------------------------------------------------------
/src/Commands/CreateRepositoryCommand.php:
--------------------------------------------------------------------------------
1 | argument('repository'));
82 |
83 | if (Str::contains(strtolower($repository), 'repository') === false) {
84 | $repository .= 'Repository';
85 | }
86 |
87 | return $repository;
88 | }
89 |
90 | /**
91 | * Replace App with empty string for resolve namespace
92 | *
93 | * @return string
94 | */
95 | private function resolveNamespace(): string
96 | {
97 | if (strpos($this->getServiceNamespaceFromConfig(), self::APP_PATH) === 0) {
98 | return str_replace(self::APP_PATH, '', $this->getServiceNamespaceFromConfig());
99 | }
100 | return '/' . $this->getServiceNamespaceFromConfig();
101 | }
102 |
103 | /**
104 | * Return destination path for class file publish
105 | * getDestinationFilePath
106 | *
107 | * @return string
108 | */
109 | protected function getDestinationFilePath(): string
110 | {
111 | return app_path() . $this->resolveNamespace() . '/Repositories' . '/' . $this->getRepositoryName() . '.php';
112 | }
113 |
114 | /**
115 | * Return Inference name for this repository class
116 | * getInterfaceName
117 | *
118 | * @return string
119 | */
120 | protected function getInterfaceName(): string
121 | {
122 | return $this->getRepositoryName() . "Interface";
123 | }
124 |
125 |
126 | /**
127 | * Return destination path for interface file publish
128 | * interfaceDestinationPath
129 | *
130 | * @return string
131 | */
132 | protected function interfaceDestinationPath(): string
133 | {
134 | return app_path() . $this->resolveNamespace() . "/Repositories/Interfaces" . '/' . $this->getInterfaceName() . '.php';
135 | }
136 |
137 |
138 | /**
139 | * Return only repository class name
140 | * getRepositoryNameWithoutNamespace
141 | *
142 | * @return string
143 | */
144 | private function getRepositoryNameWithoutNamespace(): string
145 | {
146 | return class_basename($this->getRepositoryName());
147 | }
148 |
149 | /**
150 | * Set Default Namespace
151 | * Override CommandGenerator class method
152 | * getDefaultNamespace
153 | *
154 | * @return string
155 | */
156 | public function getDefaultNamespace(): string
157 | {
158 | $configNamespace = $this->getRepositoryNamespaceFromConfig();
159 | return "$configNamespace\\Repositories";
160 | }
161 |
162 |
163 | /**
164 | * Return only repository interface name
165 | * getInterfaceNameWithoutNamespace
166 | *
167 | * @return string
168 | */
169 | private function getInterfaceNameWithoutNamespace(): string
170 | {
171 | return class_basename($this->getInterfaceName());
172 | }
173 |
174 | /**
175 | * Set Default interface Namespace
176 | * Override CommandGenerator class method
177 | * getDefaultInterfaceNamespace
178 | *
179 | * @return string
180 | */
181 | public function getDefaultInterfaceNamespace(): string
182 | {
183 | $configNamespace = $this->getRepositoryNamespaceFromConfig();
184 | return "$configNamespace\\Repositories\\Interfaces";
185 | }
186 |
187 |
188 | /**
189 | * Return stub file path
190 | * getStubFilePath
191 | *
192 | * @return string
193 | */
194 | protected function getStubFilePath(): string
195 | {
196 | if ($this->option('interface') === true) {
197 | $stub = '/stubs/repository-interface.stub';
198 | } else {
199 | $stub = '/stubs/repository.stub';
200 | }
201 |
202 | return $stub;
203 | }
204 |
205 |
206 | /**
207 | * Generate file content
208 | * getTemplateContents
209 | *
210 | * @return string
211 | */
212 | protected function getTemplateContents(): string
213 | {
214 | return (new GenerateFile(__DIR__ . $this->getStubFilePath(), [
215 | 'CLASS_NAMESPACE' => $this->getClassNamespace(),
216 | 'INTERFACE_NAMESPACE' => $this->getInterfaceNamespace() . '\\' . $this->getInterfaceNameWithoutNamespace(),
217 | 'CLASS' => $this->getRepositoryNameWithoutNamespace(),
218 | 'INTERFACE' => $this->getInterfaceNameWithoutNamespace()
219 | ]))->render();
220 | }
221 |
222 |
223 | /**
224 | * Generate interface file content
225 | * getInterfaceTemplateContents
226 | *
227 | * @return string
228 | */
229 | protected function getInterfaceTemplateContents(): string
230 | {
231 | return (new GenerateFile(__DIR__ . "/stubs/interface.stub", [
232 | 'CLASS_NAMESPACE' => $this->getInterfaceNamespace(),
233 | 'INTERFACE' => $this->getInterfaceNameWithoutNamespace()
234 | ]))->render();
235 | }
236 |
237 |
238 | /**
239 | * Execute the console command.
240 | *
241 | * @return int
242 | */
243 | public function handle()
244 | {
245 | $path = str_replace('\\', '/', $this->getDestinationFilePath());
246 |
247 | if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
248 | $this->laravel['files']->makeDirectory($dir, 0777, true);
249 | }
250 |
251 | $contents = $this->getTemplateContents();
252 |
253 | // For Interface
254 | if ($this->option('interface') == true) {
255 | $interfacePath = str_replace('\\', '/', $this->interfaceDestinationPath());
256 |
257 | if (!$this->laravel['files']->isDirectory($dir = dirname($interfacePath))) {
258 | $this->laravel['files']->makeDirectory($dir, 0777, true);
259 | }
260 |
261 | $interfaceContents = $this->getInterfaceTemplateContents();
262 | }
263 |
264 | try {
265 | (new FileGenerator($path, $contents))->generate();
266 |
267 | $this->info("Created : {$path}");
268 |
269 | // For Interface
270 | if ($this->option('interface') === true) {
271 |
272 | (new FileGenerator($interfacePath, $interfaceContents))->generate();
273 |
274 | $this->info("Created : {$interfacePath}");
275 | }
276 |
277 | } catch (\Exception $e) {
278 |
279 | $this->error("File : {$e->getMessage()}");
280 |
281 | return E_ERROR;
282 | }
283 |
284 | return 0;
285 |
286 | }
287 |
288 | }
289 |
--------------------------------------------------------------------------------
/src/Commands/CreateServiceCommand.php:
--------------------------------------------------------------------------------
1 | argument('service'));
70 |
71 | if (Str::contains(strtolower($service), 'service') === false) {
72 | $service .= 'Service';
73 | }
74 |
75 | return $service;
76 | }
77 |
78 | /**
79 | * Replace App with empty string for resolve namespace
80 | *
81 | * @return string
82 | */
83 | private function resolveNamespace(): string
84 | {
85 | if (strpos($this->getServiceNamespaceFromConfig(), self::APP_PATH) === 0) {
86 | return str_replace(self::APP_PATH, '', $this->getServiceNamespaceFromConfig());
87 | }
88 | return '/' . $this->getServiceNamespaceFromConfig();
89 | }
90 |
91 | /**
92 | * Return destination path for class file publish
93 | * getDestinationFilePath
94 | *
95 | * @return string
96 | */
97 | protected function getDestinationFilePath(): string
98 | {
99 | return app_path() . $this->resolveNamespace() .'/Services'.'/'. $this->getServiceName() . '.php';
100 | }
101 |
102 |
103 | /**
104 | * Return only service class name
105 | * getServiceNameWithoutNamespace
106 | *
107 | * @return string
108 | */
109 | private function getServiceNameWithoutNamespace(): string
110 | {
111 | return class_basename($this->getServiceName());
112 | }
113 |
114 | /**
115 | * Set Default Namespace
116 | * Override CommandGenerator class method
117 | * getDefaultNamespace
118 | *
119 | * @return string
120 | */
121 | public function getDefaultNamespace() : string
122 | {
123 | $configNamespace = $this->getServiceNamespaceFromConfig();
124 | return "$configNamespace\\Services";
125 | }
126 |
127 |
128 | /**
129 | * Return stub file path
130 | * getStubFilePath
131 | *
132 | * @return string
133 | */
134 | protected function getStubFilePath(): string
135 | {
136 | return '/stubs/service.stub';
137 | }
138 |
139 |
140 | /**
141 | * Generate file content
142 | * getTemplateContents
143 | *
144 | * @return string
145 | */
146 | protected function getTemplateContents(): string
147 | {
148 | return (new GenerateFile(__DIR__.$this->getStubFilePath(), [
149 | 'CLASS_NAMESPACE' => $this->getClassNamespace(),
150 | 'CLASS' => $this->getServiceNameWithoutNamespace()
151 | ]))->render();
152 | }
153 |
154 | /**
155 | * Execute the console command.
156 | *
157 | * @return int
158 | */
159 | public function handle()
160 | {
161 | $path = str_replace('\\', '/', $this->getDestinationFilePath());
162 |
163 | if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
164 | $this->laravel['files']->makeDirectory($dir, 0777, true);
165 | }
166 |
167 | $contents = $this->getTemplateContents();
168 |
169 | try {
170 | (new FileGenerator($path, $contents))->generate();
171 |
172 | $this->info("Created : {$path}");
173 | } catch (\Exception $e) {
174 |
175 | $this->error("File : {$e->getMessage()}");
176 |
177 | return E_ERROR;
178 | }
179 |
180 | return 0;
181 | }
182 |
183 | }
184 |
--------------------------------------------------------------------------------
/src/Commands/CreateTraitCommand.php:
--------------------------------------------------------------------------------
1 | argument('trait'));
63 | }
64 |
65 | /**
66 | * getDestinationFilePath
67 | *
68 | * @return string
69 | */
70 | protected function getDestinationFilePath(): string
71 | {
72 | return app_path()."/Traits".'/'. $this->getTraitName() . '.php';
73 | }
74 |
75 | /**
76 | * getTraitNameWithoutNamespace
77 | *
78 | * @return string
79 | */
80 | private function getTraitNameWithoutNamespace(): string
81 | {
82 | return class_basename($this->getTraitName());
83 | }
84 |
85 | /**
86 | * getDefaultNamespace
87 | *
88 | * @return string
89 | */
90 | public function getDefaultNamespace() : string
91 | {
92 | return "App\\Traits";
93 | }
94 |
95 | /**
96 | * getStubFilePath
97 | *
98 | * @return string
99 | */
100 | protected function getStubFilePath(): string
101 | {
102 | return '/stubs/traits.stub';
103 | }
104 |
105 | /**
106 | * getTemplateContents
107 | *
108 | * @return string
109 | */
110 | protected function getTemplateContents(): string
111 | {
112 | return (new GenerateFile(__DIR__.$this->getStubFilePath(), [
113 | 'CLASS_NAMESPACE' => $this->getClassNamespace(),
114 | 'CLASS' => $this->getTraitNameWithoutNamespace()
115 | ]))->render();
116 | }
117 |
118 | /**
119 | * Execute the console command.
120 | *
121 | * @return int
122 | */
123 | public function handle()
124 | {
125 | $path = str_replace('\\', '/', $this->getDestinationFilePath());
126 |
127 |
128 | if (!$this->laravel['files']->isDirectory($dir = dirname($path))) {
129 | $this->laravel['files']->makeDirectory($dir, 0777, true);
130 | }
131 |
132 | $contents = $this->getTemplateContents();
133 |
134 | try {
135 |
136 | (new FileGenerator($path, $contents))->generate();
137 |
138 | $this->info("Created : {$path}");
139 | } catch (\Exception $e) {
140 |
141 | $this->error("File : {$e->getMessage()}");
142 |
143 | return E_ERROR;
144 | }
145 |
146 | return 0;
147 |
148 | }
149 |
150 | }
151 |
--------------------------------------------------------------------------------
/src/Commands/stubs/blade.stub:
--------------------------------------------------------------------------------
1 | @extends('app')
2 |
3 | @push('css')
4 |
5 | @endpush
6 |
7 | @section('content')
8 |
9 | @endsection
10 |
11 |
12 | @push('js')
13 |
14 | @endpush
--------------------------------------------------------------------------------
/src/Commands/stubs/interface.stub:
--------------------------------------------------------------------------------
1 | commands([
29 | CreateRepositoryCommand::class,
30 | CreateTraitCommand::class,
31 | CreateServiceCommand::class,
32 | CreateBladeCommand::class,
33 | ClearLogCommand::class,
34 |
35 | // For nWidart/laravel-modules:
36 | CreateModuleRepositoryCommand::class,
37 | CreateModuleTraitCommand::class,
38 | CreateModuleServiceCommand::class,
39 | CreateModuleBladeCommand::class
40 | ]);
41 | }
42 |
43 | /**
44 | * Bootstrap services.
45 | *
46 | * @return void
47 | */
48 | public function boot()
49 | {
50 | $this->publishes([
51 | __DIR__ . '/../config/laravel-more-command.php' => config_path('laravel-more-command.php'),
52 | ], 'config');
53 | }
54 | }
55 |
--------------------------------------------------------------------------------
/src/Support/FileGenerator.php:
--------------------------------------------------------------------------------
1 | path = $path;
40 | $this->contents = $contents;
41 | $this->filesystem = $filesystem ?: new Filesystem();
42 | }
43 |
44 | /**
45 | * getContents
46 | *
47 | * @return void
48 | */
49 | public function getContents()
50 | {
51 | return $this->contents;
52 | }
53 |
54 | /**
55 | * setContents
56 | *
57 | * @param mixed $contents
58 | * @return void
59 | */
60 | public function setContents($contents)
61 | {
62 | $this->contents = $contents;
63 |
64 | return $this;
65 | }
66 |
67 | /**
68 | * getFilesystem
69 | *
70 | * @return void
71 | */
72 | public function getFilesystem()
73 | {
74 | return $this->filesystem;
75 | }
76 |
77 | /**
78 | * setFilesystem
79 | *
80 | * @param mixed $filesystem
81 | * @return void
82 | */
83 | public function setFilesystem(Filesystem $filesystem)
84 | {
85 | $this->filesystem = $filesystem;
86 |
87 | return $this;
88 | }
89 |
90 |
91 | /**
92 | * getPath
93 | *
94 | * @return void
95 | */
96 | public function getPath()
97 | {
98 | return $this->path;
99 | }
100 |
101 |
102 | /**
103 | * setPath
104 | *
105 | * @param mixed $path
106 | * @return void
107 | */
108 | public function setPath($path)
109 | {
110 | $this->path = $path;
111 |
112 | return $this;
113 | }
114 |
115 | /**
116 | * withFileOverwrite
117 | *
118 | * @param mixed $overwrite
119 | * @return FileGenerator
120 | */
121 | public function withFileOverwrite(bool $overwrite): FileGenerator
122 | {
123 | $this->overwriteFile = $overwrite;
124 |
125 | return $this;
126 | }
127 |
128 |
129 | /**
130 | * generate the file
131 | * generate
132 | *
133 | * @return void
134 | */
135 | public function generate()
136 | {
137 | $path = $this->getPath();
138 | if (!$this->filesystem->exists($path)) {
139 | return $this->filesystem->put($path, $this->getContents());
140 | }
141 | throw new \Exception('File already exists!');
142 | }
143 |
144 | }
145 |
--------------------------------------------------------------------------------
/src/Support/GenerateFile.php:
--------------------------------------------------------------------------------
1 | path = $path;
39 | $this->replaces = $replaces;
40 | }
41 |
42 | /**
43 | * getPath
44 | *
45 | * @return string
46 | */
47 | public function getPath(): string
48 | {
49 | return $this->path;
50 | }
51 |
52 |
53 | /**
54 | * Get replaced file content
55 | * getContents
56 | *
57 | * @return string
58 | */
59 | public function getContents(): string
60 | {
61 | $contents = file_get_contents($this->getPath());
62 |
63 | foreach ($this->replaces as $search => $replace) {
64 | $contents = str_replace('$' . strtoupper($search) . '$', $replace, $contents);
65 | }
66 |
67 | return $contents;
68 | }
69 |
70 |
71 | /**
72 | * return the replaced file content
73 | * render
74 | *
75 | * @return string
76 | */
77 | public function render(): string
78 | {
79 | return $this->getContents();
80 | }
81 |
82 | }
83 |
--------------------------------------------------------------------------------