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
|
|
|
// Traits |
27
|
|
|
// ========================================================================= |
28
|
|
|
|
29
|
|
|
use GeneratorTrait; |
30
|
|
|
|
31
|
|
|
// Public Static Methods |
32
|
|
|
// ========================================================================= |
33
|
|
|
|
34
|
|
|
/** |
|
|
|
|
35
|
|
|
* @inheritDoc |
36
|
|
|
*/ |
|
|
|
|
37
|
|
|
public static function getGeneratorName(): string |
38
|
|
|
{ |
39
|
|
|
return ''; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
|
|
|
|
43
|
|
|
* @inheritDoc |
44
|
|
|
*/ |
|
|
|
|
45
|
|
|
public static function getGeneratorStubsPath(): string |
46
|
|
|
{ |
47
|
|
|
return Autocomplete::getInstance()->basePath . self::STUBS_DIR; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
|
|
|
|
51
|
|
|
* @inheritDoc |
52
|
|
|
*/ |
|
|
|
|
53
|
|
|
public static function generate() |
54
|
|
|
{ |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
|
|
|
|
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
|
|
|
|
60
|
|
|
public static function regenerate() |
61
|
|
|
{ |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
// Protected Static Methods |
65
|
|
|
// ========================================================================= |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Save the template to disk, with variable substitution |
69
|
|
|
* |
70
|
|
|
* @param array $vars key/value variables to be replaced in the stub |
|
|
|
|
71
|
|
|
* @return bool Whether the template was successfully saved |
|
|
|
|
72
|
|
|
*/ |
73
|
|
|
protected static function saveTemplate(array $vars): bool |
74
|
|
|
{ |
75
|
|
|
$stub = file_get_contents(static::getStubFilePath()); |
76
|
|
|
if ($stub) { |
77
|
|
|
$template = str_replace(array_keys($vars), array_values($vars), $stub); |
78
|
|
|
|
79
|
|
|
return !(file_put_contents(static::getGeneratedFilePath(), $template) === false); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return false; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Don't regenerate the file if it already exists |
87
|
|
|
* |
88
|
|
|
* @return bool |
89
|
|
|
*/ |
90
|
|
|
protected static function shouldRegenerateFile(): bool |
91
|
|
|
{ |
92
|
|
|
$path = self::getGeneratedFilePath(); |
93
|
|
|
|
94
|
|
|
return !is_file($path); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Return a path to the generated autocomplete template file |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
protected static function getGeneratedFilePath(): string |
103
|
|
|
{ |
104
|
|
|
return Craft::$app->getPath()->getCompiledClassesPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::TEMPLATE_EXTENSION; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Return a path to the autocomplete template stub |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
protected static function getStubFilePath(): string |
113
|
|
|
{ |
114
|
|
|
return static::getGeneratorStubsPath() . DIRECTORY_SEPARATOR . static::getGeneratorName() . self::STUBS_EXTENSION; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
} |
118
|
|
|
|