Skip to content

Commit

Permalink
add require and backbone js
Browse files Browse the repository at this point in the history
Signed-off-by: royganor <roy@zend.com>
  • Loading branch information
royatzend committed Apr 18, 2012
1 parent 5635515 commit 23428e0
Show file tree
Hide file tree
Showing 38 changed files with 14,918 additions and 709 deletions.
140 changes: 94 additions & 46 deletions library/Gateful/Instruction/Dispatch.php
@@ -1,4 +1,5 @@
<?php

/*******************************************************************************
* Copyright (c) 2012, 2012 Zend Technologies.
* All rights reserved. This program and the accompanying materials
Expand All @@ -9,6 +10,7 @@
*******************************************************************************/
namespace Gateful\Instruction;

use Zend\Stdlib\CallbackHandler;
use Zend\Http\PhpEnvironment\Response;
use Zend\Http\PhpEnvironment\Request;
use Gateful\Service\ServiceInstruction;
Expand All @@ -18,72 +20,118 @@
* Dispatch request
*/
class Dispatch extends ServiceInstruction {

const ERROR_SERVICE_NOT_FOUND = "Service or callable not found";

/**
* constructs a new dispatch service instruction
*
* @param array $metadata
*/
public function __construct() {
parent::__construct('dispatch');
parent::__construct ( 'dispatch' );
}

protected function internalRun(Request $req, Response $res,
ServiceEvent $event) {
$routeMatch = $event->getRouteMatch();
protected function internalRun(Request $req, Response $res, ServiceEvent $event) {
$routeMatch = $event->getRouteMatch ();

if ($routeMatch != null) {
$serviceName = $routeMatch->getParam('service', 'not-found');
$method = $routeMatch->getParam('method', 'not-found');
$params = $routeMatch->getParams();

if ($serviceName != 'not-found' && $method != 'not-found') {
$service = $event->getLocator()->get($serviceName);
$methodParams = $this
->getServiceMethodParams($serviceName, $method);
$target = array($service, $method);

// find callable
$url = $routeMatch->getParam ( 'url', 'not-found' );
$callable = $routeMatch->getParam ( 'callable', 'not-found' );
if ($url != 'not-found' && $callable != 'not-found') {
$target = new CallbackHandler ( $callable );
} else {
$target = $routeMatch->getParam('call', 'not-found');
$methodParams = $this->getFunctionParams($target);
// TODO Error
$event->setError ( self::ERROR_SERVICE_NOT_FOUND );
}

$data = array('req' => $req, 'res' => $res);
foreach ($methodParams as $param) {
$name = $param->getName();
if ($param->isOptional()) {
$data[$name] = !empty($params[$name]) ? $params[$name]
: $param->getDefaultValue();
} else if (!empty($params[$name])) {
$data[$name] = $params[$name];

// bind request parameters to callback
$data = array (
'req' => $req,
'res' => $res
);
$requestParameters = $this->getRequestParameters ( $req );
$params = $routeMatch->getParams ();
$methodParams = $this->getMethodParams ( $target->getCallback () );
if ($methodParams == null) {
// TODO Error
$event->setError ( self::ERROR_SERVICE_NOT_FOUND );
}
foreach ( $methodParams as $param ) {
$name = $param->getName ();
if ($param->isOptional ()) {
$data [$name] = ! empty ( $params [$name] ) ? $params [$name] : $param->getDefaultValue ();
} else if (! empty ( $params [$name] )) {
$data [$name] = $params [$name];
} else {
if ($req->getMethod() == "GET") {
$p = $req->query()->get($name, null);
if (isset($p)) {
$data[$name] = $p;
}
} else if ($req->getMethod() == "POST") {
$p = $req->post()->get($name, null);
if (isset($p)) {
$data[$name] = $p;
}
$p = $requestParameters [$name];
if (isset ( $p )) {
$data [$name] = $p;
}
}
}
$event->setParam('result', call_user_func_array($target, $data));

// dispatch
$event->setParam ( 'result', call_user_func_array ( $target, $data ) );
}
}


/**
* Resolve the request parameters according to its content type
*
* @param
* $req
* @return array
*/
private function getRequestParameters($req) {
$contentHeader = $req->headers ()->get ( "content-type" );
$requestParameters = array ();
if (! empty ( $contentHeader ) && $contentHeader->getFieldValue () == "application/json") {
$content = file_get_contents ( "php://input" );
if (! empty ( $content )) {
$decoded = json_decode ( $content );
$requestParameters = get_object_vars($decoded);
}

} else {
if ($req->isGet ()) {
$requestParameters = $req->query ()->toArray ();
} else if ($req->isPost ()) {
$requestParameters = $req->post ()->toArray ();
}
}
return $requestParameters;
}
private function getServiceMethodParams($serviceName, $method) {
$classRef = new \ReflectionClass($serviceName);
$className = $classRef->getName();
$methodRef = $classRef->getMethod($method);
$paramsRef = $methodRef->getParameters();
$classRef = new \ReflectionClass ( $serviceName );
$className = $classRef->getName ();
$methodRef = $classRef->getMethod ( $method );
$paramsRef = $methodRef->getParameters ();
return $paramsRef;
}
private function getObjectMethodParams($object, $method) {
$classRef = new \ReflectionObject ( $object );
$methodRef = $classRef->getMethod ( $method );
$paramsRef = $methodRef->getParameters ();
return $paramsRef;
}

private function getFunctionParams($method) {
$methodRef = new \ReflectionFunction($method);
$paramsRef = $methodRef->getParameters();
$methodRef = new \ReflectionFunction ( $method );
$paramsRef = $methodRef->getParameters ();
return $paramsRef;
}

private function getMethodParams($callback) {
if (is_string ( $callback ) || $callback instanceof \Closure) {
return $this->getFunctionParams ( $callback );
} else if (is_array ( $callback ) && count ( $callback ) == 2) {
$dispatcher = $callback [0];
$dispatch = $callback [1];
if (is_object ( $dispatcher )) {
return $this->getObjectMethodParams ( $dispatcher, $dispatch );
} else if (is_string ( $dispatcher )) {
return $this->getServiceMethodParams ( $dispatcher, $dispatch );
}
}
return null;
}
}
115 changes: 62 additions & 53 deletions library/Gateful/Instruction/Router.php
@@ -1,4 +1,5 @@
<?php

/*******************************************************************************
* Copyright (c) 2012, 2012 Zend Technologies.
* All rights reserved. This program and the accompanying materials
Expand All @@ -10,15 +11,10 @@
namespace Gateful\Instruction;

use Zend\Mvc\Router\Http\Segment;

use Zend\Mvc\Router\RouteMatch;

use Zend\Mvc\Router\Http\Literal;

use Zend\Mvc\Router\SimpleRouteStack;

use Zend\Stdlib\CallbackHandler;

use Zend\Http\PhpEnvironment\Response;
use Zend\Http\PhpEnvironment\Request;
use Gateful\Service\ServiceInstruction;
Expand All @@ -28,71 +24,84 @@
* Route request
*/
class Router extends ServiceInstruction {


/**
*
* @var name of the instruction
*/
const NAME = 'router';
const ERROR_SERVICE_NOT_FOUND = 'error-service-not-found';
private $router;

private $router = array();

/**
* parameters
*/
const PARAM_URL = 'url';
const PARAM_METHOD = 'method';
const PARAM_CALLABLE = 'callable';

/**
* constructs a new router service instruction
*
* @param array $metadata
*/
public function __construct() {
parent::__construct(Router::NAME);
$this->router = new SimpleRouteStack();
parent::__construct ( Router::NAME );
}

protected function internalRun(Request $req, Response $response,
ServiceEvent $event) {

//TODO cache router
$routes = $this->getInstructionMetadata($event);
foreach ($routes as $route) {
$this->addRoute($route);
protected function internalRun(Request $req, Response $response, ServiceEvent $event) {

$routes = $this->getInstructionMetadata ( $event );
foreach ( $routes as $route ) {
$this->addRoute ( $route );
}

$routeMatch = $this->router->match($req);

if (!$routeMatch instanceof RouteMatch) {
$event->setError(Router::ERROR_SERVICE_NOT_FOUND);
//TODO handle error

$method = $req->getMethod();
if (isset($this->router[$method])) {
$router = $this->router[$method];
$routeMatch = $router->match ( $req );
}

if (! $routeMatch instanceof RouteMatch) {
// TODO handle error
$event->setError ( Router::ERROR_SERVICE_NOT_FOUND );
} else {
$event->setRouteMatch($routeMatch);
$event->setRouteMatch ( $routeMatch );
}
}

/**
* @param config
* @param route
*
* @param
* config
* @param
* route
*/
private function addRoute($config) {
if (is_array($config)) {
foreach ($config as $name => $route) {
if (is_array($route)) {
foreach ($route as $service => $method) {
if (preg_match("/\\:/", $name)) {
$r = new Segment($name, array(),
array('service' => $service,
'method' => $method));
} else {
$r = new Literal($name,
array('service' => $service,
'method' => $method));
}
$this->router->addRoute($name, $r, $priority = null);
}
} else if (is_callable($route)) {
if (preg_match("/\\:/", $name)) {
$r = new Segment($name, array(),
array('call' => $route));
} else {
$r = new Literal($name, array('call' => $route));
}
$this->router->addRoute($name, $r, $priority = null);
}
}
if (is_array ( $config ) && sizeof ( $config ) > 0) {
$this->addConfig($config);
}
}

private function addConfig($data) {
$name = $data['url'];
if ($name == null) {
throw new \InvalidArgumentException("url parameter must be provided");
}
$method = $data['method'];
if ($method == null) {
throw new \InvalidArgumentException("method parameter must be provided");
}

if (preg_match ( "/\\:/", $name )) {
$r = new Segment ( $name, array (), $data);
} else {
$r = new Literal ( $name, $data );
}

if (!isset($this->router[$method])) {
$this->router[$method] = new SimpleRouteStack ();
}
$this->router[$method]->addRoute ( $name, $r, $priority = null );
}

}

0 comments on commit 23428e0

Please sign in to comment.