/
Sushi.php
125 lines (96 loc) · 3.52 KB
/
Sushi.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Sushi;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Connectors\ConnectionFactory;
trait Sushi
{
protected static $sushiConnection;
public static function resolveConnection($connection = null)
{
return static::$sushiConnection;
}
public static function bootSushi()
{
$instance = (new static);
$cacheDirectory = realpath(config('sushi.cache-path', storage_path('framework/cache')));
$cachePath = static::getSushiCachePath();
$modelPath = static::getSushiModelPath();
$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) {
static::migrateSushiCache();
},
'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->getRows();
throw_unless(is_array($rows), new \Exception('Sushi: $rows property not found on model: '.get_class($this)));
$firstRow = $rows[0];
$tableName = $this->getTable();
static::resolveConnection()->getSchemaBuilder()->create($tableName, function ($table) use ($firstRow) {
foreach ($firstRow as $column => $value) {
$type = is_numeric($value) ? 'integer' : 'string';
if ($column === 'id' && $type == 'integer') {
$table->increments('id');
continue;
}
$table->{$type}($column);
}
});
static::insert($rows);
}
public static function flushRowCache()
{
(new static)->query()->delete();
static::migrateSushiCache();
}
protected function getRows()
{
return $this->rows;
}
private static function migrateSushiCache()
{
$cachePath = static::getSushiCachePath();
$modelPath = static::getSushiModelPath();
file_put_contents($cachePath, '');
static::setSqliteConnection($cachePath);
(new static)->migrate();
touch($cachePath, filemtime($modelPath));
}
private static function getSushiCachePath()
{
$cacheFileName = config('sushi.cache-prefix', 'sushi').'-'.Str::kebab(str_replace('\\', '', static::class)).'.sqlite';
$cacheDirectory = realpath(config('sushi.cache-path', storage_path('framework/cache')));
return $cacheDirectory.'/'.$cacheFileName;
}
private static function getSushiModelPath()
{
return (new \ReflectionClass(static::class))->getFileName();
}
}