Skip to content

Commit

Permalink
Added the ContainerAware interface. The controller now implements it.
Browse files Browse the repository at this point in the history
  • Loading branch information
enyo committed Feb 9, 2012
1 parent db0ea67 commit b9c5b73
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 9 deletions.
44 changes: 44 additions & 0 deletions library/ContainerAware/ContainerAware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/**
* This file contains the ContainerAware interface.
*
* @author Matthias Loitsch <developer@ma.tthias.com>
* @copyright Copyright (c) 2010, Matthias Loitsch
* @package Config
*/

/**
* A class has to implement this interface to receive the container.
*/
interface ContainerAware {

/**
* Has to call verifyRequiredServices()
*
* @param sfServiceContainer $container
*/
public function setContainer($container);

/**
* @return sfServiceContainer
*/
public function getContainer();

/**
* Has to return a list of required services.
*
* @return array
*/
public function getRequiredServices();

/**
* Checks if all services are correctly implemented
*
* @param sfServiceContainer $container
* @throws Exception if not everyting was ok.
*/
public function verifyRequiredServices($container);


}
42 changes: 38 additions & 4 deletions library/Controller/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@
*/
require_class('DispatcherException', dirname(__FILE__) . '/DispatcherExceptions.php');

/**
* Including ContainerAware interface
*/
require_interface('ContainerAware');

/**
* Including model
*/
Expand All @@ -37,7 +42,7 @@
* @copyright Copyright (c) 2009, Matthias Loitsch
* @package Controller
*/
abstract class Controller {
abstract class Controller implements ContainerAware {

/**
* @var sfServiceContainer
Expand Down Expand Up @@ -73,14 +78,43 @@ abstract class Controller {
protected $router;

/**
* @param sfServiceContainer $container
* @param Router $router
*/
public function __construct(sfServiceContainer $container, Router $router) {
$this->container = $container;
public function __construct(Router $router) {
$this->router = $router;
}

/**
* @param sfServiceContainer $container
*/
public function setContainer($container) {
$this->verifyRequiredServices($container);
$this->container = $container;
}

/**
* @return sfServiceContainer
*/
public function getContainer() {
return $this->container;
}

/**
* The default is an empty array
* @return array
*/
public function getRequiredServices() {
return array();
}

public function verifyRequiredServices($container) {
foreach ($this->getRequiredServices() as $service) {
if (!$container->hasService($service)) {
throw new ControllerException(sprintf('The service definition "%s" was not present.', $service));
}
}
}

/**
* Overwrite this function to return a title (html meta tag <title></title>)
*
Expand Down
4 changes: 3 additions & 1 deletion library/Controller/ControllerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ public function get($controllerName, $skipInitialization = false) {
* @return Controller
*/
protected function getController($className) {
return new $className($this->container, $this->container->router);
$controller = new $className($this->container->router);
if ($controller instanceof ContainerAware) $controller->setContainer($this->container);
return $controller;
}

}
8 changes: 6 additions & 2 deletions library/Dispatcher/DefaultDispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public function __construct(ControllerFactory $controllerFactory, Renderers $ren
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
*
* @param string $acceptHeader
* @return array
*/
public function getAcceptContentTypes($acceptHeader) {
$groupedContentTypes = array();
Expand Down Expand Up @@ -135,6 +136,9 @@ public function getAcceptContentTypes($acceptHeader) {
*/
public function dispatch($skipControllerInitialization = false) {
Profile::start('Dispatcher', 'Dispatching');

$contentTypes = array();

try {

$controllerName = isset($_GET['controller']) ? trim($_GET['controller']) : $this->defaultControllerName;
Expand All @@ -155,9 +159,9 @@ public function dispatch($skipControllerInitialization = false) {
$controller->setModel($model);
$controller->initModel();

try {
$contentTypes = $this->getAcceptContentTypes($_SERVER['HTTP_ACCEPT']);

$contentTypes = $this->getAcceptContentTypes($_SERVER['HTTP_ACCEPT']);
try {

if ($invalidControllerName) {
ErrorCode::notFound();
Expand Down
4 changes: 2 additions & 2 deletions library/Router/SecurityRouter.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
/**
* Including the Router
*/
require_class('DefaultRouter');
require_class('DefaultRouter', 'Router');

/**
* @author Matthias Loitsch <matthias@loitsch.com>
* @copyright Copyright (c) 2009, Matthias Loitsch
* @package Utils
* @package Router
*/
class SecurityRouter implements DefaultRouter {
class SecurityRouter extends DefaultRouter {

/**
* @var Encryption
Expand Down

0 comments on commit b9c5b73

Please sign in to comment.