/** * Inspects a directory and selects files that match a needle. * * @param string $directory The absolute path to a directory * @param string $needle The string or Regular Expression to match with the content * @param string|array $filter Optional Defines a pattern(s) to exclude files * * @return Suspects The selected files */ public function inspect($directory, $needle, $filter = null) { // list files $finder = $this->getFinder()->files()->name('*')->contains($needle)->in($directory); if (null !== $filter) { if (is_array($filter)) { foreach ($filter as $f) { $finder->notName($f); } } else { $finder->notName($filter); } } $event = new Event\FileListEvent(); $event->setName(InspectorEvents::FIND); $event->setTarget($this); $event->setFinder($finder); $this->getDispatcher()->trigger($event); // mark files as suspect $suspects = new Suspects(); foreach ($finder as $file) { $suspects->append($file); } $event = new Event\SuspectEvent(); $event->setName(InspectorEvents::MARK); $event->setTarget($this); $event->setSuspects($suspects); $this->getDispatcher()->trigger($event); return $suspects; }
/** * Dispatching method. * * @param FileListEvent $event */ public function onFind(FileListEvent $event) { $filters = $this->getAvailableFilters(); $choosenFilters = $this->getFilters(); foreach ($filters as $name => $filter) { try { $this->registerFilter($event->getFinder(), $name, $filter); } catch (Exception\Filter\ContinueException $e) { if (in_array($name, $choosenFilters)) { throw $e; } continue; } } }