Twigpack::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 3
dl 0
loc 7
rs 10
1
<?php
2
/**
3
 * Twigpack plugin for Craft CMS 3.x
4
 *
5
 * Twigpack is the conduit between Twig and webpack, with manifest.json &
6
 * webpack-dev-server HMR support
7
 *
8
 * @link      https://nystudio107.com/
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
9
 * @copyright Copyright (c) 2018 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
10
 */
0 ignored issues
show
Coding Style introduced by
PHP version not specified
Loading history...
Coding Style introduced by
Missing @category tag in file comment
Loading history...
Coding Style introduced by
Missing @package tag in file comment
Loading history...
Coding Style introduced by
Missing @author tag in file comment
Loading history...
Coding Style introduced by
Missing @license tag in file comment
Loading history...
11
12
namespace nystudio107\twigpack;
13
14
use Craft;
15
use craft\base\Model;
16
use craft\base\Plugin;
17
use craft\cloud\cli\controllers\UpController;
18
use craft\events\CancelableEvent;
19
use craft\events\PluginEvent;
20
use craft\events\RegisterCacheOptionsEvent;
21
use craft\events\TemplateEvent;
22
use craft\services\Plugins;
23
use craft\utilities\ClearCaches;
24
use craft\web\Application;
25
use craft\web\twig\variables\CraftVariable;
26
use craft\web\View;
27
use nystudio107\twigpack\models\Settings;
28
use nystudio107\twigpack\services\Manifest as ManifestService;
29
use nystudio107\twigpack\variables\ManifestVariable;
30
use yii\base\Event;
31
use yii\web\NotFoundHttpException;
32
33
/**
34
 * Class Twigpack
35
 *
36
 * @author    nystudio107
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @package tag
Loading history...
Coding Style introduced by
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
Coding Style introduced by
Tag value for @author tag indented incorrectly; expected 2 spaces but found 4
Loading history...
37
 * @package   Twigpack
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
38
 * @since     1.0.0
0 ignored issues
show
Coding Style introduced by
The tag in position 3 should be the @author tag
Loading history...
Coding Style introduced by
Tag value for @since tag indented incorrectly; expected 3 spaces but found 5
Loading history...
39
 *
40
 * @property  ManifestService $manifest
0 ignored issues
show
Coding Style introduced by
Tag value for @property tag indented incorrectly; expected 1 spaces but found 2
Loading history...
41
 */
0 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
42
class Twigpack extends Plugin
43
{
44
    // Static Properties
45
    // =========================================================================
46
47
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
48
     * @var ?Twigpack
49
     */
50
    public static ?Twigpack $plugin = null;
51
52
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
53
     * @var string
54
     */
55
    public static string $templateName = '';
56
57
    // Static Methods
58
    // =========================================================================
59
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
60
     * @var string
61
     */
62
    public string $schemaVersion = '1.0.0';
63
64
    // Public Properties
65
    // =========================================================================
66
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
67
     * @var bool
68
     */
69
    public bool $hasCpSection = false;
70
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
71
     * @var bool
72
     */
73
    public bool $hasCpSettings = false;
74
75
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $id should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $parent should have a doc-comment as per coding-style.
Loading history...
Coding Style introduced by
Parameter $config should have a doc-comment as per coding-style.
Loading history...
76
     * @inheritdoc
77
     */
78
    public function __construct($id, $parent = null, array $config = [])
79
    {
80
        $config['components'] = [
81
            'manifest' => ManifestService::class,
82
        ];
83
84
        parent::__construct($id, $parent, $config);
85
    }
86
87
    // Public Methods
88
    // =========================================================================
89
90
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
91
     * @inheritdoc
92
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
93
    public function init(): void
94
    {
95
        parent::init();
96
        self::$plugin = $this;
97
        // Install our event listeners
98
        $this->installEventListeners();
99
        // Log that we've loaded
100
        Craft::info(
101
            Craft::t(
102
                'twigpack',
103
                '{name} plugin loaded',
104
                ['name' => $this->name]
105
            ),
106
            __METHOD__
107
        );
108
    }
109
110
    /**
111
     * Clear all the caches!
112
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
113
    public function clearAllCaches(): void
114
    {
115
        // Clear all of Twigpack's caches
116
        self::$plugin->manifest->invalidateCaches();
117
    }
118
119
    /**
120
     * Inject the error entry point JavaScript for auto-reloading of Twig error
121
     * pages
122
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
123
    public function injectErrorEntry(): void
124
    {
125
        if (Craft::$app->getResponse()->isServerError || Craft::$app->getResponse()->isClientError) {
126
            /** @var ?Settings $settings */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
127
            $settings = self::$plugin->getSettings();
0 ignored issues
show
Bug introduced by
The method getSettings() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

127
            /** @scrutinizer ignore-call */ 
128
            $settings = self::$plugin->getSettings();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
128
            if ($settings && !empty($settings->errorEntry) && $settings->useDevServer) {
129
                try {
130
                    $errorEntry = $settings->errorEntry;
131
                    if (is_string($errorEntry)) {
132
                        $errorEntry = [$errorEntry];
133
                    }
134
                    foreach ($errorEntry as $entry) {
135
                        $tag = self::$plugin->manifest->getJsModuleTags($entry, false);
136
                        if ($tag !== null) {
137
                            echo $tag;
138
                        }
139
                    }
140
                } catch (NotFoundHttpException $e) {
141
                    // That's okay, Twigpack will have already logged the error
142
                }
143
            }
144
        }
145
    }
146
147
    // Protected Methods
148
    // =========================================================================
149
150
    /**
151
     * Install our event listeners.
152
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
153
    protected function installEventListeners(): void
154
    {
155
        // Remember the name of the currently rendering template
156
        // Handler: View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE
157
        Event::on(
158
            View::class,
159
            View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
160
            static function(TemplateEvent $event) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
161
                self::$templateName = $event->template;
162
            }
163
        );
164
        // Handler: CraftVariable::EVENT_INIT
165
        Event::on(
166
            CraftVariable::class,
167
            CraftVariable::EVENT_INIT,
168
            static function(Event $event) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
169
                /** @var CraftVariable $variable */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
170
                $variable = $event->sender;
171
                $variable->set('twigpack', ManifestVariable::class);
172
            }
173
        );
174
        // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
175
        Event::on(
176
            Plugins::class,
177
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
178
            function(PluginEvent $event) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
179
                if ($event->plugin === $this) {
0 ignored issues
show
introduced by
The condition $event->plugin === $this is always false.
Loading history...
180
                    // Invalidate our caches after we've been installed
181
                    $this->clearAllCaches();
182
                }
183
            }
184
        );
185
        // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS
186
        Event::on(
187
            ClearCaches::class,
188
            ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
189
            function(RegisterCacheOptionsEvent $event) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
190
                Craft::debug(
191
                    'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS',
192
                    __METHOD__
193
                );
194
                // Register our caches for the Clear Cache Utility
195
                $event->options = array_merge(
196
                    $event->options,
197
                    $this->customAdminCpCacheOptions()
198
                );
199
            }
200
        );
201
        // Clears cache after craft cloud/up is run, which Craft Cloud runs on deploy
202
        // Handler: UpController::EVENT_AFTER_UP
203
        if (class_exists(UpController::class)) {
204
            Event::on(
205
                UpController::class,
206
                UpController::EVENT_AFTER_UP,
207
                function (CancelableEvent $event) {
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

207
                function (/** @scrutinizer ignore-unused */ CancelableEvent $event) {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
208
                    $this->clearAllCaches();
209
                }
210
            );
211
        }
212
213
        // delay attaching event handler to the view component after it is fully configured
214
        $app = Craft::$app;
215
        if ($app->getConfig()->getGeneral()->devMode) {
216
            $app->on(Application::EVENT_BEFORE_REQUEST, function() use ($app) {
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
217
                $app->getView()->on(View::EVENT_END_BODY, [$this, 'injectErrorEntry']);
218
            });
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
219
        }
220
    }
221
222
    /**
223
     * Returns the custom Control Panel cache options.
224
     *
225
     * @return array
226
     */
227
    protected function customAdminCpCacheOptions(): array
228
    {
229
        return [
230
            // Manifest cache
231
            [
232
                'key' => 'twigpack-manifest-cache',
233
                'label' => Craft::t('twigpack', 'Twigpack Manifest Cache'),
234
                'action' => [$this, 'clearAllCaches'],
235
            ],
236
        ];
237
    }
238
239
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
240
     * @inheritdoc
241
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
242
    protected function createSettingsModel(): ?Model
243
    {
244
        return new Settings();
245
    }
246
}
247