Skip to content

Commit

Permalink
Introduce route() method that returns the ResourceMetadata that match…
Browse files Browse the repository at this point in the history
…es the given Request object.

Works exactly like getResource() but does not instantiate the Resource object, allowing for creater flexibility with Resource class loading and object creation.
  • Loading branch information
peej committed Feb 18, 2015
1 parent a074a57 commit dde1eff
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 7 deletions.
15 changes: 15 additions & 0 deletions spec/Tonic/ApplicationSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ function it_should_be_initializable()
$this->shouldHaveType('Tonic\Application');
}

/**
* @param \Tonic\Request $request
*/
function it_should_route_a_request($request)
{
$request->getUri()->willReturn('/foo/bar');
$request->getParams()->willReturn(null);
$request->setParams(array())->willReturn(null);

$route = $this->route($request);

$route->shouldHaveType('Tonic\ResourceMetadata');
$route->getClass()->shouldBe('\spec\Tonic\ExampleResource');
}

/**
* @param \Tonic\Request $request
*/
Expand Down
35 changes: 28 additions & 7 deletions src/Tonic/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,14 +150,39 @@ public function uri($className, $params = array())
* Given the request data and the loaded resource metadata, pick the best matching
* resource to handle the request based on URI and priority.
*
* @deprecated You should use the route method instead.
* @param Request $request
* @return Resource
*/
public function getResource($request = NULL)
{
if (!$request) {
$request = new Request();
}

$route = $this->route($request);
$filename = $route->getFilename();
$className = $route->getClass();

if ($filename && is_readable($filename)) {
require_once($filename);
}

return new $className($this, $request);
}

/**
* Given the request data and the loaded resource metadata, pick the best matching
* resource to handle the request based on URI and priority.
*
* @param Request $request
* @return ResourceMetadata
*/
public function route($request = NULL)
{
$matchedResource = NULL;
if (!$request) {
$request= new Request();
$request = new Request();
}
foreach ($this->resources as $className => $resourceMetadata) {
foreach ($resourceMetadata->getUri() as $index => $uri) {
Expand All @@ -179,13 +204,9 @@ public function getResource($request = NULL)
}
}
if ($matchedResource) {
if ($matchedResource[0]->getFilename() && is_readable($matchedResource[0]->getFilename())) {
require_once($matchedResource[0]->getFilename());
}
$request->setParams($matchedResource[1]);

$className = $matchedResource[0]->getClass();
return new $className($this, $request, $matchedResource[1]);
return $matchedResource[0];
} else {
throw new NotFoundException(sprintf('Resource matching URI "%s" not found', $request->uri));
}
Expand All @@ -194,7 +215,7 @@ public function getResource($request = NULL)
/**
* Get the already loaded resource annotation metadata
* @param Tonic/Resource $resource
* @return str[]
* @return ResourceMetadata
*/
public function getResourceMetadata($resource)
{
Expand Down

1 comment on commit dde1eff

@logistiker
Copy link

Choose a reason for hiding this comment

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

This is a welcome change since getResource was quite restrictive.

Please sign in to comment.