alexkomaralex/file-search-bundle
Installation:
composer require alexkomaralex/file-search-bundle dev-master
Add to AppKernel.php:
new Alexkomaralex\FileSearchBundle\AlexkomaralexFileSearchBundle(),
First Use Case:
Search for files containing "database" in app/config/:
php app/console fsearch:find --path=app/config database
Resources/config/services.yml (default adapter setup).Command/FileSearchCommand.php (core logic).Adapter/ (Symfony Finder and DirectoryIterator implementations).Define Search Scope:
Use --path to specify directories (e.g., --path=src,config for multiple paths).
Defaults to project root if omitted.
Query Execution: The bundle splits the search into two phases:
Finder or DirectoryIterator) to traverse directories.Adapter Integration:
services.yml:
alexkomaralex_file_search.finder:
class: Symfony\Component\Finder\Finder
calls:
- [exclude, ['vendor', 'node_modules']]
Extending Adapters:
Implement Alexkomaralex\FileSearchBundle\Adapter\AdapterInterface:
class CustomAdapter implements AdapterInterface {
public function findFiles($path) { ... }
}
Register as a service and tag it with alexkomaralex_file_search.adapter.
file_search.post_find.$this->get('logger')->info('Found files:', ['files' => $results]);
*.php) in the adapter.Case Sensitivity:
The bundle uses mb_strpos (UTF-8 safe) but defaults to case-insensitive search. Override in a custom adapter if needed:
if (stripos($line, $query) !== false) { ... }
Memory Limits:
Scanning large directories (e.g., vendor/) may hit PHP’s memory_limit. Use --path to restrict scope.
Binary Files:
The bundle reads files as text. Binary files (e.g., .zip) may cause warnings. Exclude them in the adapter:
if ($file->isDir() || $file->getExtension() === 'zip') continue;
Symfony 2.5 Dependency: The bundle is tied to Symfony 2.5. Upgrade paths may require adapter refactoring.
-v to the command to see file traversal logs.php app/console debug:container alexkomaralex_file_search
Pre/Post-Processing:
Override FileSearchCommand to modify results:
protected function processResults(array $files) {
return array_filter($files, function($file) {
return strpos($file, 'test') !== false;
});
}
Custom Matching Logic:
Extend the findInFile method in adapters to support regex or fuzzy matching.
Parallel Search:
Use ReactPHP or SplFileInfo + pcntl_fork to parallelize file scanning (not bundled by default).
Default Path: If --path is omitted, the bundle uses getcwd(). Override in services.yml:
alexkomaralex_file_search.command.class: AppBundle\Command\CustomFileSearchCommand
Then set a default path in the constructor.
Adapter Priority: Services tagged with alexkomaralex_file_search.adapter are loaded in alphabetical order. Use priority tags to control order.
How can I help you explore Laravel packages today?