Skip to content

Commit 3b14549

Browse files
committed
Adding a stubs file for the make command
1 parent a1a5bc0 commit 3b14549

File tree

3 files changed

+159
-9
lines changed

3 files changed

+159
-9
lines changed

src/ArdorServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public function boot()
3636
// Commands
3737
if ($this->app->runningInConsole()) {
3838
$this->commands([
39+
\AMBERSIVE\Ardor\Console\Commands\ArdorMakeClass::class,
3940
\AMBERSIVE\Ardor\Console\Commands\ArdorRunBundler::class,
4041
\AMBERSIVE\Ardor\Console\Commands\ArdorRunContracts::class,
4142
]);

src/Console/Commands/ArdorMakeClass.php

Lines changed: 120 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,21 @@
99

1010
use Carbon\Carbon;
1111

12-
class ArdorMakeClass extends Command
12+
use Illuminate\Console\GeneratorCommand;
13+
14+
class ArdorMakeClass extends GeneratorCommand
1315
{
16+
17+
private String $classType;
18+
19+
private array $allowed = ['bundler'];
20+
1421
/**
1522
* The name and signature of the console command.
1623
*
1724
* @var string
1825
*/
19-
protected $signature = 'ardor:make {name} {--type= : bundler|contract}';
26+
protected $signature = 'ardor:make {name} {--type= : bundler}';
2027

2128
/**
2229
* The console command description.
@@ -26,23 +33,127 @@ class ArdorMakeClass extends Command
2633
protected $description = 'This command will run all your bundler classes.';
2734

2835
/**
29-
* Create a new command instance.
36+
* Execute the console command.
3037
*
31-
* @return void
38+
* @return mixed
3239
*/
33-
public function __construct()
40+
public function handle()
3441
{
35-
parent::__construct();
42+
43+
$name = $this->qualifyClass($this->getNameInput());
44+
45+
if (! $this->hasOption('type') || $this->option('type') === null || !in_array($this->option('type'), $this->allowed)) {
46+
$this->error("A valid type must be provied!");
47+
return;
48+
}
49+
50+
$this->classType = $this->option('type');
51+
$path = $this->getPath($name);
52+
53+
if ((! $this->hasOption('force') ||
54+
! $this->option('force')) &&
55+
$this->alreadyExists($this->getNameInput())) {
56+
$this->error($this->type.' already exists!');
57+
58+
return false;
59+
}
60+
61+
$this->makeDirectory($path);
62+
63+
$this->files->put($path, $this->sortImports($this->buildClassCustom($name, $this->classType)));
64+
65+
$this->info($this->type.' created successfully (data class and blade file).');
66+
3667
}
3768

3869
/**
39-
* Execute the console command.
70+
* Build the class with the given name.
4071
*
41-
* @return mixed
72+
* @param string $name
73+
* @return string
74+
*
75+
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
4276
*/
43-
public function handle()
77+
protected function buildClassCustom(String $name, String $stubname)
78+
{
79+
$stub = $this->files->get($this->getStubFilePath($stubname));
80+
81+
return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
82+
}
83+
84+
/**
85+
* Get the root namespace for the class.
86+
*
87+
* @return string
88+
*/
89+
protected function rootNamespace():String
90+
{
91+
return $this->laravel->getNamespace()."Bundlers";
92+
}
93+
94+
/**
95+
* Returns the path to the stubs folder
96+
*/
97+
protected function getStub(): String {
98+
return __DIR__."/../../Stubs/";
99+
}
100+
101+
/**
102+
* Get the stub file for the generator.
103+
*
104+
* @return string
105+
*/
106+
protected function getStubFilePath(String $stubname):String
44107
{
108+
return $this->getStub()."${stubname}.stub";
109+
}
45110

111+
/**
112+
* Returns the path for the document class
113+
*
114+
* @param mixed $name
115+
* @return String
116+
*/
117+
protected function getPath($name):String {
118+
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
119+
$type = ucfirst(Str::plural($this->classType));
120+
return $this->getPathFolder($name, "app/${type}");
121+
}
122+
123+
124+
125+
/**
126+
* Returns the base path for the file
127+
*
128+
* @param mixed $name
129+
* @param mixed $folder
130+
* @return String
131+
*/
132+
protected function getPathFolder(String $name, String $folder = ''): String {
133+
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
134+
$type = ucfirst($this->classType);
135+
return base_path($folder.str_replace('\\', '/', $name."${type}").".php");
136+
}
137+
138+
/**
139+
* Replace the namespace for the given stub.
140+
*
141+
* @param string $stub
142+
* @param string $name
143+
* @return $this
144+
*/
145+
protected function replaceNamespace(&$stub, $name)
146+
{
147+
148+
$bladename = strtolower(str_replace("\\", ".", str_replace($this->rootNamespace()."\\", "printables.", $name)));
149+
150+
$stub = str_replace(
151+
['DummyNamespace', 'DummyRootNamespace', 'NamespacedDummyUserModel', 'DummyBladeName'],
152+
[$this->getNamespace($name), $this->rootNamespace(), $this->userProviderModel()],
153+
$stub
154+
);
155+
156+
return $this;
46157
}
47158

48159
}

src/Stubs/bundler.stub

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use AMBERSIVE\Ardor\Interfaces\ArdorBundlerInterface;
6+
7+
8+
use AMBERSIVE\Ardor\Classes\ArdorBundler;
9+
use AMBERSIVE\Ardor\Models\ArdorNode;
10+
use AMBERSIVE\Ardor\Models\ArdorTransactionJson;
11+
12+
class DummyClassBundler implements ArdorBundlerInterface {
13+
14+
public array $config = [];
15+
public ArdorNode $ardorNode;
16+
public ArdorBundler $ardorBundler;
17+
18+
public function __construct(array $config = []){
19+
20+
$this->config = $config;
21+
$this->ardorNode = new ArdorNode(data_get($config, 'nodeUrl', null));
22+
$this->ardorBundler = new ArdorBundler($this->ardorNode);
23+
24+
}
25+
26+
/**
27+
* Main entry point for the bundler
28+
*
29+
* @param mixed $transaction
30+
* @return bool
31+
*/
32+
public function run(ArdorTransactionJson $transaction):bool {
33+
34+
return false;
35+
36+
}
37+
38+
}

0 commit comments

Comments
 (0)