-
Notifications
You must be signed in to change notification settings - Fork 53
/
Plugin.php
139 lines (117 loc) · 5.92 KB
/
Plugin.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
namespace Psalm\SymfonyPsalmPlugin;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Psalm\Exception\ConfigException;
use Psalm\Plugin\PluginEntryPointInterface;
use Psalm\Plugin\RegistrationInterface;
use Psalm\SymfonyPsalmPlugin\Handler\AnnotationHandler;
use Psalm\SymfonyPsalmPlugin\Handler\ConsoleHandler;
use Psalm\SymfonyPsalmPlugin\Handler\ContainerDependencyHandler;
use Psalm\SymfonyPsalmPlugin\Handler\ContainerHandler;
use Psalm\SymfonyPsalmPlugin\Handler\DoctrineQueryBuilderHandler;
use Psalm\SymfonyPsalmPlugin\Handler\DoctrineRepositoryHandler;
use Psalm\SymfonyPsalmPlugin\Handler\HeaderBagHandler;
use Psalm\SymfonyPsalmPlugin\Handler\ParameterBagHandler;
use Psalm\SymfonyPsalmPlugin\Handler\RequiredSetterHandler;
use Psalm\SymfonyPsalmPlugin\Provider\FormGetErrorsReturnTypeProvider;
use Psalm\SymfonyPsalmPlugin\Symfony\ContainerMeta;
use Psalm\SymfonyPsalmPlugin\Twig\AnalyzedTemplatesTainter;
use Psalm\SymfonyPsalmPlugin\Twig\CachedTemplatesMapping;
use Psalm\SymfonyPsalmPlugin\Twig\CachedTemplatesTainter;
use Psalm\SymfonyPsalmPlugin\Twig\TemplateFileAnalyzer;
use SimpleXMLElement;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\HttpKernel\Kernel;
/**
* @psalm-suppress UnusedClass
*/
class Plugin implements PluginEntryPointInterface
{
/**
* {@inheritdoc}
*/
public function __invoke(RegistrationInterface $api, SimpleXMLElement $config = null): void
{
require_once __DIR__.'/Handler/HeaderBagHandler.php';
require_once __DIR__.'/Handler/ContainerHandler.php';
require_once __DIR__.'/Handler/ConsoleHandler.php';
require_once __DIR__.'/Handler/ContainerDependencyHandler.php';
require_once __DIR__.'/Handler/RequiredSetterHandler.php';
require_once __DIR__.'/Handler/DoctrineQueryBuilderHandler.php';
require_once __DIR__.'/Provider/FormGetErrorsReturnTypeProvider.php';
$api->registerHooksFromClass(HeaderBagHandler::class);
$api->registerHooksFromClass(ConsoleHandler::class);
$api->registerHooksFromClass(ContainerDependencyHandler::class);
$api->registerHooksFromClass(RequiredSetterHandler::class);
if (class_exists(\Doctrine\ORM\QueryBuilder::class)) {
$api->registerHooksFromClass(DoctrineQueryBuilderHandler::class);
}
if (class_exists(AnnotationRegistry::class)) {
require_once __DIR__.'/Handler/DoctrineRepositoryHandler.php';
/** @psalm-suppress DeprecatedMethod */
AnnotationRegistry::registerLoader('class_exists');
$api->registerHooksFromClass(DoctrineRepositoryHandler::class);
require_once __DIR__.'/Handler/AnnotationHandler.php';
$api->registerHooksFromClass(AnnotationHandler::class);
}
if (isset($config->containerXml)) {
$containerMeta = new ContainerMeta((array) $config->containerXml);
ContainerHandler::init($containerMeta);
try {
TemplateFileAnalyzer::initExtensions(array_filter(array_map(function (array $m) use ($containerMeta) {
if ('addExtension' !== $m[0]) {
return null;
}
try {
return $containerMeta->get($m[1][0])->getClass();
} catch (ServiceNotFoundException $e) {
return null;
}
}, $containerMeta->get('twig')->getMethodCalls())));
} catch (ServiceNotFoundException $e) {
}
require_once __DIR__.'/Handler/ParameterBagHandler.php';
ParameterBagHandler::init($containerMeta);
$api->registerHooksFromClass(ParameterBagHandler::class);
}
$api->registerHooksFromClass(ContainerHandler::class);
$this->addStubs($api, __DIR__.'/Stubs/common');
$this->addStubs($api, __DIR__.'/Stubs/'.Kernel::MAJOR_VERSION);
$this->addStubs($api, __DIR__.'/Stubs/php');
if (isset($config->twigCachePath)) {
$twig_cache_path = getcwd().DIRECTORY_SEPARATOR.ltrim((string) $config->twigCachePath, DIRECTORY_SEPARATOR);
if (!is_dir($twig_cache_path) || !is_readable($twig_cache_path)) {
throw new ConfigException(sprintf('The twig cache directory %s is missing or not readable.', $twig_cache_path));
}
require_once __DIR__.'/Twig/CachedTemplatesTainter.php';
$api->registerHooksFromClass(CachedTemplatesTainter::class);
require_once __DIR__.'/Twig/CachedTemplatesMapping.php';
$api->registerHooksFromClass(CachedTemplatesMapping::class);
CachedTemplatesMapping::setCachePath($twig_cache_path);
}
require_once __DIR__.'/Twig/AnalyzedTemplatesTainter.php';
$api->registerHooksFromClass(AnalyzedTemplatesTainter::class);
if (isset($config->twigRootPath)) {
$twig_root_path = trim((string) $config->twigRootPath, DIRECTORY_SEPARATOR);
$real_path = getcwd().DIRECTORY_SEPARATOR.$twig_root_path;
if (!is_dir($real_path) || !is_readable($real_path)) {
throw new ConfigException(sprintf('The twig templates root directory %s is missing or not readable.', $twig_root_path));
}
TemplateFileAnalyzer::setTemplateRootPath($twig_root_path);
}
$api->registerHooksFromClass(FormGetErrorsReturnTypeProvider::class);
}
private function addStubs(RegistrationInterface $api, string $path): void
{
if (!is_dir($path)) {
// e.g. looking for stubs for version 3, but there aren't any at time of writing, so don't try and load them.
return;
}
$a = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($path));
foreach ($a as $file) {
if (!$file->isDir()) {
$api->addStubFile($file->getPathname());
}
}
}
}