Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
localusercamp committed Mar 1, 2021
1 parent d0c1020 commit 1a16aa9
Show file tree
Hide file tree
Showing 22 changed files with 631 additions and 0 deletions.
34 changes: 34 additions & 0 deletions composer.json
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"
]
}
}
}
45 changes: 45 additions & 0 deletions src/TMCommandsServiceProvider.php
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,
]);
}
}
}
48 changes: 48 additions & 0 deletions src/makes/MakeAction.php
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.'],
];
}
}
103 changes: 103 additions & 0 deletions src/makes/MakeCollection.php
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();
}
}
48 changes: 48 additions & 0 deletions src/makes/MakeContract.php
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.'],
];
}
}
48 changes: 48 additions & 0 deletions src/makes/MakeEntity.php
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.'],
];
}
}
Loading

0 comments on commit 1a16aa9

Please sign in to comment.