Completed
Push — master ( b93a9c...e7c2e1 )
by Alexey
05:14
created

Sitemap::scanModules()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
nc 3
nop 0
dl 0
loc 12
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * Sitemap
5
 *
6
 * @author Alexey Krupskiy <[email protected]>
7
 * @link http://inji.ru/
8
 * @copyright 2016 Alexey Krupskiy
9
 * @license https://github.com/injitools/cms-Inji/blob/master/LICENSE
10
 */
11
class Sitemap extends Module
12
{
13
    function scanModules()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
14
    {
15
        $modules = Module::getInstalled(App::$primary);
16
        $map = [];
17
        foreach ($modules as $module) {
18
            $map[$module] = App::$cur->$module->sitemap();
19
            if (!$map[$module]) {
20
                unset($map[$module]);
21
            }
22
        }
23
        return $map;
24
    }
25
26
    function generate($map)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28
        header("Content-Type: text/xml");
29
        header("Expires: Thu, 19 Feb 1998 13:24:18 GMT");
30
        header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
31
        header("Cache-Control: no-cache, must-revalidate");
32
        header("Cache-Control: post-check=0,pre-check=0");
33
        header("Cache-Control: max-age=0");
34
        header("Pragma: no-cache");
35
36
        $xml = new \DOMDocument('1.0', 'utf-8');
37
        $xml->formatOutput = true;
38
        $root = $xml->createElement('urlset');
39
        $root->setAttribute("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
40
        $root = $xml->appendChild($root);
41
42
        $addToXml = function ($xml, $parent, $nodeName, $text) {
43
            $node = $parent->appendChild($xml->createElement($nodeName));
44
            $node->appendChild($xml->createTextNode($text));
45
            return $node;
46
        };
47
48
        foreach ($map as $module => $items) {
49
            foreach ($items as $item) {
50
                $url = $xml->createElement('url');
51
                foreach ($item['url'] as $key => $item) {
52
                    $addToXml($xml, $url, $key, $item);
53
                }
54
                $root->appendChild($url);
55
            }
56
        }
57
        echo $xml->saveXML();
58
    }
59
60
}
61