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');
$m = new sfClassManipulator($sourceCRLF); $f->lines = array(); $m->filterMethod('foo', array($f, 'filter1')); $t->is($m->getCode(), $sourceCRLF, '->filterMethod() does not change the code if the filter does nothing'); $t->is_deeply($f->lines, array(" function foo()\r\n", " {\r\n", " if (true)\r\n", " {\r\n", " return;\r\n", " }\r\n", " }"), '->filterMethod() filters each line of the method'); $m->filterMethod('foo', array($f, 'filter2')); $t->is($m->getCode(), $sourceFilteredCRLF, '->filterMethod() modifies the method'); // LF $t->diag('LF'); $m = new sfClassManipulator($sourceLF); $f->lines = array(); $m->filterMethod('foo', array($f, 'filter1')); $t->is($m->getCode(), $sourceLF, '->filterMethod() does not change the code if the filter does nothing'); $t->is_deeply($f->lines, array(" function foo()\n", " {\n", " if (true)\n", " {\n", " return;\n", " }\n", " }"), '->filterMethod() filters each line of the method'); $m->filterMethod('foo', array($f, 'filter2')); $t->is($m->getCode(), $sourceFilteredLF, '->filterMethod() modifies the method'); // no EOL $t->diag('no EOL'); $sourceFlat = '<?php class Foo { function foo() { if (true) { return; } } function baz() { if (true) { return; } } }'; $m = new sfClassManipulator($sourceFlat); $f->lines = array(); $m->filterMethod('foo', array($f, 'filter1')); $t->is_deeply($f->lines, array('function foo() { if (true) { return; } }'), '->filterMethod() works when there are no line breaks'); $t->is($m->getCode(), $sourceFlat, '->filterMethod() works when there are no line breaks'); // mixed EOL $t->diag('mixed EOL'); $sourceMixed = "<?php\r\n\nclass Foo\r\n{\n function foo()\r\n {\n if (true)\r\n {\n return;\r\n }\n }\r\n\n function baz()\r\n {\n if (true)\r\n {\n return;\r\n }\n }\r\n}"; $m = new sfClassManipulator($sourceMixed); $f->lines = array(); $m->filterMethod('foo', array($f, 'filter1')); $t->is_deeply($f->lines, array(" function foo()\r\n", " {\n", " if (true)\r\n", " {\n", " return;\r\n", " }\n", " }"), '->filterMethod() filters each line of a mixed EOL-style method');