Skip to content

Commit

Permalink
used Helpers::splitName()
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Apr 23, 2016
1 parent 559b20c commit 421256c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 39 deletions.
8 changes: 4 additions & 4 deletions src/Application/Routers/CliRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,10 @@ public function match(Nette\Http\IRequest $httpRequest)
if (!isset($params[self::PRESENTER_KEY])) {
throw new Nette\InvalidStateException('Missing presenter & action in route definition.');
}
$presenter = $params[self::PRESENTER_KEY];
if ($a = strrpos($presenter, ':')) {
$params[self::PRESENTER_KEY] = substr($presenter, $a + 1);
$presenter = substr($presenter, 0, $a);
list($module, $presenter) = Nette\Application\Helpers::splitName($params[self::PRESENTER_KEY]);
if ($module !== '') {
$params[self::PRESENTER_KEY] = $presenter;
$presenter = $module;
}

return new Application\Request(
Expand Down
10 changes: 5 additions & 5 deletions src/Application/Routers/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,13 @@ class Route implements Application\IRouter
public function __construct($mask, $metadata = [], $flags = 0)
{
if (is_string($metadata)) {
$a = strrpos($tmp = $metadata, ':');
if (!$a) {
list($presenter, $action) = Nette\Application\Helpers::splitName($metadata);
if (!$presenter) {
throw new Nette\InvalidArgumentException("Second argument must be array or string in format Presenter:action, '$metadata' given.");
}
$metadata = [self::PRESENTER_KEY => substr($tmp, 0, $a)];
if ($a < strlen($tmp) - 1) {
$metadata['action'] = substr($tmp, $a + 1);
$metadata = [self::PRESENTER_KEY => $presenter];
if ($action !== '') {
$metadata['action'] = $action;
}
} elseif ($metadata instanceof \Closure || $metadata instanceof Nette\Callback) {
if ($metadata instanceof Nette\Callback) {
Expand Down
8 changes: 4 additions & 4 deletions src/Application/Routers/SimpleRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ class SimpleRouter implements Application\IRouter
public function __construct($defaults = [], $flags = 0)
{
if (is_string($defaults)) {
$a = strrpos($defaults, ':');
if (!$a) {
list($presenter, $action) = Nette\Application\Helpers::splitName($defaults);
if (!$presenter) {
throw new Nette\InvalidArgumentException("Argument must be array or string in format Presenter:action, '$defaults' given.");
}
$defaults = [
self::PRESENTER_KEY => substr($defaults, 0, $a),
'action' => $a === strlen($defaults) - 1 ? Application\UI\Presenter::DEFAULT_ACTION : substr($defaults, $a + 1),
self::PRESENTER_KEY => $presenter,
'action' => $action === '' ? Application\UI\Presenter::DEFAULT_ACTION : $action,
];
}

Expand Down
42 changes: 16 additions & 26 deletions src/Application/UI/Presenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Nette;
use Nette\Application;
use Nette\Application\Responses;
use Nette\Application\Helpers;
use Nette\Http;


Expand Down Expand Up @@ -497,8 +498,7 @@ public function findLayoutTemplateFile()
*/
public function formatLayoutTemplateFiles()
{
$name = $this->getName();
$presenter = substr($name, strrpos(':' . $name, ':'));
list($module, $presenter) = Helpers::splitName($this->getName());
$layout = $this->layout ? $this->layout : 'layout';
$dir = dirname($this->getReflection()->getFileName());
$dir = is_dir("$dir/templates") ? $dir : dirname($dir);
Expand All @@ -509,7 +509,7 @@ public function formatLayoutTemplateFiles()
do {
$list[] = "$dir/templates/@$layout.latte";
$dir = dirname($dir);
} while ($dir && ($name = substr($name, 0, strrpos($name, ':'))));
} while ($dir && $module && (list($module) = Helpers::splitName($module)));
return $list;
}

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

// 4) signal or empty
if (!$component instanceof self || substr($destination, -1) === '!') {
$signal = rtrim($destination, '!');
$a = strrpos($signal, ':');
if ($a !== FALSE) {
$component = $component->getComponent(strtr(substr($signal, 0, $a), ':', '-'));
$signal = (string) substr($signal, $a + 1);
list($cname, $signal) = Helpers::splitName(rtrim($destination, '!'));
if ($cname !== '') {
$component = $component->getComponent(strtr($cname, ':', '-'));
}
if ($signal == NULL) { // intentionally ==
if ($signal === '') {
throw new InvalidLinkException('Signal must be non-empty string.');
}
$destination = 'this';
Expand All @@ -825,28 +822,21 @@ protected function createRequest($component, $destination, array $args, $mode)

// 5) presenter: action
$current = FALSE;
$a = strrpos($destination, ':');
if ($a === FALSE) {
$action = $destination === 'this' ? $this->action : $destination;
list($presenter, $action) = Helpers::splitName($destination);
if ($presenter === '') {
$action = $destination === 'this' ? $this->action : $action;
$presenter = $this->getName();
$presenterClass = get_class($this);

} else {
$action = (string) substr($destination, $a + 1);
if ($destination[0] === ':') { // absolute
if ($a < 2) {
if ($presenter[0] === ':') { // absolute
$presenter = substr($presenter, 1);
if (!$presenter) {
throw new InvalidLinkException("Missing presenter name in '$destination'.");
}
$presenter = substr($destination, 1, $a - 1);

} else { // relative
$presenter = $this->getName();
$b = strrpos($presenter, ':');
if ($b === FALSE) { // no module
$presenter = substr($destination, 0, $a);
} else { // with module
$presenter = substr($presenter, 0, $b + 1) . substr($destination, 0, $a);
}
list($module, , $sep) = Helpers::splitName($this->getName());
$presenter = $module . $sep . $presenter;
}
if (!$this->presenterFactory) {
throw new Nette\InvalidStateException('Unable to create link to other presenter, service PresenterFactory has not been set.');
Expand Down

0 comments on commit 421256c

Please sign in to comment.