Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for component argument resolution #214

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions lib/action/sfActions.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
* (c) 2004-2006 Sean Kerr <sean@code-box.org>
*
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand Down Expand Up @@ -57,6 +57,9 @@ public function execute($request)
}

// run action
return $this->$actionToRun($request);
return $this->getService('sf_parameter_resolver')
->setRequest($request)
->setComponent($this)
->execute($actionToRun);
}
}
70 changes: 70 additions & 0 deletions lib/action/sfParameterResolver.class.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

class sfParameterResolver
{
protected $container;
protected $request;
protected $component;

public function __construct(sfServiceContainer $container = null)
{
$this->container = $container;
}

public function setRequest(sfWebRequest $request)
{
$this->request = $request;

return $this;
}

public function setComponent(sfComponent $component)
{
$this->component = $component;

return $this;
}

public function execute($actionToRun = 'execute')
{
return call_user_func_array(array($this->component, $actionToRun), $this->resolveParams($actionToRun));
}

protected function resolveParams($actionToRun)
{
$reflection = new ReflectionObject($this->component);
$method = $reflection->getMethod($actionToRun);

$parameters = array();
foreach ($method->getParameters() as $i => $param) {
$type = $param->getClass();

if (null === $type) {
if ($i === 0) {
// first parameter is always the request
$parameters[] = $this->request;
continue;
} elseif ($param->getDefaultValue()) {
// additional params with default values may have been added
$params[] = $param->getDefaultValue();
continue;
} else {
throw new \Exception("Additional parameters must be type hinted or provide a default value");
}
}

if ($type->getName() == "sfWebRequest") {
$parameters[] = $this->request;
} else {
$parameters[] = $this->getParamFromContainer($type->getName());
}
}

return $parameters;
}

protected function getParamFromContainer($param)
{
return $this->container->getService($param);
}
}
3 changes: 2 additions & 1 deletion lib/autoload/sfCoreAutoload.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ static public function make()
'sfactions' => 'action/sfActions.class.php',
'sfcomponent' => 'action/sfComponent.class.php',
'sfcomponents' => 'action/sfComponents.class.php',
'sfparameterresolver' => 'action/sfParameterResolver.class.php',
'sfdata' => 'addon/sfData.class.php',
'sfpager' => 'addon/sfPager.class.php',
'sfautoload' => 'autoload/sfAutoload.class.php',
Expand Down Expand Up @@ -522,8 +523,8 @@ static public function make()
'sfwidgetforminput' => 'widget/sfWidgetFormInput.class.php',
'sfwidgetforminputcheckbox' => 'widget/sfWidgetFormInputCheckbox.class.php',
'sfwidgetforminputfile' => 'widget/sfWidgetFormInputFile.class.php',
'sfwidgetforminputfilemulti' => 'widget/sfWidgetFormInputFileMulti.class.php',
'sfwidgetforminputfileeditable' => 'widget/sfWidgetFormInputFileEditable.class.php',
'sfwidgetforminputfilemulti' => 'widget/sfWidgetFormInputFileMulti.class.php',
'sfwidgetforminputhidden' => 'widget/sfWidgetFormInputHidden.class.php',
'sfwidgetforminputpassword' => 'widget/sfWidgetFormInputPassword.class.php',
'sfwidgetforminputread' => 'widget/sfWidgetFormInputRead.class.php',
Expand Down
5 changes: 5 additions & 0 deletions lib/config/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ all:
arguments:
- '@sf_event_dispatcher'
- '@sf_formatter'

sf_parameter_resolver:
class: sfParameterResolver
arguments:
- '@service_container'
6 changes: 5 additions & 1 deletion lib/filter/sfExecutionFilter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,11 @@ protected function executeAction($actionInstance)
{
// execute the action
$actionInstance->preExecute();
$viewName = $actionInstance->execute($this->context->getRequest());
$viewName = $this->context->getService('sf_parameter_resolver')
->setRequest($this->context->getRequest())
->setComponent($actionInstance)
->execute();

$actionInstance->postExecute();

return null === $viewName ? sfView::SUCCESS : $viewName;
Expand Down
7 changes: 5 additions & 2 deletions lib/helper/PartialHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function has_component_slot($name)
{
return false;
}

// check to see if component slot is empty (null)
if ($viewInstance->getComponentSlot($name))
{
Expand Down Expand Up @@ -385,7 +385,10 @@ function _call_component($moduleName, $componentName, $vars)
$timer = sfTimerManager::getTimer(sprintf('Component "%s/%s"', $moduleName, $componentName));
}

$retval = $componentInstance->$componentToRun($context->getRequest());
$retval = $context->getService('sf_parameter_resolver')
->setRequest($context->getRequest())
->setComponent($componentInstance)
->execute($componentToRun);

if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
{
Expand Down
34 changes: 34 additions & 0 deletions test/functional/autowiringTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

$app = 'frontend';
if (!include(__DIR__ . '/../bootstrap/functional.php')) {
return;
}

$b = new sfTestBrowser();

// test action execution
$b->
get('/autowiring/index')->
with('request')->begin()->
isParameter('module', 'autowiring')->
isParameter('action', 'index')->
end()->
with('response')->begin()->
isStatusCode(200)->
checkElement('div[id=value]', 'success')->
end()
;

// test component execution
$b->
get('/autowiring/withComponents')->
with('request')->begin()->
isParameter('module', 'autowiring')->
isParameter('action', 'withComponents')->
end()->
with('response')->begin()->
isStatusCode(200)->
checkElement('div[id=component_value]', 'success')->
end()
;
4 changes: 4 additions & 0 deletions test/functional/fixtures/apps/frontend/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ all:
class: sfOutputEscaperSafe
arguments: [%my_project_param%]

MyService:
class: MyService


test:
parameters:
my_app_test_param: foo
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php


class autowiringActions extends sfActions
{
public function executeIndex($request, MyService $service)
{
$this->value = $service->execute();
return sfView::SUCCESS;
}

public function executeWithComponents($request)
{
return sfView::SUCCESS;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


class autowiringComponents extends sfComponents
{
public function executeComponent(MyService $service)
{
$this->value = $service->execute();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php


class MyService
{
public function execute()
{
return "success";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="component_value"><?php echo $value ?></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<div id="value"><?php echo $value ?></div>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php include_component('autowiring', 'component') ?>
58 changes: 58 additions & 0 deletions test/unit/action/sfParameterResolverTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
*
* For the full copyright and license information, please component the LICENSE
* file that was distributed with this source code.
*/

require_once(__DIR__.'/../../bootstrap/unit.php');
require_once($_test_dir.'/unit/sfContextMock.class.php');
require_once($_test_dir.'/unit/sfNoRouting.class.php');

$t = new lime_test(3);

class MyService {
public function execute()
{
return "success";
}
}

class myComponent extends sfComponent
{
function execute($request, MyService $service = null) {
return $service->execute();
}

function executeNamed($request, MyService $service) {
return $service->execute();
}

function executeFoo($request, $options = "foo") {
return $options;
}
}

$context = sfContext::getInstance(array(
'routing' => 'sfNoRouting',
'request' => 'sfWebRequest',
));

$context->getServiceContainer()->setService("MyService", new MyService());

$component = new myComponent($context, 'module', 'action');


$resolver = new sfParameterResolver($context->getServiceContainer());
$resolver
->setRequest($context->getRequest())
->setComponent($component);


$t->diag('execute()');
$t->is($resolver->execute(), "success", 'without arguments executes default ->execute() method and resolves required dependencies');
$t->is($resolver->execute('executeNamed'), "success", 'can execute an arbitrary method and resolves required dependencies');
$t->is($resolver->execute('executeFoo'), "foo", 'uses default value if no type hint is present');
13 changes: 12 additions & 1 deletion test/unit/sfContextMock.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/*
* This file is part of the symfony package.
* (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
*
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
Expand Down Expand Up @@ -35,6 +35,7 @@ static public function getInstance($factories = array(), $force = false)
self::$instance->storage = new sfSessionTestStorage(array('session_path' => self::$instance->sessionPath));

self::$instance->dispatcher = new sfEventDispatcher();
self::$instance->serviceContainer = new sfServiceContainer();

foreach ($factories as $type => $class)
{
Expand All @@ -60,6 +61,16 @@ public function getEventDispatcher()
return self::$instance->dispatcher;
}

public function getServiceContainer()
{
return self::$instance->serviceContainer;
}

public function getService($service_id)
{
return $this->getServiceContainer()->getService($service_id);
}

public function getModuleName()
{
return 'module';
Expand Down