diff --git a/README.md b/README.md new file mode 100644 index 0000000..a3e9d21 --- /dev/null +++ b/README.md @@ -0,0 +1,27 @@ +# Pilot SDK + +## Installation + +```bash +composer config repositories.pilot vcs "https://git.bluesquare.io/bluesquare/pilot-sdk.git" +composer require bluesquare/pilot-sdk +``` + +## Configuration + +### Laravel + +Ajouter la clé d'API Pilot au `.env` : + +```dotenv +PILOT_KEY=xxxxx +``` + +## Utilisation + +### Laravel + +```php +php artisan pilot:metric MyMetric +php artisan pilot:action MyAction +``` diff --git a/src/Laravel/Commands/MakeAction.php b/src/Laravel/Commands/MakeAction.php new file mode 100644 index 0000000..21c6c2e --- /dev/null +++ b/src/Laravel/Commands/MakeAction.php @@ -0,0 +1,47 @@ +hasArgument('name') + ? $this->argument('name') + : $this->ask('What is the name of the action?', 'MakeSmoothie'); + + $contents = file_get_contents(__DIR__.'/../stubs/action.php'); + $contents = str_replace('MakeSmoothie', $name, $contents); + $contents = str_replace('make-smoothie', Str::slug($name), $contents); + $path = app_path('Pilot/Actions/'.$name.'.php'); + + if (file_exists($path)) { + $this->error('Action already exists!'); + return; + } + + @mkdir(dirname($path), 0755, true); + file_put_contents($path, $contents); + $this->info('Action created successfully!'); + } +} diff --git a/src/Laravel/Commands/MakeMetric.php b/src/Laravel/Commands/MakeMetric.php new file mode 100644 index 0000000..118544b --- /dev/null +++ b/src/Laravel/Commands/MakeMetric.php @@ -0,0 +1,47 @@ +hasArgument('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::slug($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!'); + } +} diff --git a/src/Laravel/PilotServiceProvider.php b/src/Laravel/PilotServiceProvider.php index a848c0d..aaa77fb 100644 --- a/src/Laravel/PilotServiceProvider.php +++ b/src/Laravel/PilotServiceProvider.php @@ -35,6 +35,11 @@ class PilotServiceProvider extends ServiceProvider Route::any('api/pilot', function (Request $request) { return $this->handleRequest($request); }); + + $this->commands([ + \Bluesquare\Pilot\Laravel\Commands\MakeAction::class, + \Bluesquare\Pilot\Laravel\Commands\MakeMetric::class, + ]); } protected function registerPilot() @@ -49,6 +54,10 @@ class PilotServiceProvider extends ServiceProvider if (class_exists($class)) { $action = new $class; + + if (method_exists($action, 'form')) + $this->pilot->form($action->slug(), [$action, 'form']); + $this->pilot->action($action->slug(), [$action, 'handle']); } } diff --git a/src/Laravel/stubs/action.php b/src/Laravel/stubs/action.php new file mode 100644 index 0000000..ec3ae6f --- /dev/null +++ b/src/Laravel/stubs/action.php @@ -0,0 +1,44 @@ + [ + 'type' => 'text', + 'label' => 'Flavour', + 'required' => true, + ], + ]; + } + + /** + * Available params: + * @param $data array The form data + * @param $files array The uploaded files + * @param $entry mixed|null The Pilot entry ID to execute the action for + * @param $entries array The Pilot entries ID to execute the action for + */ + public function handle(array $data) + { + if ($data['flavour'] === 'mayonnaise') { + return $this->error('Please choose a better flavour'); + } + + // ... + + return $this->action( + type: 'info', + title: 'Smoothie', + message: 'Your smoothie is ready!' + ); + } +} diff --git a/src/Laravel/stubs/metric.php b/src/Laravel/stubs/metric.php new file mode 100644 index 0000000..af5f697 --- /dev/null +++ b/src/Laravel/stubs/metric.php @@ -0,0 +1,45 @@ + The Pilot entries ID to compute the metric for + */ + public function compute($entry = null) + { + // Availability + // return $this->monitor(true); + + // Progress bar + // return $this->progress(10, 100); + + // Doughnut chart + // return $this->partition([ + // ['label' => 'Banana', 'value' => 90], + // ['label' => 'Pineapple', 'value' => 89], + // ]); + + // Trend chart + // return $this->trend([ + // ['label' => 'Banana', 'value' => 90], + // ['label' => 'Pineapple', 'value' => 89], + // ]); + + // Simple value + // return $this->value(12); + + // Simple value with filters + return $this->filters([ + $this->filter('Banana', 'banana', fn () => $this->value(90, 10)), + $this->filter('Pineapple', 'pineapple', fn () => $this->value(89, 20)), + ]); + } +} diff --git a/src/Pilot.php b/src/Pilot.php index 024ead3..b6cc275 100644 --- a/src/Pilot.php +++ b/src/Pilot.php @@ -9,6 +9,7 @@ class Pilot */ protected $registry = [ 'action' => [], + 'form' => [], 'metric' => [], ]; @@ -24,6 +25,15 @@ class Pilot } } + public function form($slug, callable $function) + { + $this->register('form', $slug, function () use ($function) { + return [ + 'json' => $function(), + ]; + }); + } + public function action($slug, callable $function) { $this->register('action', $slug, $function);