Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
Now, in the router configuration can be specified method in the service.
Browse files Browse the repository at this point in the history
  • Loading branch information
ghua committed Sep 16, 2012
1 parent 27863c3 commit 2046e05
Show file tree
Hide file tree
Showing 9 changed files with 733 additions and 22 deletions.
15 changes: 10 additions & 5 deletions Api/Api.php
Expand Up @@ -20,6 +20,9 @@ class Api
protected $api = array('actions' => array());

protected $config;

const Bundle_Action_Regex = '/^([\w]+)Bundle:([\w]+):([\w]+)$/i';
const Service_Regex = '/^([\w]+):([\w]+)$/i';

/**
* Initialize the API.
Expand Down Expand Up @@ -50,13 +53,15 @@ protected function createApi()
$api = array();

foreach($this->config['router']['rules'] as $rule) {
if(!preg_match('/^([\w]+)Bundle:([\w]+):([\w]+)$/', $rule['defaults']['_controller'], $match)) {
if(preg_match($this::Bundle_Action_Regex, $rule['defaults']['_controller'], $match)) {
list($all, $shortBundleName, $controllerName, $methodName) = $match;
$key = sprintf('%s_%s', $shortBundleName, $controllerName);
} elseif(preg_match($this::Service_Regex, $rule['defaults']['_controller'], $match)) {
list($all, $key, $methodName) = $match;
} else {
throw new \InvalidArgumentException();
}
list($all, $shortBundleName, $controllerName, $methodName) = $match;

$key = sprintf('%s_%s', $shortBundleName, $controllerName);


if(!array_key_exists($key, $api) or !is_array($api[$key]))
$api[$key] = array();

Expand Down
14 changes: 14 additions & 0 deletions Controller/ForTestingController.php
Expand Up @@ -2,6 +2,7 @@

namespace Ext\DirectBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Ext\DirectBundle\Response\Response;
use Ext\DirectBundle\Response\FormError;
use Ext\DirectBundle\Response\ValidatorError;
Expand Down Expand Up @@ -81,4 +82,17 @@ public function testExceptionAction()
throw new \Exception('Exception from testExceptionAction');
}

public function __construct($container = null)
{
if($container instanceof ContainerInterface)
$this->container = $container;
}

public function testActionAsService($_data)
{
return $this->container->get('ext_direct')
->createResponse(new Response(), $_data)
->setSuccess(true);
}

}
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Expand Up @@ -63,7 +63,7 @@ private function addRouterSection(ArrayNodeDefinition $rootNode)
->scalarNode('_controller')
->validate()
->ifTrue(function($v) {
return !preg_match('/^[\w]+:[\w]+:[\w]+$/i', $v);
return !preg_match('/^[\w]+(:[\w]+)?:[\w]+$/i', $v);
})
->thenInvalid('This %s format is not supported')
->end()
Expand Down
5 changes: 5 additions & 0 deletions Resources/config/services.yml
Expand Up @@ -2,6 +2,7 @@ parameters:
ext_direct.controller.class: Ext\DirectBundle\Controller\DirectController
ext_direct.response_factory.class: Ext\DirectBundle\Response\ResponseFactory
ext_direct.controller_resolver.class : Ext\DirectBundle\Router\ControllerResolver
ext_direct_test_service.class: Ext\DirectBundle\Controller\ForTestingController

services:
ext_direct.controller:
Expand All @@ -16,3 +17,7 @@ services:
class: %ext_direct.response_factory.class%
scope: request
arguments: [ @request, @ext_direct.controller_resolver, @service_container ]

ext_direct_test_service:
class: %ext_direct_test_service.class%
arguments: [ @service_container ]
53 changes: 52 additions & 1 deletion Resources/doc/index.eng.md
Expand Up @@ -50,7 +50,7 @@ Alternative way, add to deps file:
* error_template - template of validation errors array;
* basic - basic parameters (optional);
* defaults - basic parameters;
* _controller - NodeName:Controller:method;
* _controller - NodeName:Controller:method or или service\_name:methodName;
* params – method has parameters;
* form – formHandler method;
* reader – analogue of store.reader in extjs, it supports:
Expand Down Expand Up @@ -79,6 +79,9 @@ Alternative way, add to deps file:

createCustomer:
defaults: { _controller: AcmeDemoBundle:Demo:createCustomer, params: true, form: true }

chat:
defaults: { _controller: chat_service:chat, params: true, form: true }
</pre>

Example of Use
Expand Down Expand Up @@ -466,6 +469,48 @@ Similar task can also be solved using DirectBundle.

In this example, the errors are specially retrieved from validator service, the response format will be similar to the response of the previous section.

##### Exceptions #####

For assist in the development, router can catch exceptions from symfony2 controller.
Example:

###### Conroller (Symfony2) ######

public function testExceptionAction()
{
throw new \Exception('Exception from testExceptionAction');
}

###### ExtJS application ######

Ext.Direct.on('exception', function(e) {
Ext.Msg.show({
title: 'Exception!',
msg: e.message + ' ' + e.where,
buttons: Ext.Msg.OK,
icon: Ext.MessageBox.ERROR
});
});

Result of calling testException method will be ejection exception:

[
{
"message":"exception 'Exception' with message 'Exception from testExceptionAction'",
"where":"in \/home\/gh\/dev\/symfony2sandbox\/vendor\/bundles\/Ext\/DirectBundle\/Controller\/ForTestingController.php: 81",
"type":"exception",
"tid":3,
"action":
"ExtDirect_ForTesting",
"method":"testException"
}
]

ExtJS can display an error message or do something else.

_Warning! This mode can use only in the develop. In production mode, exceptions are handled by symfony.
By default response is HTTP code 500, with the message: Internal Server Error._

Development
---------

Expand Down Expand Up @@ -494,6 +539,12 @@ For testing add to config_test.yml:

testFormEntityValidationResponse:
defaults: { _controller: ExtDirectBundle:ForTesting:testFormEntityValidationResponse, params: true, form: true }
testServiceAction:
defaults: { _controller: ext_direct_test_service:testActionAsService, params: true }
testException:
defaults: { _controller: ExtDirectBundle:ForTesting:testException }

Also you should add to the configuration of phpunit.xml the following:

Expand Down

0 comments on commit 2046e05

Please sign in to comment.