nystudio107 /
craft-autocomplete
| 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 |
||||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||||
| 8 | * @link https://putyourlightson.com |
||||
| 9 | * @copyright Copyright (c) nystudio107 |
||||
|
0 ignored issues
–
show
|
|||||
| 10 | * @copyright Copyright (c) PutYourLightsOn |
||||
|
0 ignored issues
–
show
|
|||||
| 11 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 12 | |||||
| 13 | namespace nystudio107\autocomplete\base; |
||||
| 14 | |||||
| 15 | use Craft; |
||||
| 16 | use nystudio107\autocomplete\Autocomplete; |
||||
| 17 | |||||
| 18 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 19 | * @author nystudio107 |
||||
|
0 ignored issues
–
show
Content of the @author tag must be in the form "Display Name <[email protected]>"
Loading history...
|
|||||
| 20 | * @package autocomplete |
||||
|
0 ignored issues
–
show
|
|||||
| 21 | * @since 1.0.0 |
||||
|
0 ignored issues
–
show
|
|||||
| 22 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 23 | abstract class Generator implements GeneratorInterface |
||||
| 24 | { |
||||
| 25 | // Constants |
||||
| 26 | // ========================================================================= |
||||
| 27 | |||||
| 28 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 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 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 50 | * @inheritDoc |
||||
| 51 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 52 | public static function getGeneratorName(): string |
||||
| 53 | { |
||||
| 54 | return ''; |
||||
| 55 | } |
||||
| 56 | |||||
| 57 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 58 | * @inheritDoc |
||||
| 59 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 60 | public static function getGeneratorStubsPath(): string |
||||
| 61 | { |
||||
| 62 | return Autocomplete::getInstance()->basePath . self::STUBS_DIR; |
||||
| 63 | } |
||||
| 64 | |||||
| 65 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 66 | * @inheritDoc |
||||
| 67 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 68 | public static function generate() |
||||
| 69 | { |
||||
| 70 | } |
||||
| 71 | |||||
| 72 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 73 | * @inheritDoc |
||||
| 74 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 75 | public static function regenerate() |
||||
| 76 | { |
||||
| 77 | } |
||||
| 78 | |||||
| 79 | /** |
||||
|
0 ignored issues
–
show
|
|||||
| 80 | * @inheritDoc |
||||
| 81 | */ |
||||
|
0 ignored issues
–
show
|
|||||
| 82 | public static function delete() |
||||
| 83 | { |
||||
| 84 | $path = static::getGeneratedFilePath(); |
||||
| 85 | if (is_file($path)) { |
||||
| 86 | @unlink($path); |
||||
|
0 ignored issues
–
show
It seems like you do not handle an error condition for
unlink(). This can introduce security issues, and is generally not recommended.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
If you suppress an error, we recommend checking for the error condition explicitly: // For example instead of
@mkdir($dir);
// Better use
if (@mkdir($dir) === false) {
throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
|
|||||
| 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 |
||||
|
0 ignored issues
–
show
|
|||||
| 97 | * @return bool Whether the template was successfully saved |
||||
|
0 ignored issues
–
show
|
|||||
| 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 |