/**
  * Generate the sitemap.
  *
  * For each plugin, it calls dynamically all methods from class
  * `$PLUGIN\Utility\Sitemap`.
  * Each method must be return an array or urls to add to the sitemap.
  * @return string
  * @see MeCms\Utility\Sitemap
  * @uses MeCms\Core\Plugin::all()
  * @uses parse()
  */
 public function generate()
 {
     //Adds the homepage
     $url = [self::parse('/')];
     foreach (Plugin::all() as $plugin) {
         //Sets the class name
         $class = sprintf('\\%s\\Utility\\Sitemap', $plugin);
         //Gets all methods from `$PLUGIN\Utility\Sitemap` class
         $methods = getChildMethods($class);
         if (empty($methods)) {
             continue;
         }
         //Calls all methods
         foreach ($methods as $method) {
             $url = am($url, call_user_func([$class, $method]));
         }
     }
     $xml = Xml::fromArray(['urlset' => ['xmlns:' => 'http://www.sitemaps.org/schemas/sitemap/0.9', 'url' => $url]], ['pretty' => true]);
     return $xml->asXML();
 }
Exemple #2
0
 /**
  * Serialize view vars.
  *
  * ### Special parameters
  * `_xmlOptions` You can set an array of custom options for Xml::fromArray() this way, e.g.
  *   'format' as 'attributes' instead of 'tags'.
  *
  * @param array|string $serialize The name(s) of the view variable(s) that need(s) to be serialized
  * @return string The serialized data
  */
 protected function _serialize($serialize)
 {
     $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
     if ($serialize === true) {
         $serialize = array_diff(array_keys($this->viewVars), $this->_specialVars);
         if (empty($serialize)) {
             $serialize = null;
         } elseif (count($serialize) === 1) {
             $serialize = current($serialize);
         }
     }
     if (is_array($serialize)) {
         $data = [$rootNode => []];
         foreach ($serialize as $alias => $key) {
             if (is_numeric($alias)) {
                 $alias = $key;
             }
             if (array_key_exists($key, $this->viewVars)) {
                 $data[$rootNode][$alias] = $this->viewVars[$key];
             }
         }
     } else {
         $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
         if (is_array($data) && Hash::numeric(array_keys($data))) {
             $data = [$rootNode => [$serialize => $data]];
         }
     }
     $options = [];
     if (isset($this->viewVars['_xmlOptions'])) {
         $options = $this->viewVars['_xmlOptions'];
     }
     if (Configure::read('debug')) {
         $options['pretty'] = true;
     }
     if (isset($options['return']) && strtolower($options['return']) === 'domdocument') {
         return Xml::fromArray($data, $options)->saveXML();
     }
     return Xml::fromArray($data, $options)->asXML();
 }
Exemple #3
0
    /**
     * testNamespace
     *
     * @return void
     */
    public function testNamespace()
    {
        $xml = <<<XML
<root xmlns:ns="http://cakephp.org">
\t<ns:tag id="1">
\t\t<child>good</child>
\t\t<otherchild>bad</otherchild>
\t</ns:tag>
\t<tag>Tag without ns</tag>
</root>
XML;
        $xmlResponse = Xml::build($xml);
        $expected = ['root' => ['ns:tag' => ['@id' => '1', 'child' => 'good', 'otherchild' => 'bad'], 'tag' => 'Tag without ns']];
        $this->assertEquals($expected, Xml::toArray($xmlResponse));
        $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:tag id="1" /><tag><id>1</id></tag></root>');
        $expected = ['root' => ['ns:tag' => ['@id' => '1'], 'tag' => ['id' => '1']]];
        $this->assertEquals($expected, Xml::toArray($xmlResponse));
        $xmlResponse = Xml::build('<root xmlns:ns="http://cakephp.org"><ns:attr>1</ns:attr></root>');
        $expected = ['root' => ['ns:attr' => '1']];
        $this->assertEquals($expected, Xml::toArray($xmlResponse));
        $xmlResponse = Xml::build('<root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>');
        $this->assertEquals($expected, Xml::toArray($xmlResponse));
        $xml = ['root' => ['ns:attr' => ['xmlns:ns' => 'http://cakephp.org', '@' => 1]]];
        $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><ns:attr xmlns:ns="http://cakephp.org">1</ns:attr></root>';
        $xmlResponse = Xml::fromArray($xml);
        $this->assertEquals($expected, str_replace(["\r", "\n"], '', $xmlResponse->asXML()));
        $xml = ['root' => ['tag' => ['xmlns:pref' => 'http://cakephp.org', 'pref:item' => ['item 1', 'item 2']]]];
        $expected = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<root>
  <tag xmlns:pref="http://cakephp.org">
    <pref:item>item 1</pref:item>
    <pref:item>item 2</pref:item>
  </tag>
</root>
XML;
        $xmlResponse = Xml::fromArray($xml);
        $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
        $xml = ['root' => ['tag' => ['xmlns:' => 'http://cakephp.org']]];
        $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root><tag xmlns="http://cakephp.org"/></root>';
        $xmlResponse = Xml::fromArray($xml);
        $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
        $xml = ['root' => ['xmlns:' => 'http://cakephp.org']];
        $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns="http://cakephp.org"/>';
        $xmlResponse = Xml::fromArray($xml);
        $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
        $xml = ['root' => ['xmlns:ns' => 'http://cakephp.org']];
        $expected = '<' . '?xml version="1.0" encoding="UTF-8"?><root xmlns:ns="http://cakephp.org"/>';
        $xmlResponse = Xml::fromArray($xml);
        $this->assertXmlStringEqualsXmlString($expected, $xmlResponse->asXML());
    }
 /**
  * Render sitemap
  * @return string as xml
  */
 public function render()
 {
     // respond as xml
     $this->RequestHandler->respondAs('xml');
     // start xml
     $xml = Xml::build(sprintf($this->_root, $this->_path('Unimatrix/Utility.xsl/sitemap.xsl')));
     // get urls from router
     $this->_router();
     // append urls
     $this->_append($xml, Xml::fromArray(['urlset' => ['url' => $this->_url]]));
     // return xml
     return $xml->asXML();
 }
Exemple #5
0
 /**
  * Serialize view vars.
  *
  * @param array|string $serialize The name(s) of the view variable(s) that need(s) to be serialized
  * @return string The serialized data
  */
 protected function _serialize($serialize)
 {
     $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'response';
     if (is_array($serialize)) {
         $data = [$rootNode => []];
         foreach ($serialize as $alias => $key) {
             if (is_numeric($alias)) {
                 $alias = $key;
             }
             $data[$rootNode][$alias] = $this->viewVars[$key];
         }
     } else {
         $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
         if (is_array($data) && Hash::numeric(array_keys($data))) {
             $data = [$rootNode => [$serialize => $data]];
         }
     }
     $options = [];
     if (Configure::read('debug')) {
         $options['pretty'] = true;
     }
     return Xml::fromArray($data, $options)->asXML();
 }
Exemple #6
0
 /**
  * Serialize view vars.
  *
  * @param string|array $serialize The viewVars that need to be serialized.
  * @return string The serialized data
  * @throws RuntimeException When the prefix is not specified
  */
 protected function _serialize($serialize)
 {
     $rootNode = isset($this->viewVars['_rootNode']) ? $this->viewVars['_rootNode'] : 'channel';
     if (is_array($serialize)) {
         $data = [$rootNode => []];
         foreach ($serialize as $alias => $key) {
             if (is_numeric($alias)) {
                 $alias = $key;
             }
             $data[$rootNode][$alias] = $this->viewVars[$key];
         }
     } else {
         $data = isset($this->viewVars[$serialize]) ? $this->viewVars[$serialize] : null;
         if (is_array($data) && Hash::numeric(array_keys($data))) {
             $data = [$rootNode => [$serialize => $data]];
         }
     }
     $defaults = ['document' => [], 'channel' => [], 'items' => []];
     $data += $defaults;
     if (!empty($data['document']['namespace'])) {
         foreach ($data['document']['namespace'] as $prefix => $url) {
             $this->setNamespace($prefix, $url);
         }
     }
     $channel = $this->channel($data['channel']);
     if (!empty($channel['image']) && empty($channel['image']['title'])) {
         $channel['image']['title'] = $channel['title'];
     }
     foreach ($data['items'] as $item) {
         $channel['item'][] = $this->_prepareOutput($item);
     }
     $array = ['rss' => ['@version' => $this->version, 'channel' => $channel]];
     $namespaces = [];
     foreach ($this->_usedNamespaces as $usedNamespacePrefix) {
         if (!isset($this->_namespaces[$usedNamespacePrefix])) {
             throw new \RuntimeException(sprintf('The prefix %s is not specified.', $usedNamespacePrefix));
         }
         $namespaces['xmlns:' . $usedNamespacePrefix] = $this->_namespaces[$usedNamespacePrefix];
     }
     $array['rss'] += $namespaces;
     $options = [];
     if (Configure::read('debug')) {
         $options['pretty'] = true;
     }
     $output = Xml::fromArray($array, $options)->asXML();
     $output = $this->_replaceCdata($output);
     return $output;
 }