Skip to content

Commit

Permalink
achievements:load command
Browse files Browse the repository at this point in the history
  • Loading branch information
assada committed Mar 6, 2020
1 parent 2e904e0 commit 02c6406
Show file tree
Hide file tree
Showing 2 changed files with 118 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/AchievementsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use Assada\Achievements\Console\AchievementChainMakeCommand;
use Assada\Achievements\Console\AchievementMakeCommand;
use Assada\Achievements\Console\LoadAchievementsCommand;
use Illuminate\Support\ServiceProvider;

/**
Expand All @@ -23,7 +24,13 @@ public function boot(): void
{
$this->loadMigrationsFrom(__DIR__ . '/Migrations');
if ($this->app->runningInConsole()) {
$this->commands([AchievementMakeCommand::class, AchievementChainMakeCommand::class]);
$this->commands(
[
AchievementMakeCommand::class,
AchievementChainMakeCommand::class,
LoadAchievementsCommand::class
]
);
}
$this->app[Achievement::class] = static function ($app) {
return $app['gstt.achievements.achievement'];
Expand Down
110 changes: 110 additions & 0 deletions src/Console/LoadAchievementsCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
declare(strict_types=1);

namespace Assada\Achievements\Console;

use Assada\Achievements\Achievement;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Illuminate\Support\Str;

/**
* Class LoadAchievements
*
* @package Assada\Achievements\Console
*/
class LoadAchievementsCommand extends Command
{
use ConfirmableTrait;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'achievements:load {--force : Force the operation to run when in production}
{--path=* : The path(s) to the achievements files to be executed}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Load all Achievements to database';

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
if (!$this->confirmToProceed()) {
return;
}

$paths = $this->option('path');

if (empty($paths)) {
$paths = [
'app/Achievements'
];
}

$classes = [];

foreach ($paths as $path) {
$this->info(sprintf('Load classes in %s...', $path));

$files = array_diff(scandir($path, SCANDIR_SORT_ASCENDING), array('.', '..'));

foreach ($files as $file) {
if (!Str::endsWith($file, '.php')) {
$this->warn(sprintf('File %s in %s not an php file', $file, $path));
continue;
}

$classes[] = [
'name' => Str::before($file, '.php'),
'namespace' => $this->getNamespace(file_get_contents($path . '/' . $file))
];
}

$this->info(sprintf('Found %d classes. Instantiating...', count($classes)));
}

/** @var Achievement[] $objects */
$objects = [];

foreach ($classes as $class) {
$fullClass = sprintf('%s\%s', $class['namespace'], $class['name']);
$objects[] = new $fullClass;
}

$this->info(sprintf('Created %d objects. Migrating...', count($objects)));

$bar = $this->output->createProgressBar(count($objects));

foreach ($objects as $object) {
$model = $object->getModel();

$bar->advance();
}

$bar->finish();

$this->line('');
}

/**
* @param $src
* @return string|null
*/
private function getNamespace($src): ?string
{
if (preg_match('#^namespace\s+(.+?);$#sm', $src, $m)) {
return $m[1];
}
return null;
}
}

0 comments on commit 02c6406

Please sign in to comment.