Completed
Pull Request — master (#8)
by
unknown
03:18
created

ConsoleController::actionIndex()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 22
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 22
rs 9.2
cc 2
eloc 16
nc 2
nop 0
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
        $rootDir = trim($this->rootDir, '/');
51
        $file = Yii::getAlias($rootDir) . Url::to([$route], false);
52
53
        $this->stdout("Generate sitemap file.\n", Console::FG_GREEN);
54
        $this->stdout("Rendering sitemap...\n", Console::FG_GREEN);
55
        $sitemap = $this->module->generator->render();
56
57
        $this->stdout("Writing sitemap to $file\n", Console::FG_GREEN);
58
        file_put_contents($file, $sitemap[0]['xml']);
59
        $sitemap_count = count($sitemap);
60
61
        for ($i = 0; $i < $sitemap_count - 1; $i++) {
62
            $file = Yii::getAlias($rootDir) . Url::to([$route, 'id' => $i], false);
63
            $this->stdout("Writing sitemap to $file\n", Console::FG_GREEN);
64
            file_put_contents($file, $sitemap[$i + 1]['xml']);
65
        }
66
        $this->stdout("Done\n", Console::FG_GREEN);
67
        return self::EXIT_CODE_NORMAL;
68
    }
69
}
70