class RulesPHPEvaluator

A class implementing a rules input evaluator processing PHP.

Hierarchy

Expanded class hierarchy of RulesPHPEvaluator

Related topics

1 string reference to 'RulesPHPEvaluator'
rules_php_evaluator_info in modules/php.rules.inc
Implements hook_rules_evaluator_info() on behalf of the php module.

File

modules/php.eval.inc, line 15

View source
class RulesPHPEvaluator extends RulesDataInputEvaluator {
    
    /**
     * Overrides RulesDataProcessor::editAccess().
     */
    public function editAccess() {
        return parent::editAccess() && (user_access('use PHP for settings') || drupal_is_cli());
    }
    
    /**
     * Helper function to find variables in PHP code.
     *
     * @param string $text
     *   The PHP code.
     * @param array $var_info
     *   Array with variable names as keys.
     */
    public static function getUsedVars($text, $var_info) {
        if (strpos($text, '<?') !== FALSE) {
            $used_vars = array();
            foreach ($var_info as $name => $info) {
                if (strpos($text, '$' . $name) !== FALSE) {
                    $used_vars[] = $name;
                }
            }
            return $used_vars;
        }
    }
    
    /**
     * Overrides RulesDataInputEvaluator::prepare().
     */
    public function prepare($text, $var_info) {
        // A returned NULL skips the evaluator.
        $this->setting = self::getUsedVars($text, $var_info);
    }
    
    /**
     * Evaluates PHP code contained in $text.
     *
     * This method doesn't apply $options, thus the PHP code is responsible for
     * behaving appropriately.
     */
    public function evaluate($text, $options, RulesState $state) {
        $vars['eval_options'] = $options;
        foreach ($this->setting as $key => $var_name) {
            $vars[$var_name] = $state->get($var_name);
        }
        return rules_php_eval($text, rules_unwrap_data($vars));
    }
    
    /**
     * Overrides RulesDataInputEvaluator::help().
     */
    public static function help($var_info) {
        module_load_include('inc', 'rules', 'rules/modules/php.rules');
        $render = array(
            '#type' => 'fieldset',
            '#title' => t('PHP Evaluation'),
            '#collapsible' => TRUE,
            '#collapsed' => TRUE,
        ) + rules_php_evaluator_help($var_info);
        return $render;
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
RulesDataInputEvaluator::attachForm public static function Overrides RulesDataProcessor::attachForm(). Overrides RulesDataProcessor::attachForm
RulesDataInputEvaluator::evaluators public static function Returns all input evaluators that can be applied to the parameters type.
RulesDataInputEvaluator::getEvaluatorOptions protected function Generates the evaluator $options.
RulesDataInputEvaluator::getPreparedValue protected function Return $this or skip this processor by returning the next processor. Overrides RulesDataProcessor::getPreparedValue
RulesDataInputEvaluator::prepareSetting public static function Overridden to prepare input evaluator processors. Overrides RulesDataProcessor::prepareSetting
RulesDataInputEvaluator::process public function Overridden to generate evaluator $options and invoke evaluate(). Overrides RulesDataProcessor::process 1
RulesDataInputEvaluator::processors public static function Overrides RulesDataProcessor::processors(). Overrides RulesDataProcessor::processors
RulesDataInputEvaluator::__construct protected function Overridden to invoke prepare(). Overrides RulesDataProcessor::__construct
RulesDataProcessor::$processor protected property Allows chaining processors. If set, the next processor to invoke.
RulesDataProcessor::$setting protected property The processors&#039; setting value.
RulesDataProcessor::access public static function Return whether the current user has permission to use the processor. 1
RulesDataProcessor::dependencies public function Returns an array of modules which we depend on.
RulesDataProcessor::form protected static function Defines the processor form element. 3
RulesDataProcessor::getChainSettings public function Gets the settings array for this and all contained chained processors.
RulesDataProcessor::getSetting public function Gets the settings of this processor.
RulesDataProcessor::unchain protected function
RulesDataProcessor::_item_sort public static function
RulesPHPEvaluator::editAccess public function Overrides RulesDataProcessor::editAccess(). Overrides RulesDataProcessor::editAccess
RulesPHPEvaluator::evaluate public function Evaluates PHP code contained in $text. Overrides RulesDataInputEvaluator::evaluate
RulesPHPEvaluator::getUsedVars public static function Helper function to find variables in PHP code.
RulesPHPEvaluator::help public static function Overrides RulesDataInputEvaluator::help(). Overrides RulesDataInputEvaluator::help
RulesPHPEvaluator::prepare public function Overrides RulesDataInputEvaluator::prepare(). Overrides RulesDataInputEvaluator::prepare