Passed
Push — v1 ( f6b6a9...6b5c66 )
by Andrew
18:02 queued 13:21
created

src/Twigpack.php (1 issue)

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/
9
 * @copyright Copyright (c) 2018 nystudio107
10
 */
11
12
namespace nystudio107\twigpack;
13
14
use nystudio107\twigpack\services\Manifest as ManifestService;
15
use nystudio107\twigpack\models\Settings;
16
use nystudio107\twigpack\variables\ManifestVariable;
17
18
use Craft;
19
use craft\base\Plugin;
20
use craft\events\DeleteTemplateCachesEvent;
21
use craft\events\PluginEvent;
22
use craft\events\RegisterCacheOptionsEvent;
23
use craft\events\TemplateEvent;
24
use craft\services\Plugins;
25
use craft\services\TemplateCaches;
26
use craft\utilities\ClearCaches;
27
use craft\web\twig\variables\CraftVariable;
28
use craft\web\Application;
29
use craft\web\View;
30
31
use yii\base\Event;
32
use yii\web\NotFoundHttpException;
33
34
/**
35
 * Class Twigpack
36
 *
37
 * @author    nystudio107
38
 * @package   Twigpack
39
 * @since     1.0.0
40
 *
41
 * @property  ManifestService $manifest
42
 */
43
class Twigpack extends Plugin
44
{
45
    // Static Properties
46
    // =========================================================================
47
48
    /**
49
     * @var Twigpack
50
     */
51
    public static $plugin;
52
53
    /**
54
     * @var string
55
     */
56
    public static $templateName;
57
58
    // Public Properties
59
    // =========================================================================
60
61
    /**
62
     * @var string
63
     */
64
    public $schemaVersion = '1.0.0';
65
66
    // Public Methods
67
    // =========================================================================
68
69
    /**
70
     * @inheritdoc
71
     */
72
    public function init()
73
    {
74
        parent::init();
75
        self::$plugin = $this;
76
        // Install our event listeners
77
        $this->installEventListeners();
78
        // Log that we've loaded
79
        Craft::info(
80
            Craft::t(
81
                'twigpack',
82
                '{name} plugin loaded',
83
                ['name' => $this->name]
84
            ),
85
            __METHOD__
86
        );
87
    }
88
89
    /**
90
     * Clear all the caches!
91
     */
92
    public function clearAllCaches()
93
    {
94
        // Clear all of Twigpack's caches
95
        self::$plugin->manifest->invalidateCaches();
96
    }
97
98
    /**
99
     * Inject the error entry point JavaScript for auto-reloading of Twig error
100
     * pages
101
     */
102
    public function injectErrorEntry()
103
    {
104
        if (Craft::$app->getResponse()->isServerError || Craft::$app->getResponse()->isClientError) {
105
            $settings = self::$plugin->getSettings();
106
            if (!empty($settings->errorEntry) && $settings->useDevServer) {
107
                try {
108
                    $errorEntry = $settings->errorEntry;
109
                    if (is_string($errorEntry)) {
110
                        $errorEntry = [$errorEntry];
111
                    }
112
                    foreach ($errorEntry as $entry) {
113
                        $tag = self::$plugin->manifest->getJsModuleTags($entry, false);
114
                        if ($tag !== null) {
115
                            echo $tag;
116
                        }
117
                    }
118
                } catch (NotFoundHttpException $e) {
119
                    // That's okay, Twigpack will have already logged the error
120
                }
121
            }
122
        }
123
    }
124
125
    // Protected Methods
126
    // =========================================================================
127
128
    /**
129
     * Install our event listeners.
130
     */
131
    protected function installEventListeners()
132
    {
133
        // Remember the name of the currently rendering template
134
        // Handler: View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE
135
        Event::on(
136
            View::class,
137
            View::EVENT_BEFORE_RENDER_PAGE_TEMPLATE,
138
            function (TemplateEvent $event) {
139
                self::$templateName = $event->template;
140
            }
141
        );
142
        // Handler: CraftVariable::EVENT_INIT
143
        Event::on(
144
            CraftVariable::class,
145
            CraftVariable::EVENT_INIT,
146
            function (Event $event) {
147
                /** @var CraftVariable $variable */
148
                $variable = $event->sender;
149
                $variable->set('twigpack', ManifestVariable::class);
150
            }
151
        );
152
        // Handler: TemplateCaches::EVENT_AFTER_DELETE_CACHES
153
        Event::on(
154
            TemplateCaches::class,
155
            TemplateCaches::EVENT_AFTER_DELETE_CACHES,
156
            function (DeleteTemplateCachesEvent $event) {
0 ignored issues
show
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

156
            function (/** @scrutinizer ignore-unused */ DeleteTemplateCachesEvent $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...
157
                // Invalidate the caches when template caches are deleted
158
                $this->clearAllCaches();
159
            }
160
        );
161
        // Handler: Plugins::EVENT_AFTER_INSTALL_PLUGIN
162
        Event::on(
163
            Plugins::class,
164
            Plugins::EVENT_AFTER_INSTALL_PLUGIN,
165
            function (PluginEvent $event) {
166
                if ($event->plugin === $this) {
167
                    // Invalidate our caches after we've been installed
168
                    $this->clearAllCaches();
169
                }
170
            }
171
        );
172
        // Handler: ClearCaches::EVENT_REGISTER_CACHE_OPTIONS
173
        Event::on(
174
            ClearCaches::class,
175
            ClearCaches::EVENT_REGISTER_CACHE_OPTIONS,
176
            function (RegisterCacheOptionsEvent $event) {
177
                Craft::debug(
178
                    'ClearCaches::EVENT_REGISTER_CACHE_OPTIONS',
179
                    __METHOD__
180
                );
181
                // Register our caches for the Clear Cache Utility
182
                $event->options = array_merge(
183
                    $event->options,
184
                    $this->customAdminCpCacheOptions()
185
                );
186
            }
187
        );
188
        // delay attaching event handler to the view component after it is fully configured
189
        $app = Craft::$app;
190
        if ($app->getConfig()->getGeneral()->devMode) {
191
            $app->on(Application::EVENT_BEFORE_REQUEST, function () use ($app) {
192
                $app->getView()->on(View::EVENT_END_BODY, [$this, 'injectErrorEntry']);
193
            });
194
        }
195
    }
196
197
    /**
198
     * Returns the custom Control Panel cache options.
199
     *
200
     * @return array
201
     */
202
    protected function customAdminCpCacheOptions(): array
203
    {
204
        return [
205
            // Manifest cache
206
            [
207
                'key' => 'twigpack-manifest-cache',
208
                'label' => Craft::t('twigpack', 'Twigpack Manifest Cache'),
209
                'action' => [$this, 'clearAllCaches'],
210
            ],
211
        ];
212
    }
213
214
    /**
215
     * @inheritdoc
216
     */
217
    protected function createSettingsModel()
218
    {
219
        return new Settings();
220
    }
221
}
222