diff --git a/src/Commands/Refresh.php b/src/Commands/Refresh.php index d1619ba4e..a0aeeb8ab 100644 --- a/src/Commands/Refresh.php +++ b/src/Commands/Refresh.php @@ -31,6 +31,7 @@ public function handle() $this->call('cache:clear'); $this->call('config:cache'); $this->call('view:clear'); + $this->call('route:clear'); $this->call('optimize'); } } diff --git a/src/Repositories/NullModel.php b/src/Repositories/NullModel.php new file mode 100644 index 000000000..dab48b4bc --- /dev/null +++ b/src/Repositories/NullModel.php @@ -0,0 +1,16 @@ + + */ +class NullModel extends Model +{ + use InteractWithSQLight; + + public $rows = []; +} diff --git a/src/Repositories/Repository.php b/src/Repositories/Repository.php index e1f1d1185..6324e0be2 100644 --- a/src/Repositories/Repository.php +++ b/src/Repositories/Repository.php @@ -76,7 +76,11 @@ public static function uriKey() */ public static function newModel() { - $model = static::$model; + if (property_exists(static::class, 'model')) { + $model = static::$model; + } else { + $model = NullModel::class; + } return new $model; } diff --git a/src/Traits/AuthorizableModels.php b/src/Traits/AuthorizableModels.php index b72454d0c..7f70a93b7 100644 --- a/src/Traits/AuthorizableModels.php +++ b/src/Traits/AuthorizableModels.php @@ -2,6 +2,7 @@ namespace Binaryk\LaravelRestify\Traits; +use Binaryk\LaravelRestify\Repositories\Repository; use Illuminate\Auth\Access\AuthorizationException; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\ModelNotFoundException; @@ -241,7 +242,7 @@ public function authorizedTo(Request $request, $ability) */ public function determineModel() { - $model = $this instanceof Model ? $this : ($this->resource ?? null); + $model = $this->isRepositoryContext() === false ? $this : ($this->resource ?? null); if (is_null($model)) { throw new ModelNotFoundException(__('Model is not declared in :class', ['class' => self::class])); @@ -249,4 +250,14 @@ public function determineModel() return $model; } + + /** + * Determine if the trait is used by repository or model. + * + * @return bool + */ + public static function isRepositoryContext() + { + return new static instanceof Repository; + } } diff --git a/src/Traits/InteractWithSQLight.php b/src/Traits/InteractWithSQLight.php new file mode 100644 index 000000000..7c310ac9c --- /dev/null +++ b/src/Traits/InteractWithSQLight.php @@ -0,0 +1,94 @@ +getFileName(); + + $states = [ + 'cache-file-found-and-up-to-date' => function () use ($cachePath) { + static::setSqliteConnection($cachePath); + }, + 'cache-file-not-found-or-stale' => function () use ($cachePath, $modelPath, $instance) { + file_put_contents($cachePath, ''); + + static::setSqliteConnection($cachePath); + + $instance->migrate(); + + touch($cachePath, filemtime($modelPath)); + }, + 'no-caching-capabilities' => function () use ($instance) { + static::setSqliteConnection(':memory:'); + + $instance->migrate(); + }, + ]; + + switch (true) { + case file_exists($cachePath) && filemtime($modelPath) === filemtime($cachePath): + $states['cache-file-found-and-up-to-date'](); + break; + + case file_exists($cacheDirectory) && is_writable($cacheDirectory): + $states['cache-file-not-found-or-stale'](); + break; + + default: + $states['no-caching-capabilities'](); + break; + } + } + + protected static function setSqliteConnection($database) + { + static::$sushiConnection = app(ConnectionFactory::class)->make([ + 'driver' => 'sqlite', + 'database' => $database, + ]); + } + + public function migrate() + { + $rows = $this->rows; + $firstRow = $rows[0]; + $tableName = $this->getTable(); + + throw_unless($rows, new \Exception('Sushi: $rows property not found on model: '.get_class($this))); + + static::resolveConnection()->getSchemaBuilder()->create($tableName, function ($table) use ($firstRow) { + foreach ($firstRow as $column => $value) { + if ($column === 'id') { + $table->increments('id'); + continue; + } + + $type = is_numeric($value) ? 'integer' : 'string'; + + $table->{$type}($column); + } + }); + + static::insert($rows); + } +}