-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d0c1020
commit 1a16aa9
Showing
22 changed files
with
631 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"name": "thread-media/commands", | ||
"description": "Laravel 8 artisan commands package.", | ||
"type": "artisan-commands", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "localusercamp", | ||
"email": "user062199@gmail.com" | ||
} | ||
], | ||
"require-dev": { | ||
"laravel/framework": "^8.0", | ||
"php": "^7.4", | ||
"illuminate/console": "^7|^8" | ||
}, | ||
"require": { | ||
"laravel/framework": "^8.0", | ||
"php": "^7.4", | ||
"illuminate/console": "^7|^8" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"TM\\Commands\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"laravel": { | ||
"providers": [ | ||
"TM\\Commands\\TMCommandsServiceProvider" | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
namespace TM\Commands; | ||
|
||
use Illuminate\Support\ServiceProvider; | ||
use TM\Commands\Makes\MakeContract; | ||
use TM\Commands\Makes\MakeAction; | ||
use TM\Commands\Makes\MakeTask; | ||
use TM\Commands\Makes\MakeEntity; | ||
use TM\Commands\Makes\MakeCollection; | ||
use TM\Commands\Makes\MakeInterface; | ||
use TM\Commands\Makes\MakeInit; | ||
|
||
class TMCommandsServiceProvider extends ServiceProvider | ||
{ | ||
/** | ||
* Register services. | ||
* | ||
* @return void | ||
*/ | ||
public function register() | ||
{ | ||
|
||
} | ||
|
||
/** | ||
* Bootstrap services. | ||
* | ||
* @return void | ||
*/ | ||
public function boot() | ||
{ | ||
if ($this->app->runningInConsole()) { | ||
$this->commands([ | ||
MakeContract::class, | ||
MakeAction::class, | ||
MakeTask::class, | ||
MakeEntity::class, | ||
MakeCollection::class, | ||
MakeInterface::class, | ||
MakeInit::class, | ||
]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace TM\Commands\Makes; | ||
|
||
use Illuminate\Console\GeneratorCommand; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
class MakeAction extends GeneratorCommand | ||
{ | ||
protected $name = 'make:action'; | ||
|
||
protected $type = 'Action'; | ||
|
||
protected $description = 'Creates a new action'; | ||
|
||
/** | ||
* Get the stub file for the generator. | ||
* | ||
* @return string | ||
*/ | ||
protected function getStub() : string | ||
{ | ||
return __DIR__ . '/stubs/make-action.stub'; | ||
} | ||
|
||
/** | ||
* Get the default namespace for the class. | ||
* | ||
* @param string $rootNamespace | ||
* @return string | ||
*/ | ||
protected function getDefaultNamespace($rootNamespace) | ||
{ | ||
return $rootNamespace . '\Actions'; | ||
} | ||
|
||
/** | ||
* Get the console command arguments. | ||
* | ||
* @return array | ||
*/ | ||
protected function getArguments() | ||
{ | ||
return [ | ||
['name', InputArgument::REQUIRED, 'The name of the action.'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace TM\Commands\Makes; | ||
|
||
use Illuminate\Console\GeneratorCommand; | ||
use Illuminate\Support\Str; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputOption; | ||
|
||
class MakeCollection extends GeneratorCommand | ||
{ | ||
protected $name = 'make:collection'; | ||
|
||
protected $type = 'Collection'; | ||
|
||
protected $description = 'Creates a new collection'; | ||
|
||
/** | ||
* Get the stub file for the generator. | ||
* | ||
* @return string | ||
*/ | ||
protected function getStub() : string | ||
{ | ||
return __DIR__ . '/stubs/make-collection.stub'; | ||
} | ||
|
||
/** | ||
* Get the default namespace for the class. | ||
* | ||
* @param string $rootNamespace | ||
* @return string | ||
*/ | ||
protected function getDefaultNamespace($rootNamespace) | ||
{ | ||
return $rootNamespace . '\Collections'; | ||
} | ||
|
||
/** | ||
* Get the console command arguments. | ||
* | ||
* @return array | ||
*/ | ||
protected function getArguments() | ||
{ | ||
return [ | ||
['name', InputArgument::REQUIRED, 'The name of the collection.'], | ||
]; | ||
} | ||
|
||
/** | ||
* Get the console command options. | ||
* | ||
* @return array | ||
*/ | ||
protected function getOptions() | ||
{ | ||
return [ | ||
new InputOption('model', 'm', InputOption::VALUE_REQUIRED, 'The name of the model to bind to'), | ||
]; | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
*/ | ||
public function handle() | ||
{ | ||
$models_namespace = 'App\\Models'; | ||
|
||
$model = $this->option('model'); | ||
$name = $this->argument('name'); | ||
$class = "{$models_namespace}\\{$model}"; | ||
$exists = class_exists($class); | ||
|
||
if ($exists) { | ||
$ds = DIRECTORY_SEPARATOR; | ||
$file_path = app_path("Models{$ds}{$model}.php"); | ||
|
||
$new_collection_exists = strpos(file_get_contents($file_path), 'function newCollection(') !== false; | ||
|
||
if ($new_collection_exists) { | ||
$this->warn(' The newCollection method already exists.'); | ||
} | ||
else { | ||
$search = '}'; | ||
$stub = file_get_contents(__DIR__ . "{$ds}stubs{$ds}collection-model-bind.stub"); | ||
$insert = str_replace(['{{ class }}', '{{class}}'], $name, $stub); | ||
$replace = "\n{$insert}\n{$search}"; | ||
file_put_contents($file_path, Str::replaceLast($search, $replace, file_get_contents($file_path))); | ||
|
||
$search = "namespace {$models_namespace};"; | ||
$namespace = $this->getDefaultNamespace(trim($this->rootNamespace(), '\\')); | ||
$insert = "use $namespace\\$name;"; | ||
$replace = "{$search}\n\n{$insert}"; | ||
file_put_contents($file_path, Str::replaceLast($search, $replace, file_get_contents($file_path))); | ||
} | ||
} | ||
else { | ||
$this->error(" model $model not found!"); | ||
} | ||
return parent::handle(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace TM\Commands\Makes; | ||
|
||
use Illuminate\Console\GeneratorCommand; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
class MakeContract extends GeneratorCommand | ||
{ | ||
protected $name = 'make:contract'; | ||
|
||
protected $type = 'Contract'; | ||
|
||
protected $description = 'Creates a new contract'; | ||
|
||
/** | ||
* Get the stub file for the generator. | ||
* | ||
* @return string | ||
*/ | ||
protected function getStub() : string | ||
{ | ||
return __DIR__ . '/stubs/make-contract.stub'; | ||
} | ||
|
||
/** | ||
* Get the default namespace for the class. | ||
* | ||
* @param string $rootNamespace | ||
* @return string | ||
*/ | ||
protected function getDefaultNamespace($rootNamespace) | ||
{ | ||
return $rootNamespace . '\Contracts'; | ||
} | ||
|
||
/** | ||
* Get the console command arguments. | ||
* | ||
* @return array | ||
*/ | ||
protected function getArguments() | ||
{ | ||
return [ | ||
['name', InputArgument::REQUIRED, 'The name of the contract.'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace TM\Commands\Makes; | ||
|
||
use Illuminate\Console\GeneratorCommand; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
|
||
class MakeEntity extends GeneratorCommand | ||
{ | ||
protected $name = 'make:entity'; | ||
|
||
protected $type = 'Entity'; | ||
|
||
protected $description = 'Creates a new entity'; | ||
|
||
/** | ||
* Get the stub file for the generator. | ||
* | ||
* @return string | ||
*/ | ||
protected function getStub() : string | ||
{ | ||
return __DIR__ . '/stubs/make-entity.stub'; | ||
} | ||
|
||
/** | ||
* Get the default namespace for the class. | ||
* | ||
* @param string $rootNamespace | ||
* @return string | ||
*/ | ||
protected function getDefaultNamespace($rootNamespace) | ||
{ | ||
return $rootNamespace . '\Entities'; | ||
} | ||
|
||
/** | ||
* Get the console command arguments. | ||
* | ||
* @return array | ||
*/ | ||
protected function getArguments() | ||
{ | ||
return [ | ||
['name', InputArgument::REQUIRED, 'The name of the entity.'], | ||
]; | ||
} | ||
} |
Oops, something went wrong.