Skip to content

Commit

Permalink
Adding a stubs file for the make command
Browse files Browse the repository at this point in the history
  • Loading branch information
leganz committed Jun 26, 2020
1 parent a1a5bc0 commit 3b14549
Show file tree
Hide file tree
Showing 3 changed files with 159 additions and 9 deletions.
1 change: 1 addition & 0 deletions src/ArdorServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public function boot()
// Commands
if ($this->app->runningInConsole()) {
$this->commands([
\AMBERSIVE\Ardor\Console\Commands\ArdorMakeClass::class,
\AMBERSIVE\Ardor\Console\Commands\ArdorRunBundler::class,
\AMBERSIVE\Ardor\Console\Commands\ArdorRunContracts::class,
]);
Expand Down
129 changes: 120 additions & 9 deletions src/Console/Commands/ArdorMakeClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,21 @@

use Carbon\Carbon;

class ArdorMakeClass extends Command
use Illuminate\Console\GeneratorCommand;

class ArdorMakeClass extends GeneratorCommand
{

private String $classType;

private array $allowed = ['bundler'];

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'ardor:make {name} {--type= : bundler|contract}';
protected $signature = 'ardor:make {name} {--type= : bundler}';

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

/**
* Create a new command instance.
* Execute the console command.
*
* @return void
* @return mixed
*/
public function __construct()
public function handle()
{
parent::__construct();

$name = $this->qualifyClass($this->getNameInput());

if (! $this->hasOption('type') || $this->option('type') === null || !in_array($this->option('type'), $this->allowed)) {
$this->error("A valid type must be provied!");
return;
}

$this->classType = $this->option('type');
$path = $this->getPath($name);

if ((! $this->hasOption('force') ||
! $this->option('force')) &&
$this->alreadyExists($this->getNameInput())) {
$this->error($this->type.' already exists!');

return false;
}

$this->makeDirectory($path);

$this->files->put($path, $this->sortImports($this->buildClassCustom($name, $this->classType)));

$this->info($this->type.' created successfully (data class and blade file).');

}

/**
* Execute the console command.
* Build the class with the given name.
*
* @return mixed
* @param string $name
* @return string
*
* @throws \Illuminate\Contracts\Filesystem\FileNotFoundException
*/
public function handle()
protected function buildClassCustom(String $name, String $stubname)
{
$stub = $this->files->get($this->getStubFilePath($stubname));

return $this->replaceNamespace($stub, $name)->replaceClass($stub, $name);
}

/**
* Get the root namespace for the class.
*
* @return string
*/
protected function rootNamespace():String
{
return $this->laravel->getNamespace()."Bundlers";
}

/**
* Returns the path to the stubs folder
*/
protected function getStub(): String {
return __DIR__."/../../Stubs/";
}

/**
* Get the stub file for the generator.
*
* @return string
*/
protected function getStubFilePath(String $stubname):String
{
return $this->getStub()."${stubname}.stub";
}

/**
* Returns the path for the document class
*
* @param mixed $name
* @return String
*/
protected function getPath($name):String {
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
$type = ucfirst(Str::plural($this->classType));
return $this->getPathFolder($name, "app/${type}");
}



/**
* Returns the base path for the file
*
* @param mixed $name
* @param mixed $folder
* @return String
*/
protected function getPathFolder(String $name, String $folder = ''): String {
$name = Str::replaceFirst($this->rootNamespace(), '', $name);
$type = ucfirst($this->classType);
return base_path($folder.str_replace('\\', '/', $name."${type}").".php");
}

/**
* Replace the namespace for the given stub.
*
* @param string $stub
* @param string $name
* @return $this
*/
protected function replaceNamespace(&$stub, $name)
{

$bladename = strtolower(str_replace("\\", ".", str_replace($this->rootNamespace()."\\", "printables.", $name)));

$stub = str_replace(
['DummyNamespace', 'DummyRootNamespace', 'NamespacedDummyUserModel', 'DummyBladeName'],
[$this->getNamespace($name), $this->rootNamespace(), $this->userProviderModel()],
$stub
);

return $this;
}

}
38 changes: 38 additions & 0 deletions src/Stubs/bundler.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace DummyNamespace;

use AMBERSIVE\Ardor\Interfaces\ArdorBundlerInterface;


use AMBERSIVE\Ardor\Classes\ArdorBundler;
use AMBERSIVE\Ardor\Models\ArdorNode;
use AMBERSIVE\Ardor\Models\ArdorTransactionJson;

class DummyClassBundler implements ArdorBundlerInterface {

public array $config = [];
public ArdorNode $ardorNode;
public ArdorBundler $ardorBundler;

public function __construct(array $config = []){

$this->config = $config;
$this->ardorNode = new ArdorNode(data_get($config, 'nodeUrl', null));
$this->ardorBundler = new ArdorBundler($this->ardorNode);

}

/**
* Main entry point for the bundler
*
* @param mixed $transaction
* @return bool
*/
public function run(ArdorTransactionJson $transaction):bool {

return false;

}

}

0 comments on commit 3b14549

Please sign in to comment.