Skip to content

Instantly share code, notes, and snippets.

@lira92
Created August 8, 2016 17:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save lira92/4b98d6ef6114d5f664e6d6a6aadfbb4e to your computer and use it in GitHub Desktop.
Save lira92/4b98d6ef6114d5f664e6d6a6aadfbb4e to your computer and use it in GitHub Desktop.
<?php
namespace App\Migrations;
use Phinx\Seed\AbstractSeed;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;
class AbstractSeedFromJson extends AbstractSeed
{
/**
* @var String
*/
protected $jsonFileName;
/**
* @var Array
*/
protected $externalData;
public function setJsonFileName($file)
{
if(!(new FileSystem)->exists($file)) {
throw new FileNotFoundException(sprintf('Failed to set JsonFileName because file "%s" does not exist.', $file), 0, null, $file);
}
$this->jsonFileName = $file;
$this->loadExternalFile();
}
public function getExternalData()
{
return $this->externalData;
}
private function loadExternalFile()
{
$this->externalData = json_decode(file_get_contents($this->jsonFileName), true);
}
}
<?php
use App\Migrations\AbstractSeedFromJson;
class UfSeeder extends AbstractSeedFromJson
{
public function init()
{
$this->setJsonFileName(__DIR__ . DIRECTORY_SEPARATOR . 'uf.json');
}
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* http://docs.phinx.org/en/latest/seeding.html
*/
public function run()
{
$estados = $this->table('aux_estados');
$estados->insert($this->getExternalData())
->save();
}
}
@rquadling
Copy link

That's the sort of thing.

<?php
namespace App\Migrations;

use Phinx\Seed\AbstractSeed;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\FileNotFoundException;

class AbstractSeedFromJson extends AbstractSeed
{
    /**
     * @var array
     */
    protected $externalData;

    /**
     * @var string
     */
    protected $jsonFileName;

    /**
     * @var Table
     */
    protected $seedTable;

    /**
     * @return array
     */
    public function getExternalData()
    {
        return $this->externalData;
    }

    /**
     * Run Method.
     *
     * Write your database seeder using this method.
     *
     * More information on writing seeders is available here:
     * http://docs.phinx.org/en/latest/seeding.html
     */
    final public function run()
    {
        $this->seedTable->insert($this->getExternalData())->save();
    }

    protected function setJsonFileName($file)
    {
        if (!(new FileSystem)->exists($file)) {
            throw new FileNotFoundException(sprintf('Failed to set JsonFileName because file "%s" does not exist.', $file), 0, null, $file);
        }
        $this->jsonFileName = $file;
        $this->loadExternalFile();
    }

    protected function setSeedTable($tableName)
    {
        $this->seedTable = $this->table($tableName);
    }

    private function loadExternalFile()
    {
        $this->externalData = json_decode(file_get_contents($this->jsonFileName), true);
    }
}

class UfSeeder extends AbstractSeedFromJson
{
    public function init()
    {
        $this->setJsonFileName(__DIR__ . DIRECTORY_SEPARATOR . 'uf.json');
        $this->setSeedTable('aux_estados');
    }
}

This is how I would done it. Moved the run() to the abstract class, reducing the amount needed to be edited in the seeder.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment