Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
calebporzio committed Jan 25, 2020
0 parents commit 24249b8
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/vendor/
.idea
composer.lock
47 changes: 47 additions & 0 deletions README.md
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
24 changes: 24 additions & 0 deletions composer.json
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"
}
}
23 changes: 23 additions & 0 deletions phpunit.xml
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>
41 changes: 41 additions & 0 deletions src/Sushi.php
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);
}
}
27 changes: 27 additions & 0 deletions src/SushiTest.php
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'],
];
}

0 comments on commit 24249b8

Please sign in to comment.