Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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

rquadling commented Aug 9, 2016

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