Issues (447)

src/RouteMap.php (32 issues)

1
<?php
2
/**
3
 * Route Map plugin for Craft CMS
4
 *
5
 * Returns a list of public routes for elements with URLs
6
 *
7
 * @link      https://nystudio107.com/
0 ignored issues
show
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2017 nystudio107
0 ignored issues
show
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
0 ignored issues
show
PHP version not specified
Loading history...
Missing @category tag in file comment
Loading history...
Missing @package tag in file comment
Loading history...
Missing @author tag in file comment
Loading history...
Missing @license tag in file comment
Loading history...
10
11
namespace nystudio107\routemap;
12
13
use Craft;
14
use craft\base\Element;
15
use craft\base\Plugin;
16
use craft\elements\Entry;
17
use craft\events\ElementEvent;
18
use craft\events\RegisterCacheOptionsEvent;
19
use craft\services\Elements;
20
use craft\utilities\ClearCaches;
21
use craft\web\twig\variables\CraftVariable;
22
use nystudio107\routemap\services\ServicesTrait;
23
use nystudio107\routemap\variables\RouteMapVariable;
24
use yii\base\Event;
25
26
/**
27
 * Class RouteMap
28
 *
29
 * @author    nystudio107
0 ignored issues
show
The tag in position 1 should be the @package tag
Loading history...
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
30
 * @package   RouteMap
0 ignored issues
show
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
31
 * @since     1.0.0
0 ignored issues
show
The tag in position 3 should be the @author tag
Loading history...
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
32
 */
0 ignored issues
show
Missing @category tag in class comment
Loading history...
Missing @license tag in class comment
Loading history...
Missing @link tag in class comment
Loading history...
33
class RouteMap extends Plugin
34
{
35
    // Traits
36
    // =========================================================================
37
38
    use ServicesTrait;
39
40
    // Public Static Properties
41
    // =========================================================================
42
43
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
44
     * @var ?RouteMap
45
     */
46
    public static ?RouteMap $plugin = null;
47
48
    // Public Properties
49
    // =========================================================================
50
51
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
52
     * @var string
53
     */
54
    public string $schemaVersion = '1.0.0';
55
56
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
57
     * @var bool
58
     */
59
    public bool $hasCpSection = false;
60
61
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
62
     * @var bool
63
     */
64
    public bool $hasCpSettings = false;
65
66
    // Public Methods
67
    // =========================================================================
68
69
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
70
     * @inheritdoc
71
     */
0 ignored issues
show
Missing @return tag in function comment
Loading history...
72
    public function init(): void
73
    {
74
        parent::init();
75
        self::$plugin = $this;
76
77
        Event::on(
78
            CraftVariable::class,
79
            CraftVariable::EVENT_INIT,
80
            static function(Event $event): void {
0 ignored issues
show
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
81
                /** @var CraftVariable $variable */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
82
                $variable = $event->sender;
83
                $variable->set('routeMap', RouteMapVariable::class);
84
            }
85
        );
86
87
        // Handler: Elements::EVENT_AFTER_SAVE_ELEMENT
88
        Event::on(
89
            Elements::class,
90
            Elements::EVENT_AFTER_SAVE_ELEMENT,
91
            static function(ElementEvent $event): void {
0 ignored issues
show
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
92
                Craft::debug(
93
                    'Elements::EVENT_AFTER_SAVE_ELEMENT',
94
                    __METHOD__
95
                );
96
                /** @var Element $element */
0 ignored issues
show
The open comment tag must be the only content on the line
Loading history...
Missing short description in doc comment
Loading history...
The close comment tag must be the only content on the line
Loading history...
97
                $element = $event->element;
98
                $bustCache = true;
99
                // Only bust the cache if the element is ENABLED or LIVE
100
                if (($element->getStatus() !== Element::STATUS_ENABLED)
101
                    && ($element->getStatus() !== Entry::STATUS_LIVE)
102
                ) {
103
                    $bustCache = false;
104
                }
105
106
                if ($bustCache) {
107
                    Craft::debug(
108
                        'Cache busted due to saving: ' . $element::class . ' - ' . $element->title,
109
                        __METHOD__
110
                    );
111
                    RouteMap::$plugin->routes->invalidateCache();
112
                }
113
            }
114
        );
115
116
        // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS
117
        Event::on(
118
            ClearCaches::class,
119
            ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
120
            static function(RegisterCacheOptionsEvent $event): void {
0 ignored issues
show
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
121
                $event->options[] = [
122
                    'key' => 'route-map',
123
                    'label' => Craft::t('route-map', 'Route Map Cache'),
124
                    'action' => function(): void {
0 ignored issues
show
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
125
                        RouteMap::$plugin->routes->invalidateCache();
126
                    },
127
                ];
128
            }
129
        );
130
131
        Craft::info(
132
            Craft::t(
133
                'route-map',
134
                '{name} plugin loaded',
135
                ['name' => $this->name]
136
            ),
137
            __METHOD__
138
        );
139
    }
140
141
    // Protected Methods
142
    // =========================================================================
143
}
144