Skip to content

Instantly share code, notes, and snippets.

@arnold-almeida
Created July 31, 2013 06:25
Show Gist options
  • Save arnold-almeida/6119780 to your computer and use it in GitHub Desktop.
Save arnold-almeida/6119780 to your computer and use it in GitHub Desktop.
Shortcut shell for quick fixtures
<?php
/**
* Shortcut shell for easy fixtures
*
* Plugin/s :
* git submodule add git@github.com:FloatingPoints/Resources.git app/Plugin/Resources
* Install this file:
* app/Console/Command
* Usage:
* cake fixture [subcommand] [-h] [-v] [-q]
*
* @author Sime (sime@sime.net.au)
* @author Arnold (arnold@floatingpoints.com.au)
*/
class FixtureShell extends AppShell
{
public $tasks = array();
public $datasource = 'default';
/**
* Explicitly name the models you want to use fixtures for
*
* Syntax:
* Model
* Plugin.Model
*/
public $models = array();
public function main()
{
$this->out($this->OptionParser->help());
}
public function generate()
{
foreach ($this->models as $model) {
$this->_create($model);
}
}
/**
* Imports specifed fixtures into db table
* @param cake fixture import
*/
public function import()
{
foreach ($this->models as $model) {
$fixture = "app.{$model}";;
if (strstr($model, '.')) {
$fixture = "plugin.{$model}";
}
$this->_load($fixture);
}
}
private function _load($fixture)
{
if (env('CI') == "true") {
$this->dispatchShell('Resources.fixture_loader', '-q', $fixture, '--datasource', $this->connection());
} else {
$this->dispatchShell('Resources.fixture_loader', $fixture, '--datasource', $this->connection());
}
}
private function _create($modelName)
{
// Is the model a part of a plugin?
// Ripped from BakeTast::execute()
if (strpos($modelName, '.')) {
list($this->Fixture->plugin, $modelName) = pluginSplit($modelName);
} else {
$this->Fixture->plugin = null;
}
$this->Fixture->Model->listAll($this->connection());
$useTable = $this->Fixture->Model->getTable($modelName);
$this->Fixture->connection = $this->connection();
$this->params['count'] = 1000;
$this->params['records'] = true;
$this->Fixture->bake($modelName, $useTable);
}
public function getOptionParser()
{
$parser = parent::getOptionParser();
$parser->description(
'Shortcut shell loading fixtures'
)
# Args
->addOption('connection', array(
'short' => 'c',
'help' => __('Which database connection are we using ?'),
'default' => 'default'))
# Opts
->addSubcommand('generate', array(
'help' => __('Generate fixtures')))
->addSubcommand('import', array(
'help' => __('Import fixtures')));
return $parser;
}
private function connection()
{
if (isset($this->params['connection'])) {
$this->datasource = $this->params['connection'];
}
return $this->datasource;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment