From 1a16aa9b75723b09e0c10f9afa490f08bd149064 Mon Sep 17 00:00:00 2001 From: localusercamp Date: Mon, 1 Mar 2021 10:33:48 +0500 Subject: [PATCH] init --- composer.json | 34 ++++++ src/TMCommandsServiceProvider.php | 45 ++++++++ src/makes/MakeAction.php | 48 ++++++++ src/makes/MakeCollection.php | 103 ++++++++++++++++++ src/makes/MakeContract.php | 48 ++++++++ src/makes/MakeEntity.php | 48 ++++++++ src/makes/MakeInit.php | 83 ++++++++++++++ src/makes/MakeInterface.php | 48 ++++++++ src/makes/MakeTask.php | 48 ++++++++ src/makes/stubs/collection-model-bind.stub | 4 + src/makes/stubs/init/contracts/Action.stub | 10 ++ .../init/contracts/CustomCollection.stub | 12 ++ src/makes/stubs/init/contracts/Task.stub | 10 ++ src/makes/stubs/init/interfaces/IAction.stub | 8 ++ .../init/interfaces/ICustomCollection.stub | 8 ++ src/makes/stubs/init/interfaces/ITask.stub | 8 ++ src/makes/stubs/make-action.stub | 16 +++ src/makes/stubs/make-collection.stub | 10 ++ src/makes/stubs/make-contract.stub | 8 ++ src/makes/stubs/make-entity.stub | 8 ++ src/makes/stubs/make-interface.stub | 8 ++ src/makes/stubs/make-task.stub | 16 +++ 22 files changed, 631 insertions(+) create mode 100644 composer.json create mode 100644 src/TMCommandsServiceProvider.php create mode 100644 src/makes/MakeAction.php create mode 100644 src/makes/MakeCollection.php create mode 100644 src/makes/MakeContract.php create mode 100644 src/makes/MakeEntity.php create mode 100644 src/makes/MakeInit.php create mode 100644 src/makes/MakeInterface.php create mode 100644 src/makes/MakeTask.php create mode 100644 src/makes/stubs/collection-model-bind.stub create mode 100644 src/makes/stubs/init/contracts/Action.stub create mode 100644 src/makes/stubs/init/contracts/CustomCollection.stub create mode 100644 src/makes/stubs/init/contracts/Task.stub create mode 100644 src/makes/stubs/init/interfaces/IAction.stub create mode 100644 src/makes/stubs/init/interfaces/ICustomCollection.stub create mode 100644 src/makes/stubs/init/interfaces/ITask.stub create mode 100644 src/makes/stubs/make-action.stub create mode 100644 src/makes/stubs/make-collection.stub create mode 100644 src/makes/stubs/make-contract.stub create mode 100644 src/makes/stubs/make-entity.stub create mode 100644 src/makes/stubs/make-interface.stub create mode 100644 src/makes/stubs/make-task.stub diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..94e6623 --- /dev/null +++ b/composer.json @@ -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" + ] + } + } +} diff --git a/src/TMCommandsServiceProvider.php b/src/TMCommandsServiceProvider.php new file mode 100644 index 0000000..96880c2 --- /dev/null +++ b/src/TMCommandsServiceProvider.php @@ -0,0 +1,45 @@ +app->runningInConsole()) { + $this->commands([ + MakeContract::class, + MakeAction::class, + MakeTask::class, + MakeEntity::class, + MakeCollection::class, + MakeInterface::class, + MakeInit::class, + ]); + } + } +} diff --git a/src/makes/MakeAction.php b/src/makes/MakeAction.php new file mode 100644 index 0000000..909c73a --- /dev/null +++ b/src/makes/MakeAction.php @@ -0,0 +1,48 @@ +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(); + } +} diff --git a/src/makes/MakeContract.php b/src/makes/MakeContract.php new file mode 100644 index 0000000..424b60f --- /dev/null +++ b/src/makes/MakeContract.php @@ -0,0 +1,48 @@ + 'App\\Contracts', + 'interfaces' => 'App\\Interfaces', + ]; + + protected function getStub() {} + + protected function getArguments() + { + return []; + } + + /** + * Execute the console command. + */ + public function handle() + { + $directories = $this->files->directories($this->init_stubs_folder); + foreach ($directories as $directory) { + $stubs = $this->files->files($directory); + $dirname = $this->files->name($directory); + $namespace = $this->namespaces[$dirname]; + foreach ($stubs as $stub) { + $stub_path_name = $stub->getPathname(); + $stub_name = $this->files->name($stub_path_name); + $namespace_name = "$namespace\\$stub_name"; + $class_name = $this->qualifyClass($namespace_name); + $path = $this->getPath($class_name); + + if ((! $this->hasOption('force') || + ! $this->option('force')) && + $this->alreadyExists($namespace_name)) { + $this->error("$namespace_name already exists!"); + + return false; + } + + $this->makeDirectory($path); + + $this->files->put($path, $this->sortImports($this->buildClass($stub_path_name))); + + $this->info("$class_name generated successfully."); + } + } + } + + /** + * Build the class. + */ + protected function buildClass($path) + { + $stub = $this->files->get($path); + + return $stub; + } + + /** + * Get the console command options. + * + * @return array + */ + protected function getOptions() + { + return [ + new InputOption('force', null, InputOption::VALUE_NONE, 'Regenerate'), + ]; + } +} diff --git a/src/makes/MakeInterface.php b/src/makes/MakeInterface.php new file mode 100644 index 0000000..3a76c74 --- /dev/null +++ b/src/makes/MakeInterface.php @@ -0,0 +1,48 @@ +