Commit 1599a2cf by Qiang Xue

Removed Object::evaluateExpression().

Finished ExpressionDependency.
parent 37c874f1
......@@ -34,6 +34,3 @@ One can call [[hasProperty]], [[canGetProperty]] and/or [[canSetProperty]] to ch
Besides the property feature, the Object class defines a static method [[create]] which provides a convenient
alternative way of creating a new object instance.
The Object class also defines the [[evaluateExpression]] method so that a PHP expression or callback can be dynamically
evaluated within the context of an object.
\ No newline at end of file
......@@ -92,7 +92,7 @@ class Component extends \yii\base\Object
} elseif (strncmp($name, 'as ', 3) === 0) {
// as behavior: attach behavior
$name = trim(substr($name, 3));
$this->attachBehavior($name, \Yii::createObject($value));
$this->attachBehavior($name, $value instanceof Behavior ? $value : \Yii::createObject($value));
} else {
// behavior property
$this->ensureBehaviors();
......
......@@ -180,41 +180,6 @@ class Object
}
/**
* Evaluates a PHP expression or callback under the context of this object.
*
* Valid PHP callback can be class method name in the form of
* array(ClassName/Object, MethodName), or anonymous function.
*
* If a PHP callback is used, the corresponding function/method signature should be
*
* ~~~
* function foo($param1, $param2, ..., $object) { ... }
* ~~~
*
* where the array elements in the second parameter to this method will be passed
* to the callback as `$param1`, `$param2`, ...; and the last parameter will be the object itself.
*
* If a PHP expression is used, the second parameter will be "extracted" into PHP variables
* that can be directly accessed in the expression.
* See [PHP extract](http://us.php.net/manual/en/function.extract.php)
* for more details. In the expression, the object can be accessed using `$this`.
*
* @param mixed $_expression_ a PHP expression or PHP callback to be evaluated.
* @param array $_data_ additional parameters to be passed to the above expression/callback.
* @return mixed the expression result
*/
public function evaluateExpression($_expression_, $_data_ = array())
{
if (is_string($_expression_)) {
extract($_data_);
return eval('return ' . $_expression_ . ';');
} else {
$_data_[] = $this;
return call_user_func_array($_expression_, $_data_);
}
}
/**
* Creates a new instance of the calling class.
*
* The newly created object will be initialized with the specified configuration.
......
......@@ -12,9 +12,8 @@ namespace yii\caching;
/**
* ExpressionDependency represents a dependency based on the result of a PHP expression.
*
* ExpressionDependency performs dependency checking based on the
* result of a PHP {@link expression}.
* The dependency is reported as unchanged if and only if the result is
* ExpressionDependency will use `eval()` to evaluate the PHP expression.
* The dependency is reported as unchanged if and only if the result of the expression is
* the same as the one evaluated when storing the data to cache.
*
* @author Qiang Xue <qiang.xue@gmail.com>
......@@ -24,10 +23,6 @@ class ExpressionDependency extends Dependency
{
/**
* @var string the PHP expression whose result is used to determine the dependency.
* The expression can also be a valid PHP callback,
* including class method name (array(ClassName/Object, MethodName)),
* or anonymous function (PHP 5.3.0+). The function/method will be passed with a
* parameter which is the dependency object itself.
*/
public $expression;
......@@ -47,6 +42,6 @@ class ExpressionDependency extends Dependency
*/
protected function generateDependencyData()
{
return $this->evaluateExpression($this->expression);
return eval("return {$this->expression};");
}
}
......@@ -116,13 +116,6 @@ class ObjectTest extends \yiiunit\TestCase
$this->assertTrue(isset($this->object->Text));
$this->assertTrue(empty($this->object->Text));
}
public function testEvaluateExpression()
{
$object = new NewObject;
$this->assertEquals('Hello world',$object->evaluateExpression('"Hello $who"',array('who' => 'world')));
$this->assertEquals('Hello world',$object->evaluateExpression(array($object,'exprEvaluator'),array('who' => 'world')));
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment