Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Router: Have a routes collection interface & support in Tracy router panel #94

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/Application/ErrorPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ public function run(Application\Request $request)
$this->logger->log($e, ILogger::EXCEPTION);
}
}
ob_start();
require __DIR__ . '/templates/error.phtml';
return new Application\Responses\TextResponse(ob_get_clean());
return new Application\Responses\CallbackResponse(function () use ($code) {
require __DIR__ . '/templates/error.phtml';
});
}

}
23 changes: 23 additions & 0 deletions src/Application/IRouteList.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/

namespace Nette\Application;

use Nette;


/**
* Routes collection.
*/
interface IRouteList extends \IteratorAggregate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not extend Traversable?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not extend IRouter?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

User-land code can't extend Traversable directly.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interface can extend, but class can't implement.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is funny:

this works:

interface A extends Traversable 
{}

class B implements IteratorAggregate, A
{
    function getIterator() {}
}

this triggers Fatal error: Class B must implement interface Traversable as part of either Iterator or IteratorAggregate:

interface A extends Traversable 
{}

class B implements A, IteratorAggregate
{
    function getIterator() {}
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have encountered this (the need to have interfaces in the correct order) when developing the PR and it is exactly the reason I won't implement it using Traversable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
/**
* @return string
*/
function getModule();

}
37 changes: 37 additions & 0 deletions src/Application/Responses/CallbackResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

/**
* This file is part of the Nette Framework (http://nette.org)
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
*/

namespace Nette\Application\Responses;

use Nette;


/**
* Callback response.
*/
class CallbackResponse extends Nette\Object implements Nette\Application\IResponse
{
/** @var callable */
private $callback;


public function __construct(callable $callback)
{
$this->callback = $callback;
}


/**
* Sends response to output.
* @return void
*/
public function send(Nette\Http\IRequest $httpRequest, Nette\Http\IResponse $httpResponse)
{
call_user_func($this->callback, $httpRequest, $httpResponse);
}

}
2 changes: 1 addition & 1 deletion src/Application/Routers/RouteList.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
/**
* The router broker.
*/
class RouteList extends Nette\Utils\ArrayList implements Nette\Application\IRouter
class RouteList extends Nette\Utils\ArrayList implements Nette\Application\IRouter, Nette\Application\IRouteList
{
/** @var array */
private $cachedRoutes;
Expand Down
2 changes: 1 addition & 1 deletion src/Bridges/ApplicationTracy/RoutingPanel.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public function getPanel()
*/
private function analyse($router, $module = '')
{
if ($router instanceof Routers\RouteList) {
if ($router instanceof Nette\Application\IRouteList) {
foreach ($router as $subRouter) {
$this->analyse($subRouter, $module . $router->getModule());
}
Expand Down
21 changes: 21 additions & 0 deletions tests/Application/CallbackResponse.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

/**
* Test: Nette\Application\Responses\CallbackResponse.
*/

use Nette\Application\Responses\CallbackResponse;
use Nette\Http;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';


test(function () {
$response = new CallbackResponse(function (Http\IRequest $request, Http\IResponse $response) use (& $ok) {
$ok = TRUE;
});
$response->send(new Http\Request(new Http\UrlScript), new Http\Response);
Assert::true($ok);
});