diff --git a/src/Application/IPresenterMapper.php b/src/Application/IPresenterMapper.php new file mode 100644 index 000000000..9d04ef92e --- /dev/null +++ b/src/Application/IPresenterMapper.php @@ -0,0 +1,25 @@ + splited mask */ - private $mapping = array( - '*' => array('', '*Module\\', '*Presenter'), - 'Nette' => array('NetteModule\\', '*\\', '*Presenter'), - ); - /** @var array */ private $cache = array(); /** @var Nette\DI\Container */ private $container; + /** @var \Nette\Application\IPresenterMapper */ + private $presenterMapper; + - public function __construct(Nette\DI\Container $container) + public function __construct(Nette\DI\Container $container, IPresenterMapper $presenterMapper) { $this->container = $container; + $this->presenterMapper = $presenterMapper; } @@ -78,7 +76,7 @@ public function getPresenterClass(& $name) throw new InvalidPresenterException("Presenter name must be alphanumeric string, '$name' is invalid."); } - $class = $this->formatPresenterClass($name); + $class = $this->presenterMapper->formatPresenterClass($name); if (!class_exists($class)) { throw new InvalidPresenterException("Cannot load presenter '$name', class '$class' was not found."); } @@ -93,7 +91,7 @@ public function getPresenterClass(& $name) } // canonicalize presenter name - $realName = $this->unformatPresenterClass($class); + $realName = $this->presenterMapper->unformatPresenterClass($class); if ($name !== $realName) { if ($this->caseSensitive) { throw new InvalidPresenterException("Cannot load presenter '$name', case mismatch. Real name is '$realName'."); @@ -108,56 +106,4 @@ public function getPresenterClass(& $name) return $class; } - - /** - * Sets mapping as pairs [module => mask] - * @return self - */ - public function setMapping(array $mapping) - { - foreach ($mapping as $module => $mask) { - if (!preg_match('#^\\\\?([\w\\\\]*\\\\)?(\w*\*\w*?\\\\)?([\w\\\\]*\*\w*)\z#', $mask, $m)) { - throw new Nette\InvalidStateException("Invalid mapping mask '$mask'."); - } - $this->mapping[$module] = array($m[1], $m[2] ?: '*Module\\', $m[3]); - } - return $this; - } - - - /** - * Formats presenter class name from its name. - * @param string - * @return string - */ - public function formatPresenterClass($presenter) - { - $parts = explode(':', $presenter); - $mapping = isset($parts[1], $this->mapping[$parts[0]]) - ? $this->mapping[array_shift($parts)] - : $this->mapping['*']; - - while ($part = array_shift($parts)) { - $mapping[0] .= str_replace('*', $part, $mapping[$parts ? 1 : 2]); - } - return $mapping[0]; - } - - - /** - * Formats presenter name from class name. - * @param string - * @return string - */ - public function unformatPresenterClass($class) - { - foreach ($this->mapping as $module => $mapping) { - $mapping = str_replace(array('\\', '*'), array('\\\\', '(\w+)'), $mapping); - if (preg_match("#^\\\\?$mapping[0]((?:$mapping[1])*)$mapping[2]\\z#i", $class, $matches)) { - return ($module === '*' ? '' : $module . ':') - . preg_replace("#$mapping[1]#iA", '$1:', $matches[1]) . $matches[3]; - } - } - } - } diff --git a/src/Application/PresenterMapper.php b/src/Application/PresenterMapper.php new file mode 100644 index 000000000..cfb5134c2 --- /dev/null +++ b/src/Application/PresenterMapper.php @@ -0,0 +1,73 @@ + splited mask */ + private $mapping = array( + '*' => array('', '*Module\\', '*Presenter'), + 'Nette' => array('NetteModule\\', '*\\', '*Presenter'), + ); + + + /** + * Sets mapping as pairs [module => mask] + * @return self + */ + public function setMapping(array $mapping) + { + foreach ($mapping as $module => $mask) { + if (!preg_match('#^\\\\?([\w\\\\]*\\\\)?(\w*\*\w*?\\\\)?([\w\\\\]*\*\w*)\z#', $mask, $m)) { + throw new Nette\InvalidStateException("Invalid mapping mask '$mask'."); + } + $this->mapping[$module] = array($m[1], $m[2] ?: '*Module\\', $m[3]); + } + return $this; + } + + + /** + * Formats presenter class name from its name. + * @param string + * @return string + */ + public function formatPresenterClass($presenter) + { + $parts = explode(':', $presenter); + $mapping = isset($parts[1], $this->mapping[$parts[0]]) + ? $this->mapping[array_shift($parts)] + : $this->mapping['*']; + + while ($part = array_shift($parts)) { + $mapping[0] .= str_replace('*', $part, $mapping[$parts ? 1 : 2]); + } + return $mapping[0]; + } + + + /** + * Formats presenter name from class name. + * @param string + * @return string + */ + public function unformatPresenterClass($class) + { + foreach ($this->mapping as $module => $mapping) { + $mapping = str_replace(array('\\', '*'), array('\\\\', '(\w+)'), $mapping); + if (preg_match("#^\\\\?$mapping[0]((?:$mapping[1])*)$mapping[2]\\z#i", $class, $matches)) { + return ($module === '*' ? '' : $module . ':') + . preg_replace("#$mapping[1]#iA", '$1:', $matches[1]) . $matches[3]; + } + } + } + +} \ No newline at end of file diff --git a/tests/Application/PresenterFactory.unformatPresenterClass.phpt b/tests/Application/PresenterMapper.unformatPresenterClass.phpt similarity index 94% rename from tests/Application/PresenterFactory.unformatPresenterClass.phpt rename to tests/Application/PresenterMapper.unformatPresenterClass.phpt index 0ed0b3c2a..bde73c173 100644 --- a/tests/Application/PresenterFactory.unformatPresenterClass.phpt +++ b/tests/Application/PresenterMapper.unformatPresenterClass.phpt @@ -4,14 +4,14 @@ * Test: Nette\Application\PresenterFactory. */ -use Nette\Application\PresenterFactory, +use Nette\Application\PresenterMapper, Tester\Assert; require __DIR__ . '/../bootstrap.php'; -$factory = new PresenterFactory(new Nette\DI\Container); +$factory = new PresenterMapper; test(function() use ($factory) { $factory->setMapping(array( diff --git a/tests/Application/PresenterFactory.formatPresenterClass.phpt b/tests/Application/PresenterMappert.formatPresenterClass.phpt similarity index 91% rename from tests/Application/PresenterFactory.formatPresenterClass.phpt rename to tests/Application/PresenterMappert.formatPresenterClass.phpt index 4098d5184..da091569d 100644 --- a/tests/Application/PresenterFactory.formatPresenterClass.phpt +++ b/tests/Application/PresenterMappert.formatPresenterClass.phpt @@ -4,7 +4,7 @@ * Test: Nette\Application\PresenterFactory. */ -use Nette\Application\PresenterFactory, +use Nette\Application\PresenterMapper, Tester\Assert; @@ -12,7 +12,7 @@ require __DIR__ . '/../bootstrap.php'; test(function() { - $factory = new PresenterFactory(new Nette\DI\Container); + $factory = new PresenterMapper; $factory->setMapping(array( 'Foo2' => 'App2\*\*Presenter', @@ -35,7 +35,7 @@ test(function() { test(function() { - $factory = new PresenterFactory(new Nette\DI\Container); + $factory = new PresenterMapper; $factory->setMapping(array( 'Foo2' => 'App2\*Presenter',