Issues (181)

src/Cookies.php (5 issues)

1
<?php
2
3
/**
4
 * Cookies plugin for Craft CMS
5
 *
6
 * @link      https://nystudio107.com/
7
 * @copyright Copyright (c) nystudio107
8
 * @license   MIT License https://opensource.org/licenses/MIT
9
 */
10
11
namespace nystudio107\cookies;
12
13
use Craft;
14
use craft\base\Plugin;
15
use craft\web\twig\variables\CraftVariable;
16
use nystudio107\cookies\services\CookiesService;
17
use nystudio107\cookies\services\ServicesTrait;
18
use nystudio107\cookies\twigextensions\CookiesTwigExtension;
19
use nystudio107\cookies\variables\CookiesVariable;
20
use yii\base\Event;
21
22
/**
23
 * Class Cookies
24
 *
25
 * @author    nystudio107
26
 * @package   Cookies
27
 * @since     1.1.0
28
 */
29
class Cookies extends Plugin
30
{
31
    // Traits
32
    // =========================================================================
33
34
    use ServicesTrait;
35
36
    // Static Public Properties
37
    // =========================================================================
38
39
    /**
40
     * @var null|Cookies
41
     */
42
    public static ?Cookies $plugin = null;
43
44
    // Public Properties
45
    // =========================================================================
46
47
    /**
48
     * @var string
49
     */
50
    public string $schemaVersion = '1.0.0';
51
52
    /**
53
     * @var bool
54
     */
55
    public bool $hasCpSection = false;
56
57
    /**
0 ignored issues
show
Missing short description in doc comment
Loading history...
58
     * @var bool
59
     */
60
    public bool $hasCpSettings = false;
61
62
    // Public Methods
63
    // =========================================================================
64
65
    /**
66
     * @inheritdoc
67
     */
68
    public function __construct($id, $parent = null, array $config = [])
69
    {
70
        $config['components'] = [
71
            'cookies' => CookiesService::class,
72
        ];
73
74
        parent::__construct($id, $parent, $config);
75
    }
76
77
    /**
78
     * @inheritdoc
79
     */
80
    public function init(): void
81
    {
82
        parent::init();
83
        self::$plugin = $this;
84
        $this->name = $this->getName();
85
86
        Event::on(
87
            CraftVariable::class,
88
            CraftVariable::EVENT_INIT,
89
            static function(Event $event): void {
0 ignored issues
show
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
90
                /** @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...
91
                $variable = $event->sender;
92
                $variable->set('cookies', CookiesVariable::class);
93
            }
94
        );
95
96
        // Add in our Twig extensions
97
        Craft::$app->view->registerTwigExtension(new CookiesTwigExtension());
98
99
        Craft::info(
100
            Craft::t(
101
                'cookies',
102
                '{name} plugin loaded',
103
                ['name' => $this->name]
104
            ),
105
            __METHOD__
106
        );
107
    }
108
109
    /**
110
     * Returns the user-facing name of the plugin, which can override the name
111
     * in composer.json
112
     */
113
    public function getName(): string
114
    {
115
        return Craft::t('cookies', 'Cookies');
116
    }
117
}
118