|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Traits; |
| 4 | + |
| 5 | +use Illuminate\Database\Connectors\ConnectionFactory; |
| 6 | +use Illuminate\Support\Str; |
| 7 | + |
| 8 | +/** |
| 9 | + * @author Caleb Porzio https://github.com/calebporzio/sushi/blob/master/src/Sushi.php |
| 10 | + */ |
| 11 | +trait InteractWithSQLight |
| 12 | +{ |
| 13 | + protected static $sushiConnection; |
| 14 | + |
| 15 | + public static function resolveConnection($connection = null) |
| 16 | + { |
| 17 | + return static::$sushiConnection; |
| 18 | + } |
| 19 | + |
| 20 | + public static function bootSushi() |
| 21 | + { |
| 22 | + $instance = (new static); |
| 23 | + $cacheFileName = 'sushi-'.Str::kebab(str_replace('\\', '', static::class)).'.sqlite'; |
| 24 | + $cacheDirectory = realpath(config('sushi.cache-path', storage_path('framework/cache'))); |
| 25 | + $cachePath = $cacheDirectory.'/'.$cacheFileName; |
| 26 | + $modelPath = (new \ReflectionClass(static::class))->getFileName(); |
| 27 | + |
| 28 | + $states = [ |
| 29 | + 'cache-file-found-and-up-to-date' => function () use ($cachePath) { |
| 30 | + static::setSqliteConnection($cachePath); |
| 31 | + }, |
| 32 | + 'cache-file-not-found-or-stale' => function () use ($cachePath, $modelPath, $instance) { |
| 33 | + file_put_contents($cachePath, ''); |
| 34 | + |
| 35 | + static::setSqliteConnection($cachePath); |
| 36 | + |
| 37 | + $instance->migrate(); |
| 38 | + |
| 39 | + touch($cachePath, filemtime($modelPath)); |
| 40 | + }, |
| 41 | + 'no-caching-capabilities' => function () use ($instance) { |
| 42 | + static::setSqliteConnection(':memory:'); |
| 43 | + |
| 44 | + $instance->migrate(); |
| 45 | + }, |
| 46 | + ]; |
| 47 | + |
| 48 | + switch (true) { |
| 49 | + case file_exists($cachePath) && filemtime($modelPath) === filemtime($cachePath): |
| 50 | + $states['cache-file-found-and-up-to-date'](); |
| 51 | + break; |
| 52 | + |
| 53 | + case file_exists($cacheDirectory) && is_writable($cacheDirectory): |
| 54 | + $states['cache-file-not-found-or-stale'](); |
| 55 | + break; |
| 56 | + |
| 57 | + default: |
| 58 | + $states['no-caching-capabilities'](); |
| 59 | + break; |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + protected static function setSqliteConnection($database) |
| 64 | + { |
| 65 | + static::$sushiConnection = app(ConnectionFactory::class)->make([ |
| 66 | + 'driver' => 'sqlite', |
| 67 | + 'database' => $database, |
| 68 | + ]); |
| 69 | + } |
| 70 | + |
| 71 | + public function migrate() |
| 72 | + { |
| 73 | + $rows = $this->rows; |
| 74 | + $firstRow = $rows[0]; |
| 75 | + $tableName = $this->getTable(); |
| 76 | + |
| 77 | + throw_unless($rows, new \Exception('Sushi: $rows property not found on model: '.get_class($this))); |
| 78 | + |
| 79 | + static::resolveConnection()->getSchemaBuilder()->create($tableName, function ($table) use ($firstRow) { |
| 80 | + foreach ($firstRow as $column => $value) { |
| 81 | + if ($column === 'id') { |
| 82 | + $table->increments('id'); |
| 83 | + continue; |
| 84 | + } |
| 85 | + |
| 86 | + $type = is_numeric($value) ? 'integer' : 'string'; |
| 87 | + |
| 88 | + $table->{$type}($column); |
| 89 | + } |
| 90 | + }); |
| 91 | + |
| 92 | + static::insert($rows); |
| 93 | + } |
| 94 | +} |
0 commit comments