Following the OptimizedPathMapping repository (puli/repository#27), I implemented a development version of this repository, as proposed by @webmozart in puli/issues#80. This pull-request is a place to debate over the code.
I did three main things:
1. Create the repository
https://github.com/tgalopin/repository/blob/dev-path-mapping/src/PathMappingRepository.php
The repository follows these rules:
- When a resource is added, the association
repositoryPath => filesystemPath
is stored in the KeyValueStore, and the parents of the repositoryPath are created as virtual resources ;
- When a resource is fetched (using
get()
or other methods), its repositoryPath is resolved to a filesystemPath: the repository find the list of virtual resources matching the searched resource repositoryPath and try to find a real directory / file corresponding to the resource in each of these virtual resources ;
This technique is much slower than the optimized version (obviously :) ) but it's a really useful tool for development as the structure inside virtual resources can change without having to "recreate" the puli repository.
2. Add a build structure system in tests
https://github.com/tgalopin/repository/blob/dev-path-mapping/tests/AbstractRepositoryTest.php#L62
This repository was not possible to test using the exact same tests as before as when we are requesting a resources, it can be either a virtual or a real one, and tests have to check for both at the same time.
I had to create a real valid structure on filesystem according to the tests: I couldn't without altering a bit the existing tests.
I added a buildStructure
method, called on each built resources (for instance https://github.com/tgalopin/repository/blob/dev-path-mapping/tests/AbstractRepositoryTest.php#L102) that by default return the root resource to be compatible with other repositories tests.
But in the PathMappingRepository tests, this method create the structure (https://github.com/tgalopin/repository/blob/dev-path-mapping/tests/PathMappingRepositoryTest.php#L113) for tests to work properly.
3. Create the specific tests for the PathMappingRepository
Once the built structure ready, I fixed the repository to pass the tests (https://github.com/tgalopin/repository/blob/dev-path-mapping/tests/PathMappingRepositoryTest.php).
I'm very open to modifications, expecially on how find()
and contains()
work as I'm not sure it's the best algorithm possible.