fixture_task.php | |
| Type: Full Script |
Category: Database Manipulation |
| License: GNU General Public License |
Language: PHP |
| Description: A bake2 task that can run your DB fixtures. Works only with CakePHP 1.2. Please see http://joelmoss.info for more info. | |
|
||||||||||||||||||||||||||||||
Download a raw-text version of this code by clicking on "Download Version"
<?php
/**
* The FixtureTask runs a specified database fixture.
*
* PHP versions 4 and 5
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @filesource
* @copyright Copyright 2006-2007, Joel Moss
* @link http://joelmoss.info
* @package cake
* @subpackage cake.cake.scripts.bake
* @since CakePHP(tm) v 1.2
* @version $Version: 3.2.1 $
* @modifiedby $LastChangedBy: joelmoss $
* @lastmodified $Date: 2007-02-20 (Tues, 20 Feb 2007) $
* @license http://www.opensource.org/licenses/mit-license.php The MIT License
*
*
* @Changelog (started as of v3.0)
*
* v 3.2.1
* [*] fixed bug that was preventing values of '0' from being inserted
* v 3.2
* [+] refactored (again!) to work wth the new Cake console
* [+] options/variables no longer have to be passed as part of the command. You will be prompted if they are missing.
* v 3.1
* [+] can now parse and run PHP code within migration files
* [+] specifying 'all' instead of the table name will run all defined fixtures
* v 3.0
* [+] refactored as a bake2 task compatible with CakePHP 1.2
*
*/
uses('file', 'folder');
class FixturesShell extends Shell
{
var $dataSource = 'default';
var $db;
function initialize()
{
$this->welcome();
if (isset($this->params['datasource'])) {
$this->dataSource = $this->params['datasource'];
}
$this->hr();
$this->out('App: '. APP_DIR);
$this->out('Path: '. ROOT . DS . APP_DIR);
$this->hr();
if(!config('database')) {
$this->out('');
$this->out('Your database configuration was not found. Take a moment to create/edit your APP/config/database.php file.');
$this->out('');
$this->out('');
exit;
}
$this->db =& ConnectionManager::getDataSource($this->dataSource);
loadModel();
define('FIXTURES_PATH', APP_PATH .'config' .DS. 'fixtures');
}
function main()
{
$this->fixture = isset($this->params['t']) ? $this->params['t'] : '*';
$this->fixtures();
}
function fixtures()
{
if (!file_exists(FIXTURES_PATH)) $folder = new Folder(FIXTURES_PATH, true, 0777);
$tables = $this->db->sources();
if (!count($tables))
$this->err('Database contains no tables. Please run your migrations before your fixtures.');
if ($this->fixture == '*')
{
$this->out('');
foreach ($tables as $t)
{
if (!file_exists(FIXTURES_PATH .DS. $t .'.yml')) continue;
$this->out(" Running fixture for '".$t."' ... ", false);
$this->startFixture($t);
}
}
else
{
if (!file_exists(FIXTURES_PATH .DS. $this->fixture .'.yml'))
{
$this->err('Fixture does not exist for table \''.$this->fixture.'\'.');
exit;
}
$this->out('');
$this->out(" Running fixture for '".$this->fixture."' ... ", false);
$this->startFixture($this->fixture);
}
$this->out('');
$this->hr();
}
function startFixture($name)
{
$file = FIXTURES_PATH .DS. $name .'.yml';
$yml = $this->_parsePhp($file);
if (function_exists('syck_load'))
{
$data = @syck_load($yml);
}
else
{
vendor('Spyc');
$data = Spyc::YAMLLoad($yml);
}
if (count($data) === 0)
{
$this->out('Fixture undefined.');
return false;
}
if (!is_array($data) || !count($data))
$this->err("Unable to parse YAML Fixture file: '$file'");
$model = new Model(false, $name);
$this->db->rawQuery("DELETE FROM `$name`");
$count = 0;
foreach($data as $ri=>$r)
{
$fields = array();
$values = array();
foreach($r as $fi=>$f)
{
$fields[] = $fi;
if ($f === 'NOW')
{
$values[] = date('Y-m-d H:i:s');
}
elseif ($f === 'LOREM')
{
$values[] = $this->_lorem();
}
else
{
$values[] = $f;
}
}
$res = $this->db->create($model, $fields, $values);
$count++;
}
$this->out("($count rows) ... Fixture completed.");
}
function _lorem()
{
return "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Maecenas quis justo. Cras purus lectus, rhoncus lacinia, ornare in, porttitor non, enim. Donec ac eros a pede semper porta. Nunc vel nibh. Praesent dignissim tellus facilisis ante. Suspendisse porttitor interdum nulla. Maecenas ligula. Sed nec ante. Ut tincidunt purus bibendum pede. Sed ullamcorper euismod justo. Phasellus euismod molestie odio. Pellentesque tristique pede et nisl. Phasellus lacus nunc, accumsan eu, vehicula eu, laoreet vel, tortor. Nam et pede eget lorem dapibus rutrum. Vivamus et orci. In adipiscing. Sed pulvinar pharetra lorem. Ut ullamcorper leo.";
}
function _parsePhp($file)
{
ob_start();
include ($file);
$buf = ob_get_contents();
ob_end_clean();
#echo $buf;exit;
return $buf;
}
function help()
{
echo "This task inserts database fixtures.\n";
echo "Usage: cake fixtures [-t TABLENAME]\n";
}
function err($str)
{
$this->out('');
$this->out(' ** '.$str.' **');
$this->hr();
exit;
}
function welcome()
{
$this->out('');
$this->out(' __ __ _ _ __ __ _ _ __ __ ___ _ __ _ ');
$this->out('| |__| |_/ |__ |__| |__| |__| |_ | \/ | | | |_| |__ |_ ');
$this->out('|__ | | | \_ |__ | | | | | | /\ | |_| | \ |__ _|');
$this->out('');
}
}
?>
You can submit a new version of this snippet if you have modified it and you feel it is appropriate to share with others..