Completed
Pull Request — master (#8)
by
unknown
09:21
created

ConsoleController::options()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * CreateController for sitemap module
4
 *
5
 * @link https://github.com/assayer-pro/yii2-sitemap-module
6
 * @author Serge Larin <[email protected]>
7
 * @copyright 2015 Assayer Pro Company
8
 * @license http://opensource.org/licenses/MIT MIT
9
 */
10
11
namespace assayerpro\sitemap\controllers;
12
13
use Yii;
14
use yii\console\Controller;
15
use yii\helpers\Console;
16
use yii\helpers\Url;
17
18
19
/**
20
 * Generate sitemap for application
21
 *
22
 * @author Serge Larin <[email protected]>
23
 * @package assayerpro\sitemap
24
 */
25
class ConsoleController extends Controller
26
{
27
28
    /**
29
     * @var string folder for sitemaps files
30
     */
31
    public $rootDir = '@webroot';
32
33
    /**
34
     * @inheritdoc
35
     */
36
    public function options($actionID)
37
    {
38
        return array_merge(parent::options($actionID), ['rootDir']);
39
    }
40
41
    /**
42
     * Generate sitemap.xml file
43
     *
44
     * @access public
45
     * @return integer
46
     */
47
    public function actionIndex()
48
    {
49
        $route = '/' . $this->module->id . '/web/index';
50
51
        $rootDir = Yii::getAlias(trim($this->rootDir, '/'));
52
        $file = $rootDir . Url::to([$route], false);
53
54
        $this->stdout("Generate sitemap file.\n", Console::FG_GREEN);
55
        $this->stdout("Rendering sitemap...\n", Console::FG_GREEN);
56
        $generator = $this->module->generator;
57
        $sitemap = $generator->render();
58
59
        $this->stdout("Writing sitemap to $file\n", Console::FG_GREEN);
60
        file_put_contents($file, $sitemap[0]['xml']);
61
        $sitemap_count = count($sitemap);
62
63
        for ($i = 0; $i < $sitemap_count - 1; $i++) {
64
            $file = $rootDir . Url::to([$route, 'id' => $i], false);
65
            $this->stdout("Writing sitemap to $file\n", Console::FG_GREEN);
66
            file_put_contents($file, $sitemap[$i + 1]['xml']);
67
        }
68
        $this->stdout("Done\n", Console::FG_GREEN);
69
        return self::EXIT_CODE_NORMAL;
70
    }
71
}
72