Last active
December 17, 2015 00:49
-
-
Save iwyg/5524103 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Thapp\ControllerFactory; | |
use Illuminate\Container\Container; | |
/** | |
* Class: AbstractControllerFactory | |
* | |
* @package | |
* @version | |
* | |
* @abstract | |
* @author Thomas Appel <[email protected]> | |
* @license MIT | |
*/ | |
abstract class AbstractControllerFactory | |
{ | |
/** | |
* controllers | |
* | |
* @var mixed | |
*/ | |
protected $controllers = array(); | |
/** | |
* create | |
* | |
* @param string $controller | |
* @access public | |
* @return AdminController | |
*/ | |
protected function create($controller, Container $container) | |
{ | |
if (!isset($this->controllers[$controller])) { | |
$resolved = $container->make($controller); | |
$this->setDependecies($container, $resolved); | |
$this->controllers[$controller] = $resolved; | |
} | |
return $this->controllers[$controller]; | |
} | |
/** | |
* getDependecies | |
* | |
* @param \Illuminate\Container\Container $container | |
* @param \BaseController $controller | |
* @access protected | |
* @return void | |
*/ | |
abstract protected function setDependecies(Container $container, \BaseController $controller); | |
/** | |
* callAction | |
* | |
* @param mixed $method | |
* @param mixed $arguments | |
* @access public | |
* @return void | |
*/ | |
final public function callAction($app, $router, $controller, $args) | |
{ | |
$route = $router->getCurrentRoute(); | |
$actions = explode('@', $route->getAction()); | |
$action = array_pop($actions); | |
$controller = $this->create($controller, $app); | |
return call_user_func_array(array($controller, 'callAction'), array($app, $router, $action, $args)); | |
} | |
/** | |
* __call | |
* | |
* @param mixed $method | |
* @param mixed $arguments | |
* @access public | |
* @return mixed | |
*/ | |
public function __call($method, $arguments) | |
{ | |
throw new \InvalidArgumentException("call to undefined method $method"); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Acme\Admin; | |
use Illuminate\Container\Container; | |
use Thapp\ControllerFactory\AbstractControllerFactory; | |
/** | |
* @class ControllerFactory | |
*/ | |
class ControllerFactory extends AbstractControllerFactory | |
{ | |
/** | |
* {@inheritDoc} | |
*/ | |
protected function setDependecies(Container $container, \BaseController $controller) | |
{ | |
$controller->setView($container['view']); | |
$controller->setInput($container['request']); | |
$controller->setRedirect($container['redirect']); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// usage | |
Route::resource('admin/', 'Acme\Admin\ControllerFactory@AdminController'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment