|
| 1 | +<?php declare(strict_types = 1); |
| 2 | + |
| 3 | +namespace PHPStan\Nette; |
| 4 | + |
| 5 | +use Nette\Application\IPresenterFactory; |
| 6 | +use Nette\Application\PresenterFactory; |
| 7 | +use PHPStan\Exceptions\LinkCheckFailedException; |
| 8 | +use PHPStan\ShouldNotHappenException; |
| 9 | +use ReflectionClass; |
| 10 | +use ReflectionException; |
| 11 | +use function count; |
| 12 | +use function is_array; |
| 13 | +use function is_string; |
| 14 | +use function preg_match; |
| 15 | +use function preg_replace; |
| 16 | +use function sprintf; |
| 17 | +use function str_replace; |
| 18 | +use function strrpos; |
| 19 | +use function substr; |
| 20 | + |
| 21 | +class PresenterResolver |
| 22 | +{ |
| 23 | + |
| 24 | + /** @var array<string, string|array{0: string, 1: string, 2: string}> */ |
| 25 | + protected $mapping; |
| 26 | + |
| 27 | + /** @var ContainerResolver */ |
| 28 | + private $containerResolver; |
| 29 | + |
| 30 | + /** @var IPresenterFactory */ |
| 31 | + private $presenterFactory; |
| 32 | + |
| 33 | + /** |
| 34 | + * @param array<string, string|array{0: string, 1: string, 2: string}> $mapping |
| 35 | + */ |
| 36 | + public function __construct(array $mapping, ContainerResolver $containerResolver) |
| 37 | + { |
| 38 | + $this->mapping = $mapping; |
| 39 | + $this->containerResolver = $containerResolver; |
| 40 | + } |
| 41 | + |
| 42 | + public function isInitialized(): bool |
| 43 | + { |
| 44 | + return $this->getCurrentMapping() !== []; |
| 45 | + } |
| 46 | + |
| 47 | + protected function getPresenterFactory(): IPresenterFactory |
| 48 | + { |
| 49 | + if ($this->presenterFactory === null) { |
| 50 | + if ($this->containerResolver->getContainer() !== null) { |
| 51 | + $this->presenterFactory = $this->containerResolver->getContainer()->getByType(IPresenterFactory::class); |
| 52 | + } else { |
| 53 | + $this->presenterFactory = new PresenterFactory(); |
| 54 | + $this->presenterFactory->setMapping($this->mapping); |
| 55 | + } |
| 56 | + } |
| 57 | + return $this->presenterFactory; |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * @return array<string, array{0: string, 1: string, 2: string}> |
| 62 | + * @throws ShouldNotHappenException |
| 63 | + * @throws ReflectionException |
| 64 | + */ |
| 65 | + protected function getCurrentMapping(): array |
| 66 | + { |
| 67 | + if ($this->mapping !== []) { |
| 68 | + $convertedMapping = []; |
| 69 | + foreach ($this->mapping as $module => $mask) { |
| 70 | + if (is_string($mask)) { |
| 71 | + if (preg_match('#^\\\\?([\w\\\\]*\\\\)?(\w*\*\w*?\\\\)?([\w\\\\]*\*\w*)$#D', $mask, $m) !== 1) { |
| 72 | + throw new ShouldNotHappenException(sprintf("Invalid mapping mask '%s' in parameters.nette.applicationMapping.", $mask)); |
| 73 | + } |
| 74 | + $convertedMapping[$module] = [$m[1], $m[2] !== '' ? $m[2] : '*Module\\', $m[3]]; |
| 75 | + } elseif (is_array($mask) && count($mask) === 3) { /** @phpstan-ignore-line */ |
| 76 | + $convertedMapping[$module] = [$mask[0] !== '' ? $mask[0] . '\\' : '', $mask[1] . '\\', $mask[2]]; |
| 77 | + } else { |
| 78 | + throw new ShouldNotHappenException(sprintf('Invalid mapping mask for module %s in parameters.nette.applicationMapping.', $module)); |
| 79 | + } |
| 80 | + } |
| 81 | + return $convertedMapping; |
| 82 | + } |
| 83 | + |
| 84 | + $presenterFactory = $this->getPresenterFactory(); |
| 85 | + if (!$presenterFactory instanceof PresenterFactory) { |
| 86 | + throw new ShouldNotHappenException( |
| 87 | + 'PresenterFactory in your container is not instance of Nette\Application\PresenterFactory. We cannot get mapping from it.' . |
| 88 | + ' Either set your mappings explicitly in parameters.nette.applicationMapping ' . |
| 89 | + ' or replace service nettePresenterResolver with your own override of getCurrentMapping() or unformatPresenterClass().' |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + $mappingPropertyReflection = (new ReflectionClass($presenterFactory))->getProperty('mapping'); |
| 94 | + $mappingPropertyReflection->setAccessible(true); |
| 95 | + /** @var array<string, array{0: string, 1: string, 2: string}> $mapping */ |
| 96 | + $mapping = $mappingPropertyReflection->getValue($presenterFactory); |
| 97 | + |
| 98 | + return $mapping; |
| 99 | + } |
| 100 | + |
| 101 | + public function getPresenterClassByName(string $name, ?string $currentPresenterClass = null): string |
| 102 | + { |
| 103 | + $name = $this->resolvePresenterName($name, $currentPresenterClass); |
| 104 | + return $this->getPresenterFactory()->getPresenterClass($name); |
| 105 | + } |
| 106 | + |
| 107 | + public function resolvePresenterName(string $name, ?string $currentPresenterClass = null): string |
| 108 | + { |
| 109 | + if ($name[0] === ':') { |
| 110 | + return substr($name, 1); |
| 111 | + } |
| 112 | + |
| 113 | + if ($currentPresenterClass === null) { |
| 114 | + throw new LinkCheckFailedException(sprintf("Cannot resolve relative presenter name '%s' - current presenter is not set.", $name)); |
| 115 | + } |
| 116 | + |
| 117 | + $currentName = $this->unformatPresenterClass($currentPresenterClass); |
| 118 | + $currentNameSepPos = strrpos($currentName, ':'); |
| 119 | + if ($currentNameSepPos !== false && $currentNameSepPos !== 0) { |
| 120 | + $currentModule = substr($currentName, 0, $currentNameSepPos); |
| 121 | + $currentPresenter = substr($currentName, $currentNameSepPos + 1); |
| 122 | + } else { |
| 123 | + $currentModule = ''; |
| 124 | + $currentPresenter = $currentName; |
| 125 | + } |
| 126 | + |
| 127 | + if ($name === 'this') { |
| 128 | + return $currentModule . ':' . $currentPresenter; |
| 129 | + } |
| 130 | + |
| 131 | + return $currentModule . ':' . $name; |
| 132 | + } |
| 133 | + |
| 134 | + protected function unformatPresenterClass(string $class): string |
| 135 | + { |
| 136 | + foreach ($this->getCurrentMapping() as $module => $mapping) { |
| 137 | + $mapping = str_replace(['\\', '*'], ['\\\\', '(\w+)'], $mapping); |
| 138 | + if (preg_match('#^\\\\?' . $mapping[0] . '((?:' . $mapping[1] . ')*)' . $mapping[2] . '$#Di', $class, $matches) === 1) { |
| 139 | + return ($module === '*' ? '' : $module . ':') |
| 140 | + . preg_replace('#' . $mapping[1] . '#iA', '$1:', $matches[1]) . $matches[3]; |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + throw new LinkCheckFailedException(sprintf("Cannot convert presenter class '%s' to presenter name. No matching mapping found.", $class)); |
| 145 | + } |
| 146 | + |
| 147 | +} |
0 commit comments