Skip to content

Commit c187fdb

Browse files
committed
add make trait command
1 parent b37735f commit c187fdb

File tree

3 files changed

+101
-0
lines changed

3 files changed

+101
-0
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Guysolamour\Command\Console\Commands\Traits;
4+
5+
6+
use Guysolamour\Command\Console\Commands\BaseCommand;
7+
8+
class CreateTraitCommand extends BaseCommand
9+
{
10+
/**
11+
* The name and signature of the console command.
12+
*
13+
* @var string
14+
*/
15+
protected $signature = 'cmd:make:trait {name}
16+
{--f|folder=Traits : folder name inside app directory }
17+
';
18+
19+
20+
/**
21+
* The console command description.
22+
*
23+
* @var string
24+
*/
25+
protected $description = 'Create trait';
26+
27+
28+
/**
29+
* @var string
30+
*/
31+
protected $name;
32+
33+
34+
/**
35+
* @var string
36+
*/
37+
protected $folder;
38+
39+
40+
41+
42+
public function __construct()
43+
{
44+
parent::__construct();
45+
$this->provider_path = app_path('Providers/HelperServiceProvider.php');
46+
}
47+
48+
49+
50+
public function handle()
51+
{
52+
$this->info('Initiating...');
53+
54+
$this->name = ucfirst($this->argument('name'));
55+
56+
$this->folder = ucfirst($this->option('folder'));
57+
58+
$this->loadTrait();
59+
}
60+
61+
62+
public function loadTrait()
63+
{
64+
$traits_path = app_path("{$this->folder}/{$this->name}.php");
65+
66+
if ($this->filesystem->exists($traits_path)) {
67+
$this->error("The [{$this->name}] trait already exists");
68+
return;
69+
}
70+
71+
$trait_stub = $this->filesystem->get($this->template_path . '/trait/trait.stub');
72+
73+
$this->compliedAndWriteFile(
74+
$trait_stub,
75+
$traits_path,
76+
);
77+
78+
$this->info("{$this->name} trait created at " . $traits_path);
79+
}
80+
81+
82+
83+
protected function parseName($name = null): array
84+
{
85+
return array_merge(parent::parseName(), [
86+
'{{traitName}}' => $this->name,
87+
'{{traitsFolder}}' => $this->folder,
88+
]);
89+
}
90+
}

src/ServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Guysolamour\Command\Console\Commands\Database\DropDatabase;
88
use Guysolamour\Command\Console\Commands\Database\CreateDatabase;
99
use Guysolamour\Command\Console\Commands\Helper\CreateHelperCommand;
10+
use Guysolamour\Command\Console\Commands\Traits\CreateTraitCommand;
1011
use Guysolamour\Command\Console\Commands\Provider\CreateProviderCommand;
1112

1213
class ServiceProvider extends \Illuminate\Support\ServiceProvider
@@ -27,6 +28,7 @@ public function boot()
2728
CreateEntity::class,
2829
CreateProviderCommand::class,
2930
CreateHelperCommand::class,
31+
CreateTraitCommand::class,
3032
]);
3133
}
3234
}

src/templates/trait/trait.stub

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace {{namespace}}\{{traitsFolder}};
4+
5+
6+
trait {{traitName}}
7+
{
8+
9+
}

0 commit comments

Comments
 (0)