1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Autocomplete module for Craft CMS |
4
|
|
|
* |
5
|
|
|
* Provides Twig template IDE autocomplete of Craft CMS & plugin variables |
6
|
|
|
* |
7
|
|
|
* @link https://nystudio107.com |
|
|
|
|
8
|
|
|
* @link https://putyourlightson.com |
9
|
|
|
* @copyright Copyright (c) nystudio107 |
|
|
|
|
10
|
|
|
* @copyright Copyright (c) PutYourLightsOn |
|
|
|
|
11
|
|
|
*/ |
|
|
|
|
12
|
|
|
|
13
|
|
|
namespace nystudio107\autocomplete\base; |
14
|
|
|
|
15
|
|
|
use Craft; |
16
|
|
|
use nystudio107\autocomplete\Autocomplete; |
17
|
|
|
|
18
|
|
|
/** |
|
|
|
|
19
|
|
|
* @author nystudio107 |
|
|
|
|
20
|
|
|
* @package autocomplete |
|
|
|
|
21
|
|
|
* @since 1.0.0 |
|
|
|
|
22
|
|
|
*/ |
|
|
|
|
23
|
|
|
abstract class Generator implements GeneratorInterface |
24
|
|
|
{ |
25
|
|
|
// Constants |
26
|
|
|
// ========================================================================= |
27
|
|
|
|
28
|
|
|
/** |
|
|
|
|
29
|
|
|
* @event Event The event that is triggered before generating autocomplete classes. |
30
|
|
|
* |
31
|
|
|
* ```php |
32
|
|
|
* use nystudio107\autocomplete\events\DefineGeneratorValuesEvent; |
33
|
|
|
* use nystudio107\autocomplete\generators\AutocompleteTwigExtensionGenerator; |
34
|
|
|
* use yii\base\Event; |
35
|
|
|
* |
36
|
|
|
* Event::on(AutocompleteTwigExtensionGenerator::class, |
37
|
|
|
* AutocompleteTwigExtensionGenerator::EVENT_BEFORE_GENERATE, |
38
|
|
|
* function(DefineGeneratorValuesEvent $event) { |
39
|
|
|
* $event->values['myVariable'] = 'value'; |
40
|
|
|
* } |
41
|
|
|
* ); |
42
|
|
|
* ``` |
43
|
|
|
*/ |
44
|
|
|
public const EVENT_BEFORE_GENERATE = 'beforeGenerate'; |
45
|
|
|
|
46
|
|
|
// Public Static Methods |
47
|
|
|
// ========================================================================= |
48
|
|
|
|
49
|
|
|
/** |
|
|
|
|
50
|
|
|
* @inheritDoc |
51
|
|
|
*/ |
|
|
|
|
52
|
|
|
public static function getGeneratorName(): string |
53
|
|
|
{ |
54
|
|
|
return ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
|
|
|
|
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
|
|
|
|
60
|
|
|
public static function getGeneratorStubsPath(): string |
61
|
|
|
{ |
62
|
|
|
return Autocomplete::getInstance()->basePath . self::STUBS_DIR; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
|
|
|
|
68
|
|
|
public static function generate() |
69
|
|
|
{ |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
|
|
|
|
73
|
|
|
* @inheritDoc |
74
|
|
|
*/ |
|
|
|
|
75
|
|
|
public static function regenerate() |
76
|
|
|
{ |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
|
|
|
|
80
|
|
|
* @inheritDoc |
81
|
|
|
*/ |
|
|
|
|
82
|
|
|
public static function delete() |
83
|
|
|
{ |
84
|
|
|
$path = static::getGeneratedFilePath(); |
85
|
|
|
if (is_file($path)) { |
86
|
|
|
@unlink($path); |
|
|
|
|
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
// Protected Static Methods |
91
|
|
|
// ========================================================================= |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Save the template to disk, with variable substitution |
95
|
|
|
* |
96
|
|
|
* @param array $vars key/value variables to be replaced in the stub |
|
|
|
|
97
|
|
|
* @return bool Whether the template was successfully saved |
|
|
|
|
98
|
|
|
*/ |
99
|
|
|
protected static function saveTemplate(array $vars): bool |
100
|
|
|
{ |
101
|
|
|
$stub = file_get_contents(static::getStubFilePath()); |
102
|
|
|
if ($stub) { |
103
|
|
|
$template = str_replace(array_keys($vars), array_values($vars), $stub); |
104
|
|
|
|
105
|
|
|
return !(file_put_contents(static::getGeneratedFilePath(), $template) === false); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return false; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Don't regenerate the file if it already exists |
113
|
|
|
* |
114
|
|
|
* @return bool |
115
|
|
|
*/ |
116
|
|
|
protected static function shouldRegenerateFile(): bool |
117
|
|
|
{ |
118
|
|
|
return !is_file(static::getGeneratedFilePath()); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Return a path to the generated autocomplete template file |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
protected static function getGeneratedFilePath(): string |
127
|
|
|
{ |
128
|
|
|
return Craft::$app->getPath()->getCompiledClassesPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::TEMPLATE_EXTENSION; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Return a path to the autocomplete template stub |
133
|
|
|
* |
134
|
|
|
* @return string |
135
|
|
|
*/ |
136
|
|
|
protected static function getStubFilePath(): string |
137
|
|
|
{ |
138
|
|
|
return static::getGeneratorStubsPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::STUBS_EXTENSION; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|