FastDirectoriesResource.php 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. <?php
  2. /*
  3. * Copyright 2011 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\DiExtraBundle\Config;
  18. use JMS\DiExtraBundle\Finder\PatternFinder;
  19. use Symfony\Component\Config\Resource\ResourceInterface;
  20. class FastDirectoriesResource implements ResourceInterface
  21. {
  22. private $finder;
  23. private $directories;
  24. private $filePattern;
  25. private $files = array();
  26. public function __construct(array $directories, $filePattern = null)
  27. {
  28. $this->finder = new PatternFinder('.*', '*.php');
  29. $this->finder->setRegexPattern(true);
  30. $this->directories = $directories;
  31. $this->filePattern = $filePattern ?: '*';
  32. }
  33. public function __toString()
  34. {
  35. return implode(', ', $this->directories);
  36. }
  37. public function getResource()
  38. {
  39. return $this->directories;
  40. }
  41. public function update()
  42. {
  43. $this->files = $this->getFiles();
  44. }
  45. public function isFresh($timestamp)
  46. {
  47. $files = $this->getFiles();
  48. if (array_diff($this->files, $files) || array_diff($files, $this->files)) {
  49. return false;
  50. }
  51. foreach ($files as $file) {
  52. if (filemtime($file) > $timestamp) {
  53. return false;
  54. }
  55. }
  56. return true;
  57. }
  58. private function getFiles()
  59. {
  60. return $this->finder->findFiles($this->directories);
  61. }
  62. }