protected function addServices(SimpleXMLElement $xml, Config $config)
 {
     foreach ($config->getServices() as $service) {
         if (!$xml->xpath("//config:services/config:service[@id='{$service->getId()}']")) {
             $element = $xml->services->addChild("service");
             $element->addAttribute("id", $service->getId());
             $element->addAttribute("class", $service->getClass());
             if ($service->getScope()) {
                 $element->addAttribute("scope", $service->getScope());
             }
             foreach ($service->getArguments() as $argument) {
                 $serviceXml = $element->addChild("argument");
                 if (null !== $argument->getId()) {
                     $serviceXml->addAttribute("id", $argument->getId());
                 }
                 if (null !== $argument->getType() && Argument::TYPE_STRING !== $argument->getType()) {
                     $serviceXml->addAttribute("type", $argument->getType());
                 }
                 if (null !== $argument->getValue()) {
                     $serviceXml[0] = $argument->getValue();
                 }
             }
             foreach ($service->getTags() as $tag) {
                 $tagXml = $element->addChild("tag");
                 foreach ($tag->getParameters() as $name => $parameter) {
                     $tagXml->addAttribute($name, $parameter);
                 }
             }
         }
     }
 }
Ejemplo n.º 2
0
 protected function saveXml(SimpleXMLElement $xml, $path)
 {
     $dom = new \DOMDocument("1.0");
     $dom->preserveWhiteSpace = false;
     $dom->formatOutput = true;
     $dom->loadXML($xml->asXML());
     $this->writeFile($path, $dom->saveXML(), true, true);
 }
 /**
  * @param \TheliaStudio\Parser\Entity\Route[] $routes
  * @param SimpleXMLElement                    $xml
  */
 protected function addRoutesToXml(array $routes, SimpleXMLElement $xml)
 {
     foreach ($routes as $route) {
         /** @var SimpleXmlElement $element */
         $element = $xml->addChild("route");
         $element->addAttribute("id", $route->getId());
         $element->addAttribute("path", $route->getPath());
         if ($route->getMethods()) {
             $element->addAttribute("methods", $route->getMethods());
         }
         foreach ($route->getDefaults() as $key => $value) {
             $default = $element->addChild("default", $value);
             $default->addAttribute("key", $key);
         }
         foreach ($route->getRequirements() as $key => $value) {
             $requirement = $element->addChild("requirement", $value);
             $requirement->addAttribute("key", $key);
         }
     }
 }
Ejemplo n.º 4
0
 protected function readBehaviors(SimpleXMLElement $xml, Table $table)
 {
     /**
      * @var SimpleXmlElement $behavior
      *
      * report behaviors
      */
     foreach ($xml->xpath(".//behavior") as $behavior) {
         $table->addBehavior($name = $behavior->getAttributeAsPhp('name'));
         if ($name === 'i18n') {
             foreach ($behavior->xpath(".//parameter") as $parameter) {
                 if ($parameter->getAttributeAsPhp('name') === 'i18n_columns') {
                     $i18nColumns = array_map("trim", explode(",", $parameter->getAttributeAsPhp("value")));
                     foreach ($i18nColumns as $i18nColumn) {
                         $table->getColumn($i18nColumn)->setI18n(true);
                     }
                 }
             }
         }
     }
 }
 /**
  * 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
  */
 public static function convertDomElementToArray(\DomElement $element)
 {
     $empty = true;
     $config = array();
     foreach ($element->attributes as $name => $node) {
         $config[$name] = SimpleXMLElement::phpize($node->value);
         $empty = false;
     }
     $nodeValue = false;
     foreach ($element->childNodes as $node) {
         if ($node instanceof \DOMText) {
             if (trim($node->nodeValue)) {
                 $nodeValue = trim($node->nodeValue);
                 $empty = false;
             }
         } elseif (!$node instanceof \DOMComment) {
             if ($node instanceof \DOMElement && '_services' === $node->nodeName) {
                 $value = new Reference($node->getAttribute('id'));
             } else {
                 $value = static::convertDomElementToArray($node);
             }
             $key = $node->localName;
             if (isset($config[$key])) {
                 if (!is_array($config[$key]) || !is_int(key($config[$key]))) {
                     $config[$key] = array($config[$key]);
                 }
                 $config[$key][] = $value;
             } else {
                 $config[$key] = $value;
             }
             $empty = false;
         }
     }
     if (false !== $nodeValue) {
         $value = SimpleXMLElement::phpize($nodeValue);
         if (count($config)) {
             $config['value'] = $value;
         } else {
             $config = $value;
         }
     }
     return !$empty ? $config : null;
 }
    /**
     * Processes anonymous services
     *
     * @param SimpleXMLElement $xml
     * @param string           $file
     */
    private function processAnonymousServices(SimpleXMLElement $xml, $file)
    {
        $definitions = array();
        $count = 0;

        // anonymous services as arguments/properties
        if (false !== $nodes = $xml->xpath('//container:argument[@type="service"][not(@id)]|//container:property[@type="service"][not(@id)]')) {
            foreach ($nodes as $node) {
                // give it a unique name
                $node['id'] = sprintf('%s_%d', md5($file), ++$count);

                $definitions[(string) $node['id']] = array($node->service, $file, false);
                $node->service['id'] = (string) $node['id'];
            }
        }

        // anonymous services "in the wild"
        if (false !== $nodes = $xml->xpath('//container:services/container:service[not(@id)]')) {
            foreach ($nodes as $node) {
                // give it a unique name
                $node['id'] = sprintf('%s_%d', md5($file), ++$count);

                $definitions[(string) $node['id']] = array($node, $file, true);
                $node->service['id'] = (string) $node['id'];
            }
        }

        // resolve definitions
        krsort($definitions);
        foreach ($definitions as $id => $def) {
            // anonymous services are always private
            $def[0]['public'] = false;

            $this->parseDefinition($id, $def[0], $def[1]);

            $oNode = dom_import_simplexml($def[0]);
            if (true === $def[2]) {
                $nNode = new \DOMElement('_services');
                $oNode->parentNode->replaceChild($nNode, $oNode);
                $nNode->setAttribute('id', $id);
            } else {
                $oNode->parentNode->removeChild($oNode);
            }
        }
    }
Ejemplo n.º 7
0
 protected function parseImports(SimpleXMLElement $xml)
 {
     if (false === ($imports = $xml->xpath('//config:imports/config:import'))) {
         return;
     }
     $con = Propel::getWriteConnection(ImportTableMap::DATABASE_NAME);
     $con->beginTransaction();
     try {
         /** @var SimpleXMLElement $import */
         foreach ($imports as $import) {
             $id = (string) $import->getAttributeAsPhp("id");
             $class = (string) $import->getAttributeAsPhp("class");
             $categoryRef = (string) $import->getAttributeAsPhp("category_id");
             if (!class_exists($class)) {
                 throw new \ErrorException("The class \"{$class}\" doesn't exist");
             }
             $category = ImportCategoryQuery::create()->findOneByRef($categoryRef);
             if (null === $category) {
                 throw new \ErrorException("The import category \"{$categoryRef}\" doesn't exist");
             }
             $importModel = ImportQuery::create()->findOneByRef($id);
             if (null === $importModel) {
                 $importModel = new Import();
                 $importModel->setRef($id);
             }
             $importModel->setImportCategory($category)->setHandleClass($class)->save($con);
             /** @var SimpleXMLElement $descriptive */
             foreach ($import->children() as $descriptive) {
                 $locale = $descriptive->getAttributeAsPhp("locale");
                 $title = null;
                 $description = null;
                 /** @var SimpleXMLElement $row */
                 foreach ($descriptive->children() as $row) {
                     switch ($row->getName()) {
                         case "title":
                             $title = (string) $row;
                             break;
                         case "description":
                             $description = (string) $row;
                             break;
                     }
                 }
                 $importModel->setLocale($locale)->setTitle($title)->setDescription($description)->save($con);
             }
         }
         $con->commit();
     } catch (\Exception $e) {
         $con->rollBack();
         Tlog::getInstance()->error($e->getMessage());
     }
 }
Ejemplo n.º 8
0
 /**
  * @param $rawData
  * @return array
  * @throws \Thelia\Core\FileFormat\Formatting\Exception\BadFormattedStringException
  */
 public function rawDecode($rawData)
 {
     try {
         $xml = new SimpleXMLElement($rawData);
     } catch (\Exception $e) {
         $errorMessage = $this->translator->trans("You tried to load a bad formatted XML");
         $this->logger->error($errorMessage . ": " . $e->getMessage());
         throw new BadFormattedStringException($errorMessage);
     }
     $array = [];
     foreach ($xml->children() as $child) {
         $this->recursiveRawDecode($array, $child);
     }
     return $array;
 }
Ejemplo n.º 9
0
 /**
  * Handles form for RPC method call
  *
  * @param $key    Identifier of connection (connected device ID)
  * @param $configParams
  * @param $postVals
  *
  * @return int
  * @throws \ErrorException
  */
 public function handleRPCMethodForm($key, $configParams, $postVals)
 {
     $name = $postVals['rootElemName'];
     $namespace = $postVals['rootElemNamespace'];
     $res = 0;
     $xmlTree = new \SimpleXMLElement('<' . $name . '></' . $name . '>');
     $xmlTree->registerXPathNamespace('xc', 'urn:ietf:params:xml:ns:netconf:base:1.0');
     if ($namespace !== 'false' && $namespace !== '') {
         $xmlTree->registerXPathNamespace('rpcMod', $namespace);
         $xmlTree->addAttribute('xmlns', $namespace);
     }
     // we will go through all post values
     $skipArray = array('rootElemName', 'rootElemNamespace');
     foreach ($postVals as $labelKey => $labelVal) {
         if (in_array($labelKey, $skipArray)) {
             continue;
         }
         $label = $this->divideInputName($labelKey);
         // values[0] - label
         // values[1] - encoded xPath
         if (count($label) != 2) {
             $this->logger->err('RPCMethodForm must contain exactly 2 params, example container_-*-*?1!-*?2!-*?1!', array('values' => $label, 'postKey' => $labelKey));
             throw new \ErrorException("Could not proccess all form fields.");
         } else {
             $xpath = $this->decodeXPath($label[1]);
             $xpath = substr($xpath, 1);
             $node = $this->insertNewElemIntoXMLTree($xmlTree, $xpath, $label[0], $labelVal, '', $addCreateNS = false);
             array_push($skipArray, $labelKey);
         }
     }
     $createString = "\n" . str_replace('<?xml version="1.0"?' . '>', '', $xmlTree->asXML());
     try {
         $res = $this->dataModel->handle("userrpc", array('key' => $key, 'content' => $createString), false, $result);
         /* RPC can return output data in $result */
         if ($res == 0) {
             $this->container->get('request')->getSession()->getFlashBag()->add('success', "RPC method invocation was successful.");
         }
     } catch (\ErrorException $e) {
         $this->logger->warn('Could not invocate RPC method.', array('error' => $e->getMessage(), 'xml' => $createString));
         $this->container->get('request')->getSession()->getFlashBag()->add('error', "Could not invocate RPC method. Error: " . $e->getMessage());
     }
     return $res;
 }
Ejemplo n.º 10
0
 public static function registerNamespace(SimpleXMLElement $xml)
 {
     $xml->registerXPathNamespace("routes", "http://symfony.com/schema/routing");
 }
Ejemplo n.º 11
0
 /**
  * Handles the response
  *
  * @param string $soapMessage The response
  *
  * @throws Exception
  *
  * @return \SimpleXMLElement The response XML <Body> tag
  */
 private function handleResponse($soapMessage)
 {
     // No message is returned, something went wrong, throw a Soap exception which
     // means there was an error communicating with the soap webservice`
     if (!$soapMessage) {
         throw new SoapException($this->getCurlClient()->getError(), $this->getCurlClient()->getErrorNr());
     }
     // Construct a SimpleXMLElement from the message
     $xml = new \SimpleXMLElement($soapMessage);
     if (self::$debug === true) {
         echo "<h2>RESPONSE</h2></hr />";
         echo '<pre>', htmlentities(self::formatXml($xml->asXml())), '</pre>';
     }
     // If the response is a Fault throw a webservice exception
     $fault = $xml->children('soap', true)->Body->Fault;
     if ($fault) {
         throw self::getExceptionForFault($fault->Detail->children()->Error->Code->__toString());
     }
     // Return the body element from the XML
     return $xml->children('soap', true)->Body;
 }
 /**
  * Parse one customergroupacl
  *
  * @param SimpleXMLElement $customerGroupAcl A customergroupacl
  * @param CustomerGroup $customerGroupModel CustomerGroup propel object for who the access have to be created
  *
  * @throws \Exception When an error is detected on xml file (customer group or acl don't exist)
  */
 protected function parseCustomerGroupAcl(SimpleXMLElement $customerGroupAcl, CustomerGroup $customerGroupModel)
 {
     $acl = AclQuery::create()->findOneByCode($customerGroupAcl->getAttributeAsPhp('aclcode'));
     if (null === $customerGroupModel) {
         throw new \Exception($this->translator->trans("Error in %a file the customer group '%s' doesn't exist", ['%a' => $this->xmlFilePath, '%s' => $customerGroupModel->getCode()], CustomerGroupAcl::DOMAIN_MESSAGE));
     }
     if (null === $acl) {
         throw new \Exception($this->translator->trans("Error in %a file the acl '%s' doesn't exist", ['%a' => $this->xmlFilePath, '%s' => $customerGroupAcl->getAttributeAsPhp('aclcode')], CustomerGroupAcl::DOMAIN_MESSAGE));
     }
     $this->parseAccesses($customerGroupAcl->children(), $acl, $customerGroupModel);
 }
Ejemplo n.º 13
0
 public static function registerNamespace(SimpleXMLElement $xml)
 {
     $xml->registerXPathNamespace('config', 'http://thelia.net/schema/dic/config');
 }