1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Autocomplete plugin for Craft CMS 3.x |
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) 2021 nystudio107 |
|
|
|
|
10
|
|
|
* @copyright Copyright (c) 2021 PutYourLightsOn |
|
|
|
|
11
|
|
|
*/ |
|
|
|
|
12
|
|
|
|
13
|
|
|
namespace nystudio107\autocomplete\base; |
14
|
|
|
|
15
|
|
|
use nystudio107\autocomplete\Autocomplete; |
16
|
|
|
|
17
|
|
|
use Craft; |
18
|
|
|
|
19
|
|
|
/** |
|
|
|
|
20
|
|
|
* @author nystudio107 |
|
|
|
|
21
|
|
|
* @package autocomplete |
|
|
|
|
22
|
|
|
* @since 1.0.0 |
|
|
|
|
23
|
|
|
*/ |
|
|
|
|
24
|
|
|
abstract class Generator implements GeneratorInterface |
25
|
|
|
{ |
26
|
|
|
// Public Static Methods |
27
|
|
|
// ========================================================================= |
28
|
|
|
|
29
|
|
|
/** |
|
|
|
|
30
|
|
|
* @inheritDoc |
31
|
|
|
*/ |
|
|
|
|
32
|
|
|
public static function getGeneratorName(): string |
33
|
|
|
{ |
34
|
|
|
return ''; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
|
|
|
|
38
|
|
|
* @inheritDoc |
39
|
|
|
*/ |
|
|
|
|
40
|
|
|
public static function getGeneratorStubsPath(): string |
41
|
|
|
{ |
42
|
|
|
return Autocomplete::getInstance()->basePath . self::STUBS_DIR; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
|
|
|
|
48
|
|
|
public static function generate() |
49
|
|
|
{ |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
|
|
|
|
53
|
|
|
* @inheritDoc |
54
|
|
|
*/ |
|
|
|
|
55
|
|
|
public static function regenerate() |
56
|
|
|
{ |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
|
|
|
|
60
|
|
|
* @inheritDoc |
61
|
|
|
*/ |
|
|
|
|
62
|
|
|
public static function delete() |
63
|
|
|
{ |
64
|
|
|
unlink(static::getGeneratedFilePath()); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// Protected Static Methods |
68
|
|
|
// ========================================================================= |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Save the template to disk, with variable substitution |
72
|
|
|
* |
73
|
|
|
* @param array $vars key/value variables to be replaced in the stub |
|
|
|
|
74
|
|
|
* @return bool Whether the template was successfully saved |
|
|
|
|
75
|
|
|
*/ |
76
|
|
|
protected static function saveTemplate(array $vars): bool |
77
|
|
|
{ |
78
|
|
|
$stub = file_get_contents(static::getStubFilePath()); |
79
|
|
|
if ($stub) { |
80
|
|
|
$template = str_replace(array_keys($vars), array_values($vars), $stub); |
81
|
|
|
|
82
|
|
|
return !(file_put_contents(static::getGeneratedFilePath(), $template) === false); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return false; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Don't regenerate the file if it already exists |
90
|
|
|
* |
91
|
|
|
* @return bool |
92
|
|
|
*/ |
93
|
|
|
protected static function shouldRegenerateFile(): bool |
94
|
|
|
{ |
95
|
|
|
return !is_file(static::getGeneratedFilePath()); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return a path to the generated autocomplete template file |
100
|
|
|
* |
101
|
|
|
* @return string |
102
|
|
|
*/ |
103
|
|
|
protected static function getGeneratedFilePath(): string |
104
|
|
|
{ |
105
|
|
|
return Craft::$app->getPath()->getCompiledClassesPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::TEMPLATE_EXTENSION; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Return a path to the autocomplete template stub |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
|
|
protected static function getStubFilePath(): string |
114
|
|
|
{ |
115
|
|
|
return static::getGeneratorStubsPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::STUBS_EXTENSION; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|