Browse Source

Removed Object::evaluateExpression().

Finished ExpressionDependency.
tags/2.0.0-beta
Qiang Xue 12 years ago
parent
commit
1599a2cf25
  1. 3
      docs/api/framework/base/Object.md
  2. 2
      framework/base/Component.php
  3. 35
      framework/base/Object.php
  4. 11
      framework/caching/ExpressionDependency.php
  5. 7
      tests/unit/framework/base/ObjectTest.php

3
docs/api/framework/base/Object.md

@ -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 Besides the property feature, the Object class defines a static method [[create]] which provides a convenient
alternative way of creating a new object instance. 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.

2
framework/base/Component.php

@ -92,7 +92,7 @@ class Component extends \yii\base\Object
} elseif (strncmp($name, 'as ', 3) === 0) { } elseif (strncmp($name, 'as ', 3) === 0) {
// as behavior: attach behavior // as behavior: attach behavior
$name = trim(substr($name, 3)); $name = trim(substr($name, 3));
$this->attachBehavior($name, \Yii::createObject($value)); $this->attachBehavior($name, $value instanceof Behavior ? $value : \Yii::createObject($value));
} else { } else {
// behavior property // behavior property
$this->ensureBehaviors(); $this->ensureBehaviors();

35
framework/base/Object.php

@ -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. * Creates a new instance of the calling class.
* *
* The newly created object will be initialized with the specified configuration. * The newly created object will be initialized with the specified configuration.

11
framework/caching/ExpressionDependency.php

@ -12,9 +12,8 @@ namespace yii\caching;
/** /**
* ExpressionDependency represents a dependency based on the result of a PHP expression. * ExpressionDependency represents a dependency based on the result of a PHP expression.
* *
* ExpressionDependency performs dependency checking based on the * ExpressionDependency will use `eval()` to evaluate the PHP expression.
* result of a PHP {@link expression}. * The dependency is reported as unchanged if and only if the result of the expression is
* The dependency is reported as unchanged if and only if the result is
* the same as the one evaluated when storing the data to cache. * the same as the one evaluated when storing the data to cache.
* *
* @author Qiang Xue <qiang.xue@gmail.com> * @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. * @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; public $expression;
@ -47,6 +42,6 @@ class ExpressionDependency extends Dependency
*/ */
protected function generateDependencyData() protected function generateDependencyData()
{ {
return $this->evaluateExpression($this->expression); return eval("return {$this->expression};");
} }
} }

7
tests/unit/framework/base/ObjectTest.php

@ -116,13 +116,6 @@ class ObjectTest extends \yiiunit\TestCase
$this->assertTrue(isset($this->object->Text)); $this->assertTrue(isset($this->object->Text));
$this->assertTrue(empty($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')));
}
} }

Loading…
Cancel
Save