Chris@0: Chris@0: * Marcello Duarte Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Prophecy\Doubler\ClassPatch; Chris@0: Chris@0: use Prophecy\Doubler\Generator\Node\ClassNode; Chris@0: use Prophecy\Doubler\Generator\Node\MethodNode; Chris@0: Chris@0: /** Chris@0: * SplFileInfo patch. Chris@0: * Makes SplFileInfo and derivative classes usable with Prophecy. Chris@0: * Chris@0: * @author Konstantin Kudryashov Chris@0: */ Chris@0: class SplFileInfoPatch implements ClassPatchInterface Chris@0: { Chris@0: /** Chris@0: * Supports everything that extends SplFileInfo. Chris@0: * Chris@0: * @param ClassNode $node Chris@0: * Chris@0: * @return bool Chris@0: */ Chris@0: public function supports(ClassNode $node) Chris@0: { Chris@0: if (null === $node->getParentClass()) { Chris@0: return false; Chris@0: } Chris@0: return 'SplFileInfo' === $node->getParentClass() Chris@0: || is_subclass_of($node->getParentClass(), 'SplFileInfo') Chris@0: ; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Updated constructor code to call parent one with dummy file argument. Chris@0: * Chris@0: * @param ClassNode $node Chris@0: */ Chris@0: public function apply(ClassNode $node) Chris@0: { Chris@0: if ($node->hasMethod('__construct')) { Chris@0: $constructor = $node->getMethod('__construct'); Chris@0: } else { Chris@0: $constructor = new MethodNode('__construct'); Chris@0: $node->addMethod($constructor); Chris@0: } Chris@0: Chris@0: if ($this->nodeIsDirectoryIterator($node)) { Chris@0: $constructor->setCode('return parent::__construct("' . __DIR__ . '");'); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: if ($this->nodeIsSplFileObject($node)) { Chris@12: $filePath = str_replace('\\','\\\\',__FILE__); Chris@12: $constructor->setCode('return parent::__construct("' . $filePath .'");'); Chris@12: Chris@12: return; Chris@12: } Chris@12: Chris@12: if ($this->nodeIsSymfonySplFileInfo($node)) { Chris@12: $filePath = str_replace('\\','\\\\',__FILE__); Chris@12: $constructor->setCode('return parent::__construct("' . $filePath .'", "", "");'); Chris@0: Chris@0: return; Chris@0: } Chris@0: Chris@0: $constructor->useParentCode(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns patch priority, which determines when patch will be applied. Chris@0: * Chris@0: * @return int Priority number (higher - earlier) Chris@0: */ Chris@0: public function getPriority() Chris@0: { Chris@0: return 50; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param ClassNode $node Chris@0: * @return boolean Chris@0: */ Chris@0: private function nodeIsDirectoryIterator(ClassNode $node) Chris@0: { Chris@0: $parent = $node->getParentClass(); Chris@0: Chris@0: return 'DirectoryIterator' === $parent Chris@0: || is_subclass_of($parent, 'DirectoryIterator'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @param ClassNode $node Chris@0: * @return boolean Chris@0: */ Chris@0: private function nodeIsSplFileObject(ClassNode $node) Chris@0: { Chris@0: $parent = $node->getParentClass(); Chris@0: Chris@0: return 'SplFileObject' === $parent Chris@0: || is_subclass_of($parent, 'SplFileObject'); Chris@0: } Chris@12: Chris@12: /** Chris@12: * @param ClassNode $node Chris@12: * @return boolean Chris@12: */ Chris@12: private function nodeIsSymfonySplFileInfo(ClassNode $node) Chris@12: { Chris@12: $parent = $node->getParentClass(); Chris@12: Chris@12: return 'Symfony\\Component\\Finder\\SplFileInfo' === $parent; Chris@12: } Chris@0: }