public function objectFilter(&$script) { if ($this->isDisabled()) { return; } $class = new sfClassManipulator($script); $class->filterMethod('doSave', array($this, 'filterDoSave')); $script = $class->getCode(); }
public function peerFilter(&$script) { if ($this->isDisabled()) { return; } $doInsertPre = <<<EOF // symfony_behaviors behavior foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}Peer:doInsert:pre') as \$sf_hook) { if (false !== \$sf_hook_retval = call_user_func(\$sf_hook, 'Base{$this->getTable()->getPhpName()}Peer', \$values, \$con)) { return \$sf_hook_retval; } } EOF; $doUpdatePre = <<<EOF // symfony_behaviors behavior foreach (sfMixer::getCallables('Base{$this->getTable()->getPhpName()}Peer:doUpdate:pre') as \$sf_hook) { if (false !== \$sf_hook_retval = call_user_func(\$sf_hook, 'Base{$this->getTable()->getPhpName()}Peer', \$values, \$con)) { return \$sf_hook_retval; } } EOF; // add doInsert and doUpdate hooks $class = new sfClassManipulator($script); $class->filterMethod('doInsert', array($this, 'filterDoInsert')); $class->wrapMethod('doInsert', $doInsertPre); $class->filterMethod('doUpdate', array($this, 'filterDoUpdate')); $class->wrapMethod('doUpdate', $doUpdatePre); $script = $class->getCode(); // add symfony behavior configuration file if ($this->createBehaviorsFile()) { $script .= $this->getBehaviorsInclude(); } }
} } $f = new MethodFilterer(); $sourceFiltered = <<<EOF <?php class Foo { function foo(\$arg) { if (false) { return; } } function baz() { if (true) { return; } } } EOF; $m = new sfClassManipulator($source); $m->filterMethod('foo', array($f, 'filter1')); $t->is($m->getCode(), $source, '->filterMethod() does not change the code if the filter does nothing'); $t->is_deeply($f->lines, array(' function foo()' . PHP_EOL, ' {' . PHP_EOL, ' if (true)' . PHP_EOL, ' {' . PHP_EOL, ' return;' . PHP_EOL, ' }' . PHP_EOL, ' }'), '->filterMethod() filters each line of the method'); $m->filterMethod('foo', array($f, 'filter2')); $t->is($m->getCode(), $sourceFiltered, '->filterMethod() modifies the method');