Files
pilot-sdk/src/Laravel/Commands/MakeMetric.php
2024-11-29 16:57:06 +01:00

48 lines
1.2 KiB
PHP

<?php
namespace Bluesquare\Pilot\Laravel\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Str;
class MakeMetric extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'pilot:metric {name?}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Generate files for a Bluesquare Pilot metric';
/**
* Execute the console command.
*/
public function handle()
{
$name = ! empty($this->argument('name'))
? $this->argument('name')
: $this->ask('What is the name of the metric?', 'SmoothiesCount');
$contents = file_get_contents(__DIR__.'/../stubs/metric.php');
$contents = str_replace('SmoothiesCount', $name, $contents);
$contents = str_replace('smoothies-count', Str::snake($name, '-'), $contents);
$path = app_path('Pilot/Metrics/'.$name.'.php');
if (file_exists($path)) {
$this->error('Metric already exists!');
return;
}
@mkdir(dirname($path), 0755, true);
file_put_contents($path, $contents);
$this->info('Metric created successfully!');
}
}