Skip to content

Commit 421256c

Browse files
committed
used Helpers::splitName()
1 parent 559b20c commit 421256c

File tree

4 files changed

+29
-39
lines changed

4 files changed

+29
-39
lines changed

src/Application/Routers/CliRouter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,10 +79,10 @@ public function match(Nette\Http\IRequest $httpRequest)
7979
if (!isset($params[self::PRESENTER_KEY])) {
8080
throw new Nette\InvalidStateException('Missing presenter & action in route definition.');
8181
}
82-
$presenter = $params[self::PRESENTER_KEY];
83-
if ($a = strrpos($presenter, ':')) {
84-
$params[self::PRESENTER_KEY] = substr($presenter, $a + 1);
85-
$presenter = substr($presenter, 0, $a);
82+
list($module, $presenter) = Nette\Application\Helpers::splitName($params[self::PRESENTER_KEY]);
83+
if ($module !== '') {
84+
$params[self::PRESENTER_KEY] = $presenter;
85+
$presenter = $module;
8686
}
8787

8888
return new Application\Request(

src/Application/Routers/Route.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,13 @@ class Route implements Application\IRouter
114114
public function __construct($mask, $metadata = [], $flags = 0)
115115
{
116116
if (is_string($metadata)) {
117-
$a = strrpos($tmp = $metadata, ':');
118-
if (!$a) {
117+
list($presenter, $action) = Nette\Application\Helpers::splitName($metadata);
118+
if (!$presenter) {
119119
throw new Nette\InvalidArgumentException("Second argument must be array or string in format Presenter:action, '$metadata' given.");
120120
}
121-
$metadata = [self::PRESENTER_KEY => substr($tmp, 0, $a)];
122-
if ($a < strlen($tmp) - 1) {
123-
$metadata['action'] = substr($tmp, $a + 1);
121+
$metadata = [self::PRESENTER_KEY => $presenter];
122+
if ($action !== '') {
123+
$metadata['action'] = $action;
124124
}
125125
} elseif ($metadata instanceof \Closure || $metadata instanceof Nette\Callback) {
126126
if ($metadata instanceof Nette\Callback) {

src/Application/Routers/SimpleRouter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,13 @@ class SimpleRouter implements Application\IRouter
3838
public function __construct($defaults = [], $flags = 0)
3939
{
4040
if (is_string($defaults)) {
41-
$a = strrpos($defaults, ':');
42-
if (!$a) {
41+
list($presenter, $action) = Nette\Application\Helpers::splitName($defaults);
42+
if (!$presenter) {
4343
throw new Nette\InvalidArgumentException("Argument must be array or string in format Presenter:action, '$defaults' given.");
4444
}
4545
$defaults = [
46-
self::PRESENTER_KEY => substr($defaults, 0, $a),
47-
'action' => $a === strlen($defaults) - 1 ? Application\UI\Presenter::DEFAULT_ACTION : substr($defaults, $a + 1),
46+
self::PRESENTER_KEY => $presenter,
47+
'action' => $action === '' ? Application\UI\Presenter::DEFAULT_ACTION : $action,
4848
];
4949
}
5050

src/Application/UI/Presenter.php

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Nette;
1111
use Nette\Application;
1212
use Nette\Application\Responses;
13+
use Nette\Application\Helpers;
1314
use Nette\Http;
1415

1516

@@ -497,8 +498,7 @@ public function findLayoutTemplateFile()
497498
*/
498499
public function formatLayoutTemplateFiles()
499500
{
500-
$name = $this->getName();
501-
$presenter = substr($name, strrpos(':' . $name, ':'));
501+
list($module, $presenter) = Helpers::splitName($this->getName());
502502
$layout = $this->layout ? $this->layout : 'layout';
503503
$dir = dirname($this->getReflection()->getFileName());
504504
$dir = is_dir("$dir/templates") ? $dir : dirname($dir);
@@ -509,7 +509,7 @@ public function formatLayoutTemplateFiles()
509509
do {
510510
$list[] = "$dir/templates/@$layout.latte";
511511
$dir = dirname($dir);
512-
} while ($dir && ($name = substr($name, 0, strrpos($name, ':'))));
512+
} while ($dir && $module && (list($module) = Helpers::splitName($module)));
513513
return $list;
514514
}
515515

@@ -520,8 +520,7 @@ public function formatLayoutTemplateFiles()
520520
*/
521521
public function formatTemplateFiles()
522522
{
523-
$name = $this->getName();
524-
$presenter = substr($name, strrpos(':' . $name, ':'));
523+
list(, $presenter) = Helpers::splitName($this->getName());
525524
$dir = dirname($this->getReflection()->getFileName());
526525
$dir = is_dir("$dir/templates") ? $dir : dirname($dir);
527526
return [
@@ -807,13 +806,11 @@ protected function createRequest($component, $destination, array $args, $mode)
807806

808807
// 4) signal or empty
809808
if (!$component instanceof self || substr($destination, -1) === '!') {
810-
$signal = rtrim($destination, '!');
811-
$a = strrpos($signal, ':');
812-
if ($a !== FALSE) {
813-
$component = $component->getComponent(strtr(substr($signal, 0, $a), ':', '-'));
814-
$signal = (string) substr($signal, $a + 1);
809+
list($cname, $signal) = Helpers::splitName(rtrim($destination, '!'));
810+
if ($cname !== '') {
811+
$component = $component->getComponent(strtr($cname, ':', '-'));
815812
}
816-
if ($signal == NULL) { // intentionally ==
813+
if ($signal === '') {
817814
throw new InvalidLinkException('Signal must be non-empty string.');
818815
}
819816
$destination = 'this';
@@ -825,28 +822,21 @@ protected function createRequest($component, $destination, array $args, $mode)
825822

826823
// 5) presenter: action
827824
$current = FALSE;
828-
$a = strrpos($destination, ':');
829-
if ($a === FALSE) {
830-
$action = $destination === 'this' ? $this->action : $destination;
825+
list($presenter, $action) = Helpers::splitName($destination);
826+
if ($presenter === '') {
827+
$action = $destination === 'this' ? $this->action : $action;
831828
$presenter = $this->getName();
832829
$presenterClass = get_class($this);
833830

834831
} else {
835-
$action = (string) substr($destination, $a + 1);
836-
if ($destination[0] === ':') { // absolute
837-
if ($a < 2) {
832+
if ($presenter[0] === ':') { // absolute
833+
$presenter = substr($presenter, 1);
834+
if (!$presenter) {
838835
throw new InvalidLinkException("Missing presenter name in '$destination'.");
839836
}
840-
$presenter = substr($destination, 1, $a - 1);
841-
842837
} else { // relative
843-
$presenter = $this->getName();
844-
$b = strrpos($presenter, ':');
845-
if ($b === FALSE) { // no module
846-
$presenter = substr($destination, 0, $a);
847-
} else { // with module
848-
$presenter = substr($presenter, 0, $b + 1) . substr($destination, 0, $a);
849-
}
838+
list($module, , $sep) = Helpers::splitName($this->getName());
839+
$presenter = $module . $sep . $presenter;
850840
}
851841
if (!$this->presenterFactory) {
852842
throw new Nette\InvalidStateException('Unable to create link to other presenter, service PresenterFactory has not been set.');

0 commit comments

Comments
 (0)