Skip to content

Commit 56f8620

Browse files
author
Hugo Chinchilla Carbonell
committed
add support for component argument resolving
1 parent 573d579 commit 56f8620

6 files changed

+88
-6
lines changed

lib/action/sfActions.class.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* This file is part of the symfony package.
55
* (c) 2004-2006 Fabien Potencier <[email protected]>
66
* (c) 2004-2006 Sean Kerr <[email protected]>
7-
*
7+
*
88
* For the full copyright and license information, please view the LICENSE
99
* file that was distributed with this source code.
1010
*/
@@ -57,6 +57,9 @@ public function execute($request)
5757
}
5858

5959
// run action
60-
return $this->$actionToRun($request);
60+
return $this->getService('sf_parameter_resolver')
61+
->setRequest($request)
62+
->setComponent($this)
63+
->execute($actionToRun);
6164
}
6265
}
+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
class sfParameterResolver
4+
{
5+
protected $container;
6+
protected $request;
7+
protected $component;
8+
9+
public function __construct(sfServiceContainer $container = null)
10+
{
11+
$this->container = $container;
12+
}
13+
14+
public function setRequest(sfWebRequest $request)
15+
{
16+
$this->request = $request;
17+
18+
return $this;
19+
}
20+
21+
public function setComponent(sfComponent $component)
22+
{
23+
$this->component = $component;
24+
25+
return $this;
26+
}
27+
28+
public function execute($actionToRun = 'execute')
29+
{
30+
return call_user_func_array(array($this->component, $actionToRun), $this->resolveParams($actionToRun));
31+
}
32+
33+
protected function resolveParams($actionToRun)
34+
{
35+
$reflection = new ReflectionObject($this->component);
36+
$method = $reflection->getMethod($actionToRun);
37+
38+
$parameters = array();
39+
foreach ($method->getParameters() as $i => $param) {
40+
$type = $param->getClass();
41+
42+
// handle case where request parameter was not type hinted
43+
if (null === $type && $i === 0) {
44+
$parameters[] = $this->request;
45+
continue;
46+
}
47+
48+
if (null === $type) {
49+
throw new \Exception("Additional parameters must be type hinted");
50+
}
51+
52+
if ($type->getName() == "sfWebRequest") {
53+
$parameters[] = $this->request;
54+
} else {
55+
$parameters[] = $this->getParamFromContainer($type->getName());
56+
}
57+
}
58+
59+
return $parameters;
60+
}
61+
62+
protected function getParamFromContainer($param)
63+
{
64+
return $this->container->getService($param);
65+
}
66+
}

lib/autoload/sfCoreAutoload.class.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ static public function make()
187187
'sfactions' => 'action/sfActions.class.php',
188188
'sfcomponent' => 'action/sfComponent.class.php',
189189
'sfcomponents' => 'action/sfComponents.class.php',
190+
'sfparameterresolver' => 'action/sfParameterResolver.class.php',
190191
'sfdata' => 'addon/sfData.class.php',
191192
'sfpager' => 'addon/sfPager.class.php',
192193
'sfautoload' => 'autoload/sfAutoload.class.php',
@@ -522,8 +523,8 @@ static public function make()
522523
'sfwidgetforminput' => 'widget/sfWidgetFormInput.class.php',
523524
'sfwidgetforminputcheckbox' => 'widget/sfWidgetFormInputCheckbox.class.php',
524525
'sfwidgetforminputfile' => 'widget/sfWidgetFormInputFile.class.php',
525-
'sfwidgetforminputfilemulti' => 'widget/sfWidgetFormInputFileMulti.class.php',
526526
'sfwidgetforminputfileeditable' => 'widget/sfWidgetFormInputFileEditable.class.php',
527+
'sfwidgetforminputfilemulti' => 'widget/sfWidgetFormInputFileMulti.class.php',
527528
'sfwidgetforminputhidden' => 'widget/sfWidgetFormInputHidden.class.php',
528529
'sfwidgetforminputpassword' => 'widget/sfWidgetFormInputPassword.class.php',
529530
'sfwidgetforminputread' => 'widget/sfWidgetFormInputRead.class.php',

lib/config/config/services.yml

+5
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,8 @@ all:
1717
arguments:
1818
- '@sf_event_dispatcher'
1919
- '@sf_formatter'
20+
21+
sf_parameter_resolver:
22+
class: sfParameterResolver
23+
arguments:
24+
- '@service_container'

lib/filter/sfExecutionFilter.class.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ protected function executeAction($actionInstance)
9090
{
9191
// execute the action
9292
$actionInstance->preExecute();
93-
$viewName = $actionInstance->execute($this->context->getRequest());
93+
$viewName = $this->context->getService('sf_parameter_resolver')
94+
->setRequest($this->context->getRequest())
95+
->setComponent($actionInstance)
96+
->execute();
97+
9498
$actionInstance->postExecute();
9599

96100
return null === $viewName ? sfView::SUCCESS : $viewName;

lib/helper/PartialHelper.php

+5-2
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ function has_component_slot($name)
8484
{
8585
return false;
8686
}
87-
87+
8888
// check to see if component slot is empty (null)
8989
if ($viewInstance->getComponentSlot($name))
9090
{
@@ -385,7 +385,10 @@ function _call_component($moduleName, $componentName, $vars)
385385
$timer = sfTimerManager::getTimer(sprintf('Component "%s/%s"', $moduleName, $componentName));
386386
}
387387

388-
$retval = $componentInstance->$componentToRun($context->getRequest());
388+
$retval = $context->getService('sf_parameter_resolver')
389+
->setRequest($context->getRequest())
390+
->setComponent($componentInstance)
391+
->execute($componentToRun);
389392

390393
if (sfConfig::get('sf_debug') && sfConfig::get('sf_logging_enabled'))
391394
{

0 commit comments

Comments
 (0)