public function testThatCustomFinderWorks() { $finder = new Finder(); $finder->in(__DIR__ . '/Fixtures/FinderDirectory'); $config = Config::create()->finder($finder); $iterator = $config->getFinder()->getIterator(); $this->assertSame(1, count($iterator)); $iterator->rewind(); $this->assertSame('somefile.php', $iterator->current()->getFilename()); }
public function provideResolveIntersectionOfPathsCases() { $dir = __DIR__ . '/../Fixtures/ConfigurationResolverPathsIntersection'; $cb = function (array $items) use($dir) { return array_map(function ($item) use($dir) { return realpath($dir . '/' . $item); }, $items); }; return array('no path at all' => array(new \LogicException(), Finder::create(), array(), 'override'), 'configured only by finder' => array($cb(array('a1.php', 'a2.php', 'b/b1.php', 'b/b2.php', 'b_b/b_b1.php', 'c/c1.php', 'c/d/cd1.php', 'd/d1.php', 'd/d2.php', 'd/e/de1.php', 'd/f/df1.php')), Finder::create()->in($dir), array(), 'override'), 'configured only by argument' => array($cb(array('a1.php', 'a2.php', 'b/b1.php', 'b/b2.php', 'b_b/b_b1.php', 'c/c1.php', 'c/d/cd1.php', 'd/d1.php', 'd/d2.php', 'd/e/de1.php', 'd/f/df1.php')), Finder::create(), array($dir), 'override'), 'configured by finder, intersected with empty argument' => array(array(), Finder::create()->in($dir), array(), 'intersection'), 'configured by finder, intersected with dir' => array($cb(array('c/c1.php', 'c/d/cd1.php')), Finder::create()->in($dir), array($dir . '/c'), 'intersection'), 'configured by finder, intersected with file' => array($cb(array('c/c1.php')), Finder::create()->in($dir), array($dir . '/c/c1.php'), 'intersection'), 'finder points to one dir while argument to another, not connected' => array(array(), Finder::create()->in($dir . '/b'), array($dir . '/c'), 'intersection'), 'finder with excluded dir, intersected with excluded file' => array(array(), Finder::create()->in($dir)->exclude('c'), array($dir . '/c/d/cd1.php'), 'intersection'), 'finder with excluded dir, intersected with dir containing excluded one' => array($cb(array('c/c1.php')), Finder::create()->in($dir)->exclude('c/d'), array($dir . '/c'), 'intersection'), 'finder with excluded file, intersected with dir containing excluded one' => array($cb(array('c/d/cd1.php')), Finder::create()->in($dir)->notPath('c/c1.php'), array($dir . '/c'), 'intersection'), 'configured by finder, intersected with non-existing path' => array(array(), Finder::create()->in($dir), array('non_existing_dir'), 'intersection'), 'configured by finder, overriden by multiple files' => array($cb(array('d/d1.php', 'd/d2.php')), null, array($dir . '/d/d1.php', $dir . '/d/d2.php'), 'override', $dir . '/d/.php_cs'), 'configured by finder, intersected with multiple files' => array($cb(array('d/d1.php', 'd/d2.php')), null, array($dir . '/d/d1.php', $dir . '/d/d2.php'), 'intersection', $dir . '/d/.php_cs'), 'configured by finder, overriden by multiple files and dirs' => array($cb(array('d/d1.php', 'd/e/de1.php', 'd/f/df1.php')), null, array($dir . '/d/d1.php', $dir . '/d/e', $dir . '/d/f/'), 'override', $dir . '/d/.php_cs'), 'configured by finder, intersected with multiple files and dirs' => array($cb(array('d/d1.php', 'd/e/de1.php', 'd/f/df1.php')), null, array($dir . '/d/d1.php', $dir . '/d/e', $dir . '/d/f/'), 'intersection', $dir . '/d/.php_cs')); }
/** * Use php cs fixer to have a nice formatting of generated files * * @param string $directory * * @return array|void */ protected function fix($directory) { if (!class_exists('PhpCsFixer\\Config')) { return; } /** @var Config $fixerConfig */ $fixerConfig = $this->fixerConfig; if (null === $fixerConfig) { $fixerConfig = Config::create()->setRiskyAllowed(true)->setRules(array('@Symfony' => true, 'simplified_null_return' => false, 'concat_without_spaces' => false, 'double_arrow_multiline_whitespaces' => false, 'unalign_equals' => false, 'unalign_double_arrow' => false, 'align_double_arrow' => true, 'align_equals' => true, 'concat_with_spaces' => true, 'ordered_imports' => true, 'phpdoc_order' => true, 'short_array_syntax' => true)); $resolver = new ConfigurationResolver(); $resolver->setDefaultConfig($fixerConfig); $resolver->resolve(); } $finder = new Finder(); $finder->in($directory); $fixerConfig->finder($finder); $fixer = new Fixer(); return $fixer->fix($fixerConfig); }
/** * Apply path on config instance. */ private function resolveConfigPath() { if ($this->isStdIn) { $this->config->finder(new \ArrayIterator(array(new StdinFileInfo()))); return; } $isIntersectionPathMode = self::PATH_MODE_INTERSECTION === $this->options['path-mode']; $paths = array_filter(array_map(function ($path) { return realpath($path); }, $this->path)); if (empty($paths)) { if ($isIntersectionPathMode) { $this->config->finder(new \ArrayIterator(array())); } return; } $pathsByType = array('file' => array(), 'dir' => array()); foreach ($paths as $path) { if (is_file($path)) { $pathsByType['file'][] = $path; } else { $pathsByType['dir'][] = $path . DIRECTORY_SEPARATOR; } } $currentFinder = $this->config->getFinder(); $nestedFinder = null; $iterator = null; try { $nestedFinder = $currentFinder instanceof \IteratorAggregate ? $currentFinder->getIterator() : $currentFinder; } catch (\Exception $e) { } if ($isIntersectionPathMode) { if (null === $nestedFinder) { throw new InvalidConfigurationException('Cannot create intersection with not-fully defined Finder in configuration file.'); } $iterator = new \CallbackFilterIterator($nestedFinder, function (\SplFileInfo $current) use($pathsByType) { $currentRealPath = $current->getRealPath(); if (in_array($currentRealPath, $pathsByType['file'], true)) { return true; } foreach ($pathsByType['dir'] as $path) { if (0 === strpos($currentRealPath, $path)) { return true; } } return false; }); } elseif ($currentFinder instanceof SymfonyFinder && null === $nestedFinder) { // finder from configuration Symfony finder and it is not fully defined, we may fulfill it $iterator = $currentFinder->in($pathsByType['dir'])->append($pathsByType['file']); } else { $iterator = Finder::create()->in($pathsByType['dir'])->append($pathsByType['file']); } $this->config->finder($iterator); }
/** * @expectedException \LogicException * @expectedExceptionMessageRegExp /^You must call (?:the in\(\) method)|(?:one of in\(\) or append\(\)) methods before iterating over a Finder\.$/ */ public function testThatDefaultFinderDoesNotSpecifyAnyDirectory() { $finder = Finder::create(); $finder->getIterator(); }
/** * Apply path on config instance. */ private function resolveFinder() { if ($this->isStdIn()) { return new \ArrayIterator(array(new StdinFileInfo())); } $modes = array(self::PATH_MODE_OVERRIDE, self::PATH_MODE_INTERSECTION); if (!in_array($this->options['path-mode'], $modes, true)) { throw new InvalidConfigurationException(sprintf('The path-mode "%s" is not defined, supported are %s.', $this->options['path-mode'], implode(', ', $modes))); } $isIntersectionPathMode = self::PATH_MODE_INTERSECTION === $this->options['path-mode']; $paths = array_filter(array_map(function ($path) { return realpath($path); }, $this->getPath())); if (!count($paths)) { if ($isIntersectionPathMode) { return new \ArrayIterator(array()); } return $this->iterableToTraversable($this->getConfig()->getFinder()); } $pathsByType = array('file' => array(), 'dir' => array()); foreach ($paths as $path) { if (is_file($path)) { $pathsByType['file'][] = $path; } else { $pathsByType['dir'][] = $path . DIRECTORY_SEPARATOR; } } $nestedFinder = null; $iterator = null; $currentFinder = $this->iterableToTraversable($this->getConfig()->getFinder()); try { $nestedFinder = $currentFinder instanceof \IteratorAggregate ? $currentFinder->getIterator() : $currentFinder; } catch (\Exception $e) { } if ($isIntersectionPathMode) { if (null === $nestedFinder) { throw new InvalidConfigurationException('Cannot create intersection with not-fully defined Finder in configuration file.'); } return new \CallbackFilterIterator($nestedFinder, function (\SplFileInfo $current) use($pathsByType) { $currentRealPath = $current->getRealPath(); if (in_array($currentRealPath, $pathsByType['file'], true)) { return true; } foreach ($pathsByType['dir'] as $path) { if (0 === strpos($currentRealPath, $path)) { return true; } } return false; }); } if ($currentFinder instanceof SymfonyFinder && null === $nestedFinder) { // finder from configuration Symfony finder and it is not fully defined, we may fulfill it return $currentFinder->in($pathsByType['dir'])->append($pathsByType['file']); } return Finder::create()->in($pathsByType['dir'])->append($pathsByType['file']); }