Author: pmacadden
Date: 2010-05-21 15:27:12 +0200 (Fri, 21 May 2010)
New Revision: 29567
Modified:
plugins/pmMigratorPlugin/trunk/lib/pmMigrator.class.php
plugins/pmMigratorPlugin/trunk/lib/pmMigratorCSV.class.php
Log:
api changed. Two main functions: toFixture toDB
Modified: plugins/pmMigratorPlugin/trunk/lib/pmMigrator.class.php
===================================================================
--- plugins/pmMigratorPlugin/trunk/lib/pmMigrator.class.php 2010-05-21
13:22:16 UTC (rev 29566)
+++ plugins/pmMigratorPlugin/trunk/lib/pmMigrator.class.php 2010-05-21
13:27:12 UTC (rev 29567)
@@ -224,9 +224,16 @@
* @param boolean $dry Run in dry mode
* @param boolean $debug Run in debug mode
*/
- public abstract function migrate($dry = false, $debug = false);
+ public abstract function toDB($dry = false, $debug = false);
/**
+ * Perform the migration into a fixture file.
+ * @param boolean $dry Run in dry mode
+ * @param boolean $debug Run in debug mode
+ */
+ public abstract function toFixture($fixture_name = null, $dry = false,
$debug = false);
+
+ /**
* Create an object
* @param bool $debug Run in debug mode
*
Modified: plugins/pmMigratorPlugin/trunk/lib/pmMigratorCSV.class.php
===================================================================
--- plugins/pmMigratorPlugin/trunk/lib/pmMigratorCSV.class.php 2010-05-21
13:22:16 UTC (rev 29566)
+++ plugins/pmMigratorPlugin/trunk/lib/pmMigratorCSV.class.php 2010-05-21
13:27:12 UTC (rev 29567)
@@ -56,7 +56,7 @@
* @param boolean $dry Run in dry mode
* @param boolean $debug Run in debug mode
*/
- public function migrate($dry = false, $debug = false)
+ public function toDB($dry = false, $debug = false)
{
$handle = fopen($this->getFile(), "r");
@@ -86,4 +86,88 @@
throw new Exception("File {$this->getFile()} could not be opened.");
}
}
+
+ /**
+ * Perform the migration into a fixture file.
+ * @param boolean $dry Run in dry mode
+ * @param boolean $debug Run in debug mode
+ */
+ public function toFixture($fixture_name = null, $dry = false, $debug = false)
+ {
+ $handle = fopen($this->getFile(), "r");
+
+ $objects = array();
+ $nb_lines = 0;
+
+ if ($handle)
+ {
+ while($data = fgetcsv($handle))
+ {
+ $object = $this->createObject($debug);
+
+ for ($i = 0; $i < count($data); $i++)
+ {
+ if (!is_null($this->getClassField($i)))
+ {
+ $value = $data[$i];
+ $this->populateObjectField($object, $this->getClassField($i),
$value, $debug);
+ }
+ }
+
+ $this->runObjectHooks($object, $debug);
+
+ $objects[] = $object;
+ }
+
+ fclose($handle);
+
+ if ($dry)
+ {
+ fwrite(STDOUT, sfYaml::dump($this->toArray($objects), 3));
+ }
+ else
+ {
+ if (is_null($fixture_name))
+ {
+ $fixture_name = sfInflector::underscore($this->getClassName());
+ }
+ $fixture_handle =
fopen(sfConfig::get('sf_data_dir')."/fixtures/$fixture_name.yml", "w");
+ fwrite($fixture_handle, sfYaml::dump($this->toArray($objects), 3));
+ }
+ }
+ else
+ {
+ throw new Exception("File {$this->getFile()} could not be opened.");
+ }
+ }
+
+ private function toArray($array_of_objects)
+ {
+ if (!count($array_of_objects))
+ {
+ return array();
+ }
+
+ $array = array($this->getClassName() => array());
+
+ $count = 0;
+ foreach ($array_of_objects as $object)
+ {
+ $index = $this->getClassName().'_'.++$count;
+ $array[$this->getClassName()][$index] =
$this->fixKeyNames($object->toArray());
+ }
+
+ return $array;
+ }
+
+ private function fixKeyNames($array)
+ {
+ $ret = array();
+ foreach ($array as $k => $v)
+ {
+ $k = sfInflector::underscore($k);
+ $ret[$k] = $v;
+ }
+ return $ret;
+ }
}
--
You received this message because you are subscribed to the Google Groups
"symfony SVN" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/symfony-svn?hl=en.