Example #1
0
File: XPath.php Project: volux/dom
 /**
  * @param string|array $expression
  * @param null|\DOMNode|\DOMDocument|Document|Html|Table|Form|Attr|Element|Tag|Field|Text|Cdata|Comment $contextnode
  * @param bool $registerNodeNS
  *
  * @return Set
  */
 public function query($expression, \DOMNode $contextnode = null, $registerNodeNS = true)
 {
     $prepared = $this->expression($expression);
     $result = @parent::query($prepared, $contextnode, $registerNodeNS);
     $this->doc->debug(array('result', $expression, $prepared, $result));
     return $this->doc->set($result);
 }
Example #2
0
 public function load($fileOptions)
 {
     self::$dirty = FALSE;
     Event::run('freeswitch.load', $fileOptions);
     if (!isset($fileOptions['filename'])) {
         return FALSE;
     }
     // Did we already load this file? If so, return
     /*if (array_search($fileOptions['filename'], $this->loaded))
       return TRUE;*/
     if (file_exists($fileOptions['filename'])) {
         $savedRoot = $this->xml->getXmlRoot();
         $this->xml->setXmlRoot('');
         // Read from disk and prep the old config
         $oldConfig = new DOMDocument();
         // don't mess with whitespace in the file
         $oldConfig->preserveWhiteSpace = false;
         // format the output in a "pretty" style, per PHP's built-in formatting
         $oldConfig->formatOutput = true;
         try {
             if (file_exists($fileOptions['filename']) and $DFSWait = kohana::config('freeswitch.dfs_wait_time')) {
                 while (!file_get_contents($fileOptions['filename']) and $DFSWait) {
                     kohana::log('debug', 'Waiting for data blocks to replicate to this node...');
                     usleep(250);
                     $DFSWait--;
                 }
             }
             $oldConfig->load($fileOptions['filename']);
         } catch (Exception $e) {
             Kohana::log('error', 'FreeSWITCH -> Unable to load XML from ' . $fileOptions['filename'] . '! Error: ' . $e->getMessage());
             return FALSE;
         }
         // Prep our in-memory document. Note that we create an empty placeholder for importing in case it doesn't already exist, but
         // the place holder does NOT include the last element in the query! Otherwise we will cause issues if there are attributes
         // on the core tag used in the query
         $query = substr($fileOptions['query'], 0, strrpos($fileOptions['query'], '/'));
         $this->xml->set($query);
         $xp = new DOMXPath($this->xml);
         $base = $xp->query($query);
         // Deal with the fact that root include tags are stripped out by FS, but not every file has an include! How annoying...
         if ($oldConfig->documentElement->tagName == 'include') {
             // Because there could be multiple items inside an <include>, but we want to ignore the include, we must import all childnodes
             $oldConfigRoot = $oldConfig->documentElement->childNodes;
             foreach ($oldConfigRoot as $oldConfigNode) {
                 $oldConfigNode = $this->xml->importNode($oldConfigNode, TRUE);
                 $base->item(0)->appendChild($oldConfigNode);
             }
         } else {
             // Import everything - including the root (we are sure there's only one, per XML spec)
             // NOTE: This will "fix" the jenky include issue listed above and ensure every file starts with an include in it's root
             $oldConfigRoot = $oldConfig->documentElement;
             $oldConfigNode = $this->xml->importNode($oldConfigRoot, TRUE);
             $base->item(0)->appendChild($oldConfigNode);
         }
         $this->xml->setXmlRoot($savedRoot);
     }
     // Process any includes in the content that was just loaded (recursively)
 }