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(); } }
} EOF; // ->wrapMethod() $t->diag('->wrapMethod()'); $m = new sfClassManipulator($source); $t->is($m->wrapMethod('bar', '// code before', '// code after'), $source, '->wrapMethod() does nothing if the method does not exist.'); $m = new sfClassManipulator($source); $t->is($m->wrapMethod('foo', '// code before'), $sourceWithCodeBefore, '->wrapMethod() adds code before the beginning of a method.'); $m = new sfClassManipulator($source); $t->is($m->wrapMethod('foo', '', '// code after'), $sourceWithCodeAfter, '->wrapMethod() adds code after the end of a method.'); $t->is($m->wrapMethod('foo', '// code before'), $sourceWithCodeBeforeAndAfter, '->wrapMethod() adds code to the previously manipulated code.'); // ->getCode() $t->diag('->getCode()'); $m = new sfClassManipulator($source); $t->is($m->getCode(), $source, '->getCode() returns the source code when no manipulations has been done'); $m->wrapMethod('foo', '', '// code after'); $t->is($m->getCode(), $sourceWithCodeAfter, '->getCode() returns the modified code'); // ->setFile() ->getFile() $t->diag('->setFile() ->getFile()'); $m = new sfClassManipulator($source); $m->setFile('foo'); $t->is($m->getFile(), 'foo', '->setFile() sets the name of the file associated with the source code'); // ::fromFile() $t->diag('::fromFile()'); $file = sys_get_temp_dir() . '/sf_tmp.php'; file_put_contents($file, $source); $m = sfClassManipulator::fromFile($file); $t->is($m->getFile(), $file, '::fromFile() sets the file internally'); // ->save() $t->diag('->save()'); $m = sfClassManipulator::fromFile($file);