Using Flint as a base controller, using flint in order to cache routes.

skala
Julio Montoya 11 years ago
parent 72ab93c23c
commit 695a036e0c
  1. 12
      config/routing.yml
  2. 7
      main/inc/routes.php
  3. 47
      main/inc/services.php
  4. 32
      src/ChamiloLMS/Controller/BaseController.php
  5. 9
      src/ChamiloLMS/Controller/CommonController.php
  6. 49
      src/ChamiloLMS/Provider/ReflectionControllerProvider.php
  7. 3
      src/ChamiloLMS/Resources/config/route_example.yml

@ -0,0 +1,12 @@
admin_login_check:
pattern: /admin/login_check
admin_logout:
pattern: /admin/logout
home:
path: /home
defaults: { _controller: index.controller:indexAction, culture: en }
example:
resource: "src/ChamiloLMS/Resources/config/route_example.yml"
prefix: /example-route

@ -444,18 +444,11 @@ $app->match('/main/{file}', 'legacy.controller:classicAction', 'GET|POST')
->assert('file', '.+')
->assert('type', '.+');
/** Logout already implemented by the the security service provider */
/* $app->get('/logout', 'index.controller:logoutAction')
->bind('logout')
->after($cleanCourseSession);*/
/** Login form */
$app->match('/login', 'index.controller:loginAction', 'GET|POST')
->bind('login');
/*$app->match('/admin/login-check', 'index.controller:checkLoginAction', 'GET|POST')
->bind('login_check');*/
/** Course home instead of courses/MATHS the new URL is web/courses/MATHS */
$app->match('/courses/{cidReq}/{id_session}/', 'course_home.controller:indexAction', 'GET|POST')

@ -7,8 +7,24 @@
* @package chamilo.services
*/
// Needed to use the "entity" option in symfony forms
use Doctrine\Common\Persistence\AbstractManagerRegistry;
use FranMoreno\Silex\Provider\PagerfantaServiceProvider;
use Silex\Application;
use Silex\ServiceProviderInterface;
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
// Flint
$app->register(new Flint\Provider\ConfigServiceProvider());
$app['root_dir'] = $app['root_sys'];
$app->register(new Flint\Provider\RoutingServiceProvider(), array(
'routing.resource' => $app['sys_config_path'].'routing.yml',
'routing.options' => array(
'cache_dir' => $app['debug'] == true ? null : $app['sys_temp_path']
//'cache_dir' => $app['sys_temp_path']
),
));
// Monolog.
if (is_writable($app['sys_temp_path'])) {
@ -39,7 +55,15 @@ $app->register(new Silex\Provider\HttpCacheServiceProvider(), array(
// http://symfony.com/doc/master/reference/configuration/security.html
$app->register(new Silex\Provider\SecurityServiceProvider(), array(
class SecurityServiceProvider extends \Silex\Provider\SecurityServiceProvider
{
public function addFakeRoute($method, $pattern, $name)
{
// Don't do anything otherwise the closures will be dumped and that leads to fatal errors.
}
}
$app->register(new SecurityServiceProvider, array(
'security.firewalls' => array(
'login' => array(
'pattern' => '^/login$',
@ -63,15 +87,11 @@ $app->register(new Silex\Provider\SecurityServiceProvider(), array(
return $app['orm.em']->getRepository('Entity\User');
}),
'anonymous' => true
),/*
'classic' => array(
'pattern' => '^/.*$'
)*/
)
)
));
// Registering Password encoder.
use Symfony\Component\Security\Core\Encoder\MessageDigestPasswordEncoder;
$app['security.encoder.digest'] = $app->share(function($app) {
// use the sha1 algorithm
// don't base64 encode the password
@ -91,7 +111,14 @@ $app['security.authentication.logout_handler.admin'] = $app->share(function($app
// Role hierarchy
$app['security.role_hierarchy'] = array(
'ROLE_ADMIN' => array('ROLE_QUESTION_MANAGER', 'ROLE_SESSION_MANAGER', 'ROLE_TEACHER', 'ROLE_ALLOWED_TO_SWITCH', 'ROLE_DIRECTOR'),
'ROLE_ADMIN' => array(
'ROLE_QUESTION_MANAGER',
'ROLE_SESSION_MANAGER',
'ROLE_TEACHER',
'ROLE_ALLOWED_TO_SWITCH',
'ROLE_DIRECTOR',
'ROLE_JURY_PRESIDENT'
),
'ROLE_RRHH' => array('ROLE_TEACHER'),
'ROLE_TEACHER' => array('ROLE_STUDENT'),
'ROLE_QUESTION_MANAGER' => array('ROLE_STUDENT', 'ROLE_QUESTION_MANAGER'),
@ -138,9 +165,6 @@ $app->register(new Silex\Provider\FormServiceProvider());
// URL generator provider
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
// Needed to use the "entity" option in symfony forms
use Doctrine\Common\Persistence\AbstractManagerRegistry;
class ManagerRegistry extends AbstractManagerRegistry
{
protected $container;
@ -317,8 +341,6 @@ if (is_writable($app['sys_temp_path'])) {
}
// Pagerfanta settings (Pagination using Doctrine2, arrays, etc)
use FranMoreno\Silex\Provider\PagerfantaServiceProvider;
$app->register(new PagerfantaServiceProvider());
// Custom route params see https://github.com/franmomu/silex-pagerfanta-provider/pull/2
@ -503,7 +525,6 @@ $app->register(new ChamiloServiceProvider(), array());
// Controller as services definitions.
/* @todo use Flint to manage controllers, routes */
$app['pages.controller'] = $app->share(
function () use ($app) {

@ -12,6 +12,7 @@ use Symfony\Component\HttpFoundation\Request;
use Doctrine\ORM\Query;
use Doctrine\ORM\NoResultException;
use Silex\Application;
use Flint\Controller\Controller as FlintController;
use Symfony\Component\Routing\Annotation\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
@ -21,9 +22,10 @@ use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
*
* @abstract
*/
abstract class BaseController
abstract class BaseController extends FlintController
{
protected $app;
protected $pimple;
/**
* @param Application $app
@ -31,6 +33,8 @@ abstract class BaseController
public function __construct(Application $app)
{
$this->app = $app;
// In order to use the Flint Controller
$this->pimple = $app;
}
/**
@ -81,28 +85,16 @@ abstract class BaseController
return $this->get('request');
}
protected function redirect($redirect)
{
return $this->app->redirect($redirect);
}
protected function createNotFoundException($message = 'Not Found', \Exception $previous = null)
{
return $this->app->abort(404, $message);
}
/**
* @param string $item
* @return mixed
*/
protected function get($item)
{
return $this->app[$item];
}
protected function getManager()
{
return $this->app['orm.em'];
return $this->get('orm.em');
}
/**
@ -114,11 +106,13 @@ abstract class BaseController
protected function createUrl($label, $params = array())
{
$links = $this->generateLinks();
$courseCode = $this->getRequest()->get('courseCode');
$params['courseCode'] = $courseCode;
if (isset($links) && is_array($links) && isset($links[$label])) {
$url = $this->get('url_generator')->generate($links[$label], $params);
$url = $this->generateUrl($links[$label], $params);
return $url;
}
return $url = $this->get('url_generator')->generate($links['list_link']);
return $url = $this->generateUrl($links['list_link']);
}
@ -168,7 +162,7 @@ abstract class BaseController
/**
*
* @Route("/{id}", requirements={"id" = "\d+"}, defaults={"foo" = "bar"})
* @Route("/{id}", requirements={"id" = "\d+"})
* @Method({"GET"})
*/
public function readAction($id)
@ -180,7 +174,7 @@ abstract class BaseController
/**
*
* @Route("/{id}/edit", requirements={"id" = "\d+"}, defaults={"foo" = "bar"})
* @Route("/{id}/edit", requirements={"id" = "\d+"})
* @Method({"GET"})
*/
public function editAction($id)
@ -217,7 +211,7 @@ abstract class BaseController
/**
*
* @Route("/{id/delete}", requirements={"id" = "\d+"}, defaults={"foo" = "bar"})
* @Route("/{id}/delete", requirements={"id" = "\d+"})
* @Method({"GET"})
*/
public function deleteAction($id)

@ -12,12 +12,6 @@ use ChamiloLMS\Controller\BaseController;
*/
class CommonController extends BaseController
{
public $languageFiles = array();
public function __construct(Application $app)
{
parent::__construct($app);
}
/**
* {@inheritdoc}
@ -43,7 +37,8 @@ class CommonController extends BaseController
/**
* {@inheritdoc}
*/
protected function getControllerAlias() {
protected function getControllerAlias()
{
}

@ -35,10 +35,14 @@ class ReflectionControllerProvider implements ControllerProviderInterface
/** @var \Silex\ControllerCollection $controllers */
$controllers = $app['controllers_factory'];
// Routes are already cached using Flint
if ($app['debug'] == false) {
return $controllers;
}
$reflection = new \ReflectionClass($app[$this->controllerName]);
$annotationReader = new AnnotationReader();
//$classAnnotations = $annotationReader->getClassAnnotations($reflection);
$routeAnnotation = new Route(array());
$methodAnnotation = new Method(array());
@ -46,37 +50,52 @@ class ReflectionControllerProvider implements ControllerProviderInterface
foreach ($methods as $method) {
$methodName = $method->getName();
$controllerName = $this->controllerName.':'.$methodName;
if (in_array($methodName, array('__construct', 'get', 'getManager'))) {
// Parse only function with the "Action" suffix
if (strpos($methodName, 'Action') === false) {
continue;
}
/** @var Route $routeObject */
$routeObject = $annotationReader->getMethodAnnotation($method, $routeAnnotation);
$req = $routeObject->getRequirements();
//$routeObject->setMethods();
// Getting all annotations
$routeObjects = $annotationReader->getMethodAnnotations($method);
/** @var Method $routeObject */
$methodObject = $annotationReader->getMethodAnnotation($method, $methodAnnotation);
$methodsToString = 'GET';
if ($methodObject) {
$methodsToString = implode('|', $methodObject->getMethods());
}
if ($routeObject) {
$match = $controllers->match($routeObject->getPath(), $controllerName, $methodsToString);
//var_dump($controllerName);
$match->bind($controllerName);
// setRequirements
if (!empty($req)) {
foreach ($req as $key => $value) {
$match->assert($key, $value);
/** @var Route $routeObject */
foreach ($routeObjects as $routeObject) {
if ($routeObject && is_a($routeObject, 'Symfony\Component\Routing\Annotation\Route')) {
$match = $controllers->match($routeObject->getPath(), $controllerName, $methodsToString);
// Setting requirements
if (!empty($req)) {
foreach ($req as $key => $value) {
$match->assert($key, $value);
}
}
$defaults = $routeObject->getDefaults();
// Setting defaults
if (!empty($defaults)) {
foreach ($defaults as $key => $value) {
$match->value($key, $value);
}
}
$match->bind($controllerName);
}
}
}
return $controllers;
}

@ -0,0 +1,3 @@
homepage:
path: /homepage
defaults: { _controller: index.controller:indexAction, culture: en }
Loading…
Cancel
Save