-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 24249b8
Showing
6 changed files
with
165 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
/vendor/ | ||
.idea | ||
composer.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# Sushi | ||
Eloquent's missing "array" driver. | ||
|
||
Sometimes you just want to use eloquent, but without all the hastle. | ||
|
||
## Install | ||
`composer require calebporzio/sushi` | ||
|
||
## Use | ||
|
||
Using this package consists of two steps: | ||
1. Add the `Sushi` trait to a model. | ||
2. Add a `$rows` property to the model. | ||
|
||
That's it. | ||
|
||
```php | ||
use Sushi\Sushi as TheMissingEloquentArrayDriver; | ||
|
||
class State extends Model | ||
{ | ||
use TheMissingEloquentArrayDriver; | ||
|
||
protected $rows = [ | ||
[ | ||
'abbr' => 'NY', | ||
'name' => 'New York', | ||
], | ||
[ | ||
'abbr' => 'CA', | ||
'name' => 'California', | ||
], | ||
]; | ||
} | ||
``` | ||
|
||
Now, you can use this model anywhere you like, and it will behave as if you created a table with the rows you provided. | ||
```php | ||
$stateName = State::whereAbbr('NY')->first()->name; | ||
``` | ||
|
||
This is really useful for "Fixture" data, like states, contries, zip codes, user_roles, sites_settings, etc... | ||
|
||
## How It Works | ||
Under the hood, this package creates a SQLite in-memory database JUST for this model. It addes a table and populates the rows, all within each request. | ||
|
||
SQLite is really fast, and my quick little benchmark with a 200 row model added a .009 second overhead to my application. "That's a number I can live with!" -- Penguins |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "calebporzio/sushi", | ||
"description": "Eloquent's missing \"array\" driver.", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Caleb Porzio", | ||
"email": "calebporzio@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"illuminate/database": "~5.8.0|^6.0.0", | ||
"illuminate/support": "~5.8.0|^6.0.0", | ||
"orchestra/testbench": "^4.5" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Sushi\\": "src/" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^8.5" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd" | ||
bootstrap="vendor/autoload.php" | ||
executionOrder="depends,defects" | ||
forceCoversAnnotation="true" | ||
colors="true" | ||
beStrictAboutCoversAnnotation="true" | ||
beStrictAboutOutputDuringTests="true" | ||
beStrictAboutTodoAnnotatedTests="true" | ||
verbose="true"> | ||
<testsuites> | ||
<testsuite name="default"> | ||
<directory suffix="Test.php">src</directory> | ||
</testsuite> | ||
</testsuites> | ||
|
||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">src</directory> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
|
||
namespace Sushi; | ||
|
||
use Illuminate\Database\Connectors\ConnectionFactory; | ||
|
||
trait Sushi | ||
{ | ||
public static function resolveConnection($connection = null) | ||
{ | ||
static $cache; | ||
|
||
return $cache ?: $cache = app(ConnectionFactory::class)->make([ | ||
'driver' => 'sqlite', | ||
'database' => ':memory:', | ||
]); | ||
} | ||
|
||
public static function bootSushi() | ||
{ | ||
$instance = (new static); | ||
$tableName = $instance->getTable(); | ||
$rows = $instance->rows; | ||
$firstRow = $rows[0]; | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Sushi; | ||
|
||
use Illuminate\Database\Eloquent\Model; | ||
use Orchestra\Testbench\TestCase; | ||
|
||
class SushiTest extends TestCase | ||
{ | ||
/** @test */ | ||
function basic_usage() | ||
{ | ||
$this->assertEquals(2, Foo::count()); | ||
$this->assertEquals('bar', Foo::first()->foo); | ||
$this->assertEquals('lob', Foo::whereBob('lob')->first()->bob); | ||
} | ||
} | ||
|
||
class Foo extends Model | ||
{ | ||
use Sushi; | ||
|
||
protected $rows = [ | ||
['foo' => 'bar', 'bob' => 'lob'], | ||
['foo' => 'baz', 'bob' => 'law'], | ||
]; | ||
} |