symfony/symfony — pull request #5650 by simensen (2012-10-02 15:31:39+00:00)
Title: [Finder] [FrameworkBundle] Added Finder Factory

Bug fix: no
Feature addition: yes
Backwards compatibility break: no
Symfony2 tests pass: yes
Fixes the following tickets: -
Todo: -
License of the code: MIT
Documentation PR: -

Added `FinderFactoryInterface` and default `FinderFactory` implementation. The idea being able to inject a factory to create Finder instances into services rather than calling `new Finder` in each service.

Added `finder_factory` service to Framework Bundle. This will allow for services to easily consume the default FinderFactory implementation.

Mocking Finder has proven to be extremely difficult if `new Finder` is called inside the class. By injecting a mocked Finder Factory we can have the opportunity to get mocked Finder instances inside our classes for the purpose of testing.

I've created implementations of Finder Factory at least twice now for two different projects and realized it was generic enough that it probably just belonged in the Finder component so I don't have to keep rewriting it and so other people could use it.

Example class:

finderFactory = $finderFactory ?: new FinderFactoryInterface;
}
public function findTmpFilesNew()
{
// Potential for mocked injected Finder Factory to return
// a mocked Finder instance.
$finder = $this->finderFactory()->create();

return $finder->in(sys_get_temp_dir());
}
public function findTmpFilesOld()
{
// Difficult to Mock
$finder = new Finder;

return $finder->in(sys_get_temp_dir());
}
}

Example service:

This also opens the door for "prepared" Finder instances to anyone that wants to leverage the `FinderFactoryInterface` and massage the Finder before returning it.

I can split the PR into two commits or two PR for Framework and Finder if that would be better.