Added xml data loader

This commit is contained in:
Kévin Gomez 2013-11-05 12:49:49 +00:00
parent fcbbff355c
commit 7e2b6289ed
2 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1,61 @@
<?php
/**
* This file is part of the PropelBundle package.
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @license MIT License
*/
namespace Propel\PropelBundle\DataFixtures\Loader;
/**
* XML fixtures loader.
*
* @author William Durand <william.durand1@gmail.com>
*/
class XmlDataLoader extends AbstractDataLoader
{
/**
* {@inheritdoc}
*/
protected function transformDataToArray($file)
{
$xml = simplexml_load_file($file);
return $this->simpleXmlToArray($xml);
}
/**
* @param SimpleXMLElement $xml
* @return array
*/
protected function simpleXmlToArray($xml)
{
$array = array();
if ($xml instanceof \SimpleXMLElement) {
foreach ($xml as $key => $value) {
// First make a valid key which is the Ns (Namespace) attribute
// + the element name (the class name)
foreach ($value->attributes() as $subkey => $subvalue) {
if ('Namespace' === (string) $subkey) {
$key = $subvalue . '\\' . $key;
break;
}
}
$array[$key] = array();
foreach ($value as $elementKey => $elementValue) {
$array[$key][$elementKey] = array();
foreach ($elementValue->attributes() as $subkey => $subvalue) {
$array[$key][$elementKey][$subkey] = (string) $subvalue;
}
}
}
}
return $array;
}
}

View file

@ -15,6 +15,7 @@
<parameter key="form.type_guesser.propel.class">Propel\PropelBundle\Form\TypeGuesser</parameter>
<parameter key="propel.dumper.yaml.class">Propel\PropelBundle\DataFixtures\Dumper\YamlDataDumper</parameter>
<parameter key="propel.loader.yaml.class">Propel\PropelBundle\DataFixtures\Loader\YamlDataLoader</parameter>
<parameter key="propel.loader.xml.class">Propel\PropelBundle\DataFixtures\Loader\XmlDataLoader</parameter>
</parameters>
<services>
@ -53,5 +54,11 @@
<argument>%kernel.root_dir%</argument>
<argument type="service" id="service_container" />
</service>
<service id="propel.loader.xml" class="%propel.loader.xml.class%">
<argument>%kernel.root_dir%</argument>
<argument type="service" id="propel" />
<argument>%propel.configuration%</argument>
</service>
</services>
</container>