Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/Commands/Refresh.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}
16 changes: 16 additions & 0 deletions src/Repositories/NullModel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Binaryk\LaravelRestify\Repositories;

use Binaryk\LaravelRestify\Traits\InteractWithSQLight;
use Illuminate\Database\Eloquent\Model;

/**
* @author Eduard Lupacescu <eduard.lupacescu@binarcode.com>
*/
class NullModel extends Model
{
use InteractWithSQLight;

public $rows = [];
}
6 changes: 5 additions & 1 deletion src/Repositories/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
13 changes: 12 additions & 1 deletion src/Traits/AuthorizableModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -241,12 +242,22 @@ 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]));
}

return $model;
}

/**
* Determine if the trait is used by repository or model.
*
* @return bool
*/
public static function isRepositoryContext()
{
return new static instanceof Repository;
}
}
94 changes: 94 additions & 0 deletions src/Traits/InteractWithSQLight.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php

namespace Binaryk\LaravelRestify\Traits;

use Illuminate\Database\Connectors\ConnectionFactory;
use Illuminate\Support\Str;

/**
* @author Caleb Porzio https://github.com/calebporzio/sushi/blob/master/src/Sushi.php
*/
trait InteractWithSQLight
{
protected static $sushiConnection;

public static function resolveConnection($connection = null)
{
return static::$sushiConnection;
}

public static function bootSushi()
{
$instance = (new static);
$cacheFileName = 'sushi-'.Str::kebab(str_replace('\\', '', static::class)).'.sqlite';
$cacheDirectory = realpath(config('sushi.cache-path', storage_path('framework/cache')));
$cachePath = $cacheDirectory.'/'.$cacheFileName;
$modelPath = (new \ReflectionClass(static::class))->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);
}
}