2010-01-04 14:26:20 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Symfony\Components\DependencyInjection\Loader;
|
|
|
|
|
|
|
|
use Symfony\Components\DependencyInjection\Definition;
|
|
|
|
use Symfony\Components\DependencyInjection\Reference;
|
|
|
|
use Symfony\Components\DependencyInjection\BuilderConfiguration;
|
|
|
|
use Symfony\Components\DependencyInjection\SimpleXMLElement;
|
2010-06-27 17:28:29 +01:00
|
|
|
use Symfony\Components\DependencyInjection\Resource\FileResource;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
|
|
|
/*
|
2010-04-07 01:51:29 +01:00
|
|
|
* This file is part of the Symfony framework.
|
2010-01-04 14:26:20 +00:00
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*
|
|
|
|
* This source file is subject to the MIT license that is bundled
|
|
|
|
* with this source code in the file LICENSE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* XmlFileLoader loads XML files service definitions.
|
|
|
|
*
|
2010-04-07 01:51:29 +01:00
|
|
|
* @package Symfony
|
|
|
|
* @subpackage Components_DependencyInjection
|
2010-01-04 14:26:20 +00:00
|
|
|
* @author Fabien Potencier <fabien.potencier@symfony-project.com>
|
|
|
|
*/
|
|
|
|
class XmlFileLoader extends FileLoader
|
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Loads an array of XML files.
|
|
|
|
*
|
2010-06-07 06:57:01 +01:00
|
|
|
* @param mixed $resource The resource
|
|
|
|
* @param Boolean $main Whether this is the main load() call
|
|
|
|
* @param BuilderConfiguration $configuration A BuilderConfiguration instance to use for the configuration
|
2010-05-06 12:25:53 +01:00
|
|
|
*
|
|
|
|
* @return BuilderConfiguration A BuilderConfiguration instance
|
|
|
|
*/
|
2010-06-07 06:57:01 +01:00
|
|
|
public function load($file, $main = true, BuilderConfiguration $configuration = null)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
$path = $this->findFile($file);
|
2010-01-17 10:21:31 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$xml = $this->parseFile($path);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-06-07 06:57:01 +01:00
|
|
|
if (null === $configuration) {
|
|
|
|
$configuration = new BuilderConfiguration();
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$configuration->addResource(new FileResource($path));
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// anonymous services
|
|
|
|
$xml = $this->processAnonymousServices($configuration, $xml, $file);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// imports
|
|
|
|
$this->parseImports($configuration, $xml, $file);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// extensions
|
|
|
|
$this->loadFromExtensions($configuration, $xml);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-06-07 06:57:01 +01:00
|
|
|
if ($main) {
|
|
|
|
$configuration->mergeExtensionsConfiguration();
|
|
|
|
}
|
|
|
|
|
2010-06-22 16:27:49 +01:00
|
|
|
// parameters
|
|
|
|
$this->parseParameters($configuration, $xml, $file);
|
|
|
|
|
|
|
|
// services
|
|
|
|
$this->parseDefinitions($configuration, $xml, $file);
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $configuration;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function parseParameters(BuilderConfiguration $configuration, $xml, $file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$xml->parameters) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-06-27 17:28:29 +01:00
|
|
|
$configuration->getParameterBag()->add($xml->parameters->getArgumentsAsPhp('parameter'));
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function parseImports(BuilderConfiguration $configuration, $xml, $file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$xml->imports) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($xml->imports->import as $import) {
|
2010-06-07 06:57:01 +01:00
|
|
|
$this->parseImport($configuration, $import, $file);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
|
|
|
}
|
2010-01-17 11:20:47 +00:00
|
|
|
|
2010-06-07 06:57:01 +01:00
|
|
|
protected function parseImport(BuilderConfiguration $configuration, $import, $file)
|
2010-05-06 12:25:53 +01:00
|
|
|
{
|
|
|
|
$class = null;
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($import['class']) && $import['class'] !== get_class($this)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$class = (string) $import['class'];
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
// try to detect loader with the extension
|
2010-05-07 15:09:11 +01:00
|
|
|
switch (pathinfo((string) $import['resource'], PATHINFO_EXTENSION)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
case 'yml':
|
|
|
|
$class = 'Symfony\\Components\\DependencyInjection\\Loader\\YamlFileLoader';
|
|
|
|
break;
|
|
|
|
case 'ini':
|
|
|
|
$class = 'Symfony\\Components\\DependencyInjection\\Loader\\IniFileLoader';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$loader = null === $class ? $this : new $class($this->paths);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$importedFile = $this->getAbsolutePath((string) $import['resource'], dirname($file));
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-06-07 06:57:01 +01:00
|
|
|
return $loader->load($importedFile, false, $configuration);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function parseDefinitions(BuilderConfiguration $configuration, $xml, $file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$xml->services) {
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($xml->services->service as $service) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->parseDefinition($configuration, (string) $service['id'], $service, $file);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function parseDefinition(BuilderConfiguration $configuration, $id, $service, $file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
if ((string) $service['alias']) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$configuration->setAlias($id, (string) $service['alias']);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition = new Definition((string) $service['class']);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-07-04 17:56:48 +01:00
|
|
|
foreach (array('shared', 'factory-method', 'factory-service', 'factory-class') as $key) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (isset($service[$key])) {
|
2010-07-05 08:47:14 +01:00
|
|
|
$method = 'set'.str_replace('-', '', $key);
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->$method((string) $service->getAttributeAsPhp($key));
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($service->file) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->setFile((string) $service->file);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->setArguments($service->getArgumentsAsPhp('argument'));
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (isset($service->configurator)) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (isset($service->configurator['function'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->setConfigurator((string) $service->configurator['function']);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (isset($service->configurator['service'])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$class = new Reference((string) $service->configurator['service']);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$class = (string) $service->configurator['class'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$definition->setConfigurator(array($class, (string) $service->configurator['method']));
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($service->call as $call) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$definition->addMethodCall((string) $call['method'], $call->getArgumentsAsPhp('argument'));
|
2010-02-09 08:45:23 +00:00
|
|
|
}
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($service->annotation as $annotation) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$parameters = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($annotation->attributes() as $name => $value) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if ('name' === $name) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameters[$name] = SimpleXMLElement::phpize($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
$definition->addAnnotation((string) $annotation['name'], $parameters);
|
|
|
|
}
|
2010-02-09 08:45:23 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$configuration->setDefinition($id, $definition);
|
2010-02-09 08:45:23 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException When loading of XML file returns error
|
|
|
|
*/
|
|
|
|
protected function parseFile($file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$dom = new \DOMDocument();
|
|
|
|
libxml_use_internal_errors(true);
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$dom->load($file, LIBXML_COMPACT)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
|
|
|
|
}
|
|
|
|
$dom->validateOnParse = true;
|
|
|
|
$dom->normalizeDocument();
|
|
|
|
libxml_use_internal_errors(false);
|
|
|
|
$this->validate($dom, $file);
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return simplexml_import_dom($dom, 'Symfony\\Components\\DependencyInjection\\SimpleXMLElement');
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function processAnonymousServices(BuilderConfiguration $configuration, $xml, $file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$definitions = array();
|
|
|
|
$count = 0;
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// find anonymous service definitions
|
|
|
|
$xml->registerXPathNamespace('container', 'http://www.symfony-project.org/schema/dic/services');
|
|
|
|
$nodes = $xml->xpath('//container:argument[@type="service"][not(@id)]');
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($nodes as $node) {
|
2010-05-06 12:25:53 +01:00
|
|
|
// give it a unique names
|
|
|
|
$node['id'] = sprintf('_%s_%d', md5($file), ++$count);
|
2010-01-11 14:56:49 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$definitions[(string) $node['id']] = array($node->service, $file);
|
|
|
|
$node->service['id'] = (string) $node['id'];
|
|
|
|
}
|
2010-01-11 14:56:49 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
// resolve definitions
|
|
|
|
krsort($definitions);
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($definitions as $id => $def) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->parseDefinition($configuration, $id, $def[0], $def[1]);
|
2010-02-13 09:39:35 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$oNode = dom_import_simplexml($def[0]);
|
|
|
|
$oNode->parentNode->removeChild($oNode);
|
2010-02-13 09:39:35 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
return $xml;
|
2010-01-11 14:56:49 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function validate($dom, $file)
|
2010-01-11 14:56:49 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$this->validateSchema($dom, $file);
|
|
|
|
$this->validateExtensions($dom, $file);
|
2010-01-11 14:56:49 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* @throws \RuntimeException When extension references a non-existent XSD file
|
|
|
|
* @throws \InvalidArgumentException When xml doesn't validate its xsd schema
|
|
|
|
*/
|
|
|
|
protected function validateSchema($dom, $file)
|
|
|
|
{
|
|
|
|
$schemaLocations = array('http://www.symfony-project.org/schema/dic/services' => str_replace('\\', '/', __DIR__.'/schema/dic/services/services-1.0.xsd'));
|
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if ($element = $dom->documentElement->getAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation')) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$items = preg_split('/\s+/', $element);
|
2010-05-07 15:09:11 +01:00
|
|
|
for ($i = 0, $nb = count($items); $i < $nb; $i += 2) {
|
2010-07-05 08:13:01 +01:00
|
|
|
if (($extension = static::getExtension($items[$i])) && false !== $extension->getXsdValidationBasePath()) {
|
2010-06-27 17:54:03 +01:00
|
|
|
$path = str_replace($extension->getNamespace(), str_replace('\\', '/', $extension->getXsdValidationBasePath()).'/', $items[$i + 1]);
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!file_exists($path)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \RuntimeException(sprintf('Extension "%s" references a non-existent XSD file "%s"', get_class($extension), $path));
|
|
|
|
}
|
|
|
|
|
|
|
|
$schemaLocations[$items[$i]] = $path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$imports = '';
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($schemaLocations as $namespace => $location) {
|
2010-05-21 15:18:29 +01:00
|
|
|
$parts = explode('/', $location);
|
|
|
|
$drive = '\\' === DIRECTORY_SEPARATOR ? array_shift($parts) : '';
|
|
|
|
$location = 'file:///'.$drive.implode('/', array_map('rawurlencode', $parts));
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$imports .= sprintf(' <xsd:import namespace="%s" schemaLocation="%s" />'."\n", $namespace, $location);
|
|
|
|
}
|
|
|
|
|
|
|
|
$source = <<<EOF
|
2010-01-11 14:56:49 +00:00
|
|
|
<?xml version="1.0" encoding="utf-8" ?>
|
|
|
|
<xsd:schema xmlns="http://www.symfony-project.org/schema"
|
|
|
|
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
|
|
|
|
targetNamespace="http://www.symfony-project.org/schema"
|
|
|
|
elementFormDefault="qualified">
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
<xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
|
2010-01-11 14:56:49 +00:00
|
|
|
$imports
|
|
|
|
</xsd:schema>
|
|
|
|
EOF
|
2010-05-06 12:25:53 +01:00
|
|
|
;
|
2010-01-11 14:56:49 +00:00
|
|
|
|
2010-05-21 15:18:29 +01:00
|
|
|
$current = libxml_use_internal_errors(true);
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!$dom->schemaValidateSource($source)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(implode("\n", $this->getXmlErrors()));
|
|
|
|
}
|
2010-05-21 15:18:29 +01:00
|
|
|
libxml_use_internal_errors($current);
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws \InvalidArgumentException When non valid tag are found or no extension are found
|
|
|
|
*/
|
|
|
|
protected function validateExtensions($dom, $file)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($dom->documentElement->childNodes as $node) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (!$node instanceof \DOMElement || in_array($node->tagName, array('imports', 'parameters', 'services'))) {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// can it be handled by an extension?
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!static::getExtension($node->namespaceURI)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
throw new \InvalidArgumentException(sprintf('There is no extension able to load the configuration for "%s" (in %s).', $node->tagName, $file));
|
|
|
|
}
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function getXmlErrors()
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$errors = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach (libxml_get_errors() as $error) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$errors[] = sprintf('[%s %s] %s (in %s - line %d, column %d)',
|
|
|
|
LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR',
|
|
|
|
$error->code,
|
|
|
|
trim($error->message),
|
|
|
|
$error->file ? $error->file : 'n/a',
|
|
|
|
$error->line,
|
|
|
|
$error->column
|
|
|
|
);
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
libxml_clear_errors();
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return $errors;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
protected function loadFromExtensions(BuilderConfiguration $configuration, $xml)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach (dom_import_simplexml($xml)->childNodes as $node) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (!$node instanceof \DOMElement || $node->namespaceURI === 'http://www.symfony-project.org/schema/dic/services') {
|
2010-05-06 12:25:53 +01:00
|
|
|
continue;
|
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
$values = static::convertDomElementToArray($node);
|
2010-06-07 06:57:01 +01:00
|
|
|
if (!is_array($values)) {
|
|
|
|
$values = array();
|
|
|
|
}
|
2010-02-12 16:44:52 +00:00
|
|
|
|
2010-06-07 06:57:01 +01:00
|
|
|
$configuration->loadFromExtension($this->getExtension($node->namespaceURI), $node->localName, $values);
|
2010-05-06 12:25:53 +01:00
|
|
|
}
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
/**
|
|
|
|
* Converts a \DomElement object to a PHP array.
|
|
|
|
*
|
|
|
|
* The following rules applies during the conversion:
|
|
|
|
*
|
|
|
|
* * Each tag is converted to a key value or an array
|
|
|
|
* if there is more than one "value"
|
|
|
|
*
|
|
|
|
* * The content of a tag is set under a "value" key (<foo>bar</foo>)
|
|
|
|
* if the tag also has some nested tags
|
|
|
|
*
|
|
|
|
* * The attributes are converted to keys (<foo foo="bar"/>)
|
|
|
|
*
|
|
|
|
* * The nested-tags are converted to keys (<foo><foo>bar</foo></foo>)
|
|
|
|
*
|
|
|
|
* @param \DomElement $element A \DomElement instance
|
|
|
|
*
|
|
|
|
* @return array A PHP array
|
|
|
|
*/
|
|
|
|
static public function convertDomElementToArray(\DomElement $element)
|
2010-01-04 14:26:20 +00:00
|
|
|
{
|
2010-05-06 12:25:53 +01:00
|
|
|
$empty = true;
|
|
|
|
$config = array();
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($element->attributes as $name => $node) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$config[$name] = SimpleXMLElement::phpize($node->value);
|
|
|
|
$empty = false;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
|
|
|
$nodeValue = false;
|
2010-05-07 15:09:11 +01:00
|
|
|
foreach ($element->childNodes as $node) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if ($node instanceof \DOMText) {
|
2010-05-07 15:09:11 +01:00
|
|
|
if (trim($node->nodeValue)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$nodeValue = trim($node->nodeValue);
|
|
|
|
$empty = false;
|
|
|
|
}
|
2010-05-07 15:09:11 +01:00
|
|
|
} elseif (!$node instanceof \DOMComment) {
|
2010-05-08 14:32:30 +01:00
|
|
|
if (isset($config[$node->localName])) {
|
2010-05-07 15:09:11 +01:00
|
|
|
if (!is_array($config[$node->localName])) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$config[$node->localName] = array($config[$node->localName]);
|
|
|
|
}
|
|
|
|
$config[$node->localName][] = static::convertDomElementToArray($node);
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$config[$node->localName] = static::convertDomElementToArray($node);
|
|
|
|
}
|
|
|
|
|
|
|
|
$empty = false;
|
|
|
|
}
|
2010-01-14 17:28:35 +00:00
|
|
|
}
|
2010-05-06 12:25:53 +01:00
|
|
|
|
2010-05-07 15:09:11 +01:00
|
|
|
if (false !== $nodeValue) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$value = SimpleXMLElement::phpize($nodeValue);
|
2010-05-07 15:09:11 +01:00
|
|
|
if (count($config)) {
|
2010-05-06 12:25:53 +01:00
|
|
|
$config['value'] = $value;
|
2010-05-07 15:09:11 +01:00
|
|
|
} else {
|
2010-05-06 12:25:53 +01:00
|
|
|
$config = $value;
|
|
|
|
}
|
2010-01-14 17:28:35 +00:00
|
|
|
}
|
|
|
|
|
2010-05-06 12:25:53 +01:00
|
|
|
return !$empty ? $config : null;
|
2010-01-04 14:26:20 +00:00
|
|
|
}
|
|
|
|
}
|