Twigfield::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 1
eloc 1
c 1
b 1
f 0
nc 1
nop 3
dl 0
loc 8
rs 10
1
<?php
2
/**
3
 * Twigfield for Craft CMS
4
 *
5
 * Provides a twig editor field with Twig & Craft API autocomplete
6
 *
7
 * @link      https://nystudio107.com
0 ignored issues
show
Coding Style introduced by
The tag in position 1 should be the @copyright tag
Loading history...
8
 * @copyright Copyright (c) 2022 nystudio107
0 ignored issues
show
Coding Style introduced by
@copyright tag must contain a year and the name of the copyright holder
Loading history...
9
 */
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...
10
11
namespace nystudio107\twigfield;
12
13
use Craft;
14
use craft\events\RegisterTemplateRootsEvent;
15
use craft\helpers\UrlHelper;
16
use craft\i18n\PhpMessageSource;
17
use craft\web\Application as CraftWebApp;
18
use craft\web\View;
19
use nystudio107\twigfield\helpers\Config;
20
use nystudio107\twigfield\models\Settings;
21
use nystudio107\twigfield\services\AutocompleteService;
22
use yii\base\BootstrapInterface;
23
use yii\base\Event;
24
use yii\base\Module;
25
26
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
27
 * @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...
28
 * @package   Twigfield
0 ignored issues
show
Coding Style introduced by
Tag value for @package tag indented incorrectly; expected 1 spaces but found 3
Loading history...
29
 * @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...
30
 *
31
 * @property AutocompleteService $autocomplete
32
 */
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...
33
class Twigfield extends Module implements BootstrapInterface
34
{
35
    // Constants
36
    // =========================================================================
37
38
    const ID = 'twigfield';
39
40
    const DEFAULT_FIELD_TYPE = 'Twigfield';
41
42
    // Public Static Properties
43
    // =========================================================================
44
45
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
46
     * @var Settings The Twigfield config settings
47
     */
48
    public static $settings = null;
49
50
    // Public Methods
51
    // =========================================================================
52
53
    /**
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...
54
     * @inerhitdoc
55
     */
56
    public function __construct($id = self::ID, $parent = null, $config = [])
57
    {
58
        /**
59
         * Explicitly set the $id parameter, as earlier versions of Yii2 look for a
60
         * default parameter, and depend on $id being explicitly set:
61
         * https://github.com/yiisoft/yii2/blob/f3d1534125c9c3dfe8fa65c28a4be5baa822e721/framework/di/Container.php#L436-L448
62
         */
63
        parent::__construct($id, $parent, $config);
64
    }
65
66
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
Parameter $app should have a doc-comment as per coding-style.
Loading history...
67
     * @inerhitDoc
68
     */
0 ignored issues
show
Coding Style introduced by
Missing @return tag in function comment
Loading history...
69
    public function bootstrap($app)
70
    {
71
        // Only bootstrap if this is a CraftWebApp
72
        if (!$app instanceof CraftWebApp) {
73
            return;
74
        }
75
        // Set the instance of this module class, so we can later access it with `Twigfield::getInstance()`
76
        static::setInstance($this);
77
        // Configure our module
78
        $this->configureModule();
79
        // Register our components
80
        $this->registerComponents();
81
        // Register our event handlers
82
        $this->registerEventHandlers();
83
        Craft::info('Twigfield module bootstrapped', __METHOD__);
84
    }
85
86
    // Protected Methods
87
    // =========================================================================
88
89
    /**
90
     * Configure our module
91
     *
92
     * @return void
93
     */
94
    protected function configureModule(): void
95
    {
96
        // Set up our alias
97
        Craft::setAlias('@nystudio107/twigfield', $this->getBasePath());
98
        Craft::setAlias('@twigfieldEndpointUrl', UrlHelper::actionUrl('twigfield/autocomplete/index'));
99
        // Register our module
100
        Craft::$app->setModule($this->id, $this);
101
        // Translation category
102
        $i18n = Craft::$app->getI18n();
103
        /** @noinspection UnSafeIsSetOverArrayInspection */
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...
104
        if (!isset($i18n->translations[$this->id]) && !isset($i18n->translations[$this->id . '*'])) {
105
            $i18n->translations[$this->id] = [
106
                'class' => PhpMessageSource::class,
107
                'sourceLanguage' => 'en-US',
108
                'basePath' => '@nystudio107/twigfield/translations',
109
                'forceTranslation' => true,
110
                'allowOverrides' => true,
111
            ];
112
        }
113
        // Set our settings
114
        $config = Config::getConfigFromFile($this->id);
115
        self::$settings = new Settings($config);
116
    }
117
118
    /**
119
     * Registers our components
120
     *
121
     * @return void
122
     */
123
    public function registerComponents(): void
124
    {
125
        $this->setComponents([
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...
126
            'autocomplete' => AutocompleteService::class,
127
        ]);
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...
128
    }
129
130
    /**
131
     * Registers our event handlers
132
     *
133
     * @return void
134
     */
135
    public function registerEventHandlers(): void
136
    {
137
        // Base CP templates directory
138
        Event::on(View::class, View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) {
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...
139
            if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) {
140
                $e->roots[$this->id] = $baseDir;
141
            }
142
        });
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...
143
        // Base Site templates directory
144
        if (self::$settings->allowFrontendAccess) {
145
            Event::on(View::class, View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS, function (RegisterTemplateRootsEvent $e) {
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...
146
                if (is_dir($baseDir = $this->getBasePath() . DIRECTORY_SEPARATOR . 'templates')) {
147
                    $e->roots[$this->id] = $baseDir;
148
                }
149
            });
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...
150
        }
151
    }
152
}
153