Skip to content

Commit 1a16aa9

Browse files
committed
init
1 parent d0c1020 commit 1a16aa9

22 files changed

+631
-0
lines changed

composer.json

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
{
2+
"name": "thread-media/commands",
3+
"description": "Laravel 8 artisan commands package.",
4+
"type": "artisan-commands",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "localusercamp",
9+
"email": "user062199@gmail.com"
10+
}
11+
],
12+
"require-dev": {
13+
"laravel/framework": "^8.0",
14+
"php": "^7.4",
15+
"illuminate/console": "^7|^8"
16+
},
17+
"require": {
18+
"laravel/framework": "^8.0",
19+
"php": "^7.4",
20+
"illuminate/console": "^7|^8"
21+
},
22+
"autoload": {
23+
"psr-4": {
24+
"TM\\Commands\\": "src/"
25+
}
26+
},
27+
"extra": {
28+
"laravel": {
29+
"providers": [
30+
"TM\\Commands\\TMCommandsServiceProvider"
31+
]
32+
}
33+
}
34+
}

src/TMCommandsServiceProvider.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace TM\Commands;
4+
5+
use Illuminate\Support\ServiceProvider;
6+
use TM\Commands\Makes\MakeContract;
7+
use TM\Commands\Makes\MakeAction;
8+
use TM\Commands\Makes\MakeTask;
9+
use TM\Commands\Makes\MakeEntity;
10+
use TM\Commands\Makes\MakeCollection;
11+
use TM\Commands\Makes\MakeInterface;
12+
use TM\Commands\Makes\MakeInit;
13+
14+
class TMCommandsServiceProvider extends ServiceProvider
15+
{
16+
/**
17+
* Register services.
18+
*
19+
* @return void
20+
*/
21+
public function register()
22+
{
23+
24+
}
25+
26+
/**
27+
* Bootstrap services.
28+
*
29+
* @return void
30+
*/
31+
public function boot()
32+
{
33+
if ($this->app->runningInConsole()) {
34+
$this->commands([
35+
MakeContract::class,
36+
MakeAction::class,
37+
MakeTask::class,
38+
MakeEntity::class,
39+
MakeCollection::class,
40+
MakeInterface::class,
41+
MakeInit::class,
42+
]);
43+
}
44+
}
45+
}

src/makes/MakeAction.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace TM\Commands\Makes;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Symfony\Component\Console\Input\InputArgument;
7+
8+
class MakeAction extends GeneratorCommand
9+
{
10+
protected $name = 'make:action';
11+
12+
protected $type = 'Action';
13+
14+
protected $description = 'Creates a new action';
15+
16+
/**
17+
* Get the stub file for the generator.
18+
*
19+
* @return string
20+
*/
21+
protected function getStub() : string
22+
{
23+
return __DIR__ . '/stubs/make-action.stub';
24+
}
25+
26+
/**
27+
* Get the default namespace for the class.
28+
*
29+
* @param string $rootNamespace
30+
* @return string
31+
*/
32+
protected function getDefaultNamespace($rootNamespace)
33+
{
34+
return $rootNamespace . '\Actions';
35+
}
36+
37+
/**
38+
* Get the console command arguments.
39+
*
40+
* @return array
41+
*/
42+
protected function getArguments()
43+
{
44+
return [
45+
['name', InputArgument::REQUIRED, 'The name of the action.'],
46+
];
47+
}
48+
}

src/makes/MakeCollection.php

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
<?php
2+
3+
namespace TM\Commands\Makes;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Illuminate\Support\Str;
7+
use Symfony\Component\Console\Input\InputArgument;
8+
use Symfony\Component\Console\Input\InputOption;
9+
10+
class MakeCollection extends GeneratorCommand
11+
{
12+
protected $name = 'make:collection';
13+
14+
protected $type = 'Collection';
15+
16+
protected $description = 'Creates a new collection';
17+
18+
/**
19+
* Get the stub file for the generator.
20+
*
21+
* @return string
22+
*/
23+
protected function getStub() : string
24+
{
25+
return __DIR__ . '/stubs/make-collection.stub';
26+
}
27+
28+
/**
29+
* Get the default namespace for the class.
30+
*
31+
* @param string $rootNamespace
32+
* @return string
33+
*/
34+
protected function getDefaultNamespace($rootNamespace)
35+
{
36+
return $rootNamespace . '\Collections';
37+
}
38+
39+
/**
40+
* Get the console command arguments.
41+
*
42+
* @return array
43+
*/
44+
protected function getArguments()
45+
{
46+
return [
47+
['name', InputArgument::REQUIRED, 'The name of the collection.'],
48+
];
49+
}
50+
51+
/**
52+
* Get the console command options.
53+
*
54+
* @return array
55+
*/
56+
protected function getOptions()
57+
{
58+
return [
59+
new InputOption('model', 'm', InputOption::VALUE_REQUIRED, 'The name of the model to bind to'),
60+
];
61+
}
62+
63+
/**
64+
* Execute the console command.
65+
*/
66+
public function handle()
67+
{
68+
$models_namespace = 'App\\Models';
69+
70+
$model = $this->option('model');
71+
$name = $this->argument('name');
72+
$class = "{$models_namespace}\\{$model}";
73+
$exists = class_exists($class);
74+
75+
if ($exists) {
76+
$ds = DIRECTORY_SEPARATOR;
77+
$file_path = app_path("Models{$ds}{$model}.php");
78+
79+
$new_collection_exists = strpos(file_get_contents($file_path), 'function newCollection(') !== false;
80+
81+
if ($new_collection_exists) {
82+
$this->warn(' The newCollection method already exists.');
83+
}
84+
else {
85+
$search = '}';
86+
$stub = file_get_contents(__DIR__ . "{$ds}stubs{$ds}collection-model-bind.stub");
87+
$insert = str_replace(['{{ class }}', '{{class}}'], $name, $stub);
88+
$replace = "\n{$insert}\n{$search}";
89+
file_put_contents($file_path, Str::replaceLast($search, $replace, file_get_contents($file_path)));
90+
91+
$search = "namespace {$models_namespace};";
92+
$namespace = $this->getDefaultNamespace(trim($this->rootNamespace(), '\\'));
93+
$insert = "use $namespace\\$name;";
94+
$replace = "{$search}\n\n{$insert}";
95+
file_put_contents($file_path, Str::replaceLast($search, $replace, file_get_contents($file_path)));
96+
}
97+
}
98+
else {
99+
$this->error(" model $model not found!");
100+
}
101+
return parent::handle();
102+
}
103+
}

src/makes/MakeContract.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace TM\Commands\Makes;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Symfony\Component\Console\Input\InputArgument;
7+
8+
class MakeContract extends GeneratorCommand
9+
{
10+
protected $name = 'make:contract';
11+
12+
protected $type = 'Contract';
13+
14+
protected $description = 'Creates a new contract';
15+
16+
/**
17+
* Get the stub file for the generator.
18+
*
19+
* @return string
20+
*/
21+
protected function getStub() : string
22+
{
23+
return __DIR__ . '/stubs/make-contract.stub';
24+
}
25+
26+
/**
27+
* Get the default namespace for the class.
28+
*
29+
* @param string $rootNamespace
30+
* @return string
31+
*/
32+
protected function getDefaultNamespace($rootNamespace)
33+
{
34+
return $rootNamespace . '\Contracts';
35+
}
36+
37+
/**
38+
* Get the console command arguments.
39+
*
40+
* @return array
41+
*/
42+
protected function getArguments()
43+
{
44+
return [
45+
['name', InputArgument::REQUIRED, 'The name of the contract.'],
46+
];
47+
}
48+
}

src/makes/MakeEntity.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace TM\Commands\Makes;
4+
5+
use Illuminate\Console\GeneratorCommand;
6+
use Symfony\Component\Console\Input\InputArgument;
7+
8+
class MakeEntity extends GeneratorCommand
9+
{
10+
protected $name = 'make:entity';
11+
12+
protected $type = 'Entity';
13+
14+
protected $description = 'Creates a new entity';
15+
16+
/**
17+
* Get the stub file for the generator.
18+
*
19+
* @return string
20+
*/
21+
protected function getStub() : string
22+
{
23+
return __DIR__ . '/stubs/make-entity.stub';
24+
}
25+
26+
/**
27+
* Get the default namespace for the class.
28+
*
29+
* @param string $rootNamespace
30+
* @return string
31+
*/
32+
protected function getDefaultNamespace($rootNamespace)
33+
{
34+
return $rootNamespace . '\Entities';
35+
}
36+
37+
/**
38+
* Get the console command arguments.
39+
*
40+
* @return array
41+
*/
42+
protected function getArguments()
43+
{
44+
return [
45+
['name', InputArgument::REQUIRED, 'The name of the entity.'],
46+
];
47+
}
48+
}

0 commit comments

Comments
 (0)