AbstractAdapter.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Finder\Adapter;
  11. /**
  12. * Interface for finder engine implementations.
  13. *
  14. * @author Jean-François Simon <contact@jfsimon.fr>
  15. */
  16. abstract class AbstractAdapter implements AdapterInterface
  17. {
  18. protected $followLinks = false;
  19. protected $mode = 0;
  20. protected $minDepth = 0;
  21. protected $maxDepth = PHP_INT_MAX;
  22. protected $exclude = array();
  23. protected $names = array();
  24. protected $notNames = array();
  25. protected $contains = array();
  26. protected $notContains = array();
  27. protected $sizes = array();
  28. protected $dates = array();
  29. protected $filters = array();
  30. protected $sort = false;
  31. protected $paths = array();
  32. protected $notPaths = array();
  33. protected $ignoreUnreadableDirs = false;
  34. private static $areSupported = array();
  35. /**
  36. * {@inheritDoc}
  37. */
  38. public function isSupported()
  39. {
  40. $name = $this->getName();
  41. if (!array_key_exists($name, self::$areSupported)) {
  42. self::$areSupported[$name] = $this->canBeUsed();
  43. }
  44. return self::$areSupported[$name];
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function setFollowLinks($followLinks)
  50. {
  51. $this->followLinks = $followLinks;
  52. return $this;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function setMode($mode)
  58. {
  59. $this->mode = $mode;
  60. return $this;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function setDepths(array $depths)
  66. {
  67. $this->minDepth = 0;
  68. $this->maxDepth = PHP_INT_MAX;
  69. foreach ($depths as $comparator) {
  70. switch ($comparator->getOperator()) {
  71. case '>':
  72. $this->minDepth = $comparator->getTarget() + 1;
  73. break;
  74. case '>=':
  75. $this->minDepth = $comparator->getTarget();
  76. break;
  77. case '<':
  78. $this->maxDepth = $comparator->getTarget() - 1;
  79. break;
  80. case '<=':
  81. $this->maxDepth = $comparator->getTarget();
  82. break;
  83. default:
  84. $this->minDepth = $this->maxDepth = $comparator->getTarget();
  85. }
  86. }
  87. return $this;
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function setExclude(array $exclude)
  93. {
  94. $this->exclude = $exclude;
  95. return $this;
  96. }
  97. /**
  98. * {@inheritdoc}
  99. */
  100. public function setNames(array $names)
  101. {
  102. $this->names = $names;
  103. return $this;
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function setNotNames(array $notNames)
  109. {
  110. $this->notNames = $notNames;
  111. return $this;
  112. }
  113. /**
  114. * {@inheritdoc}
  115. */
  116. public function setContains(array $contains)
  117. {
  118. $this->contains = $contains;
  119. return $this;
  120. }
  121. /**
  122. * {@inheritdoc}
  123. */
  124. public function setNotContains(array $notContains)
  125. {
  126. $this->notContains = $notContains;
  127. return $this;
  128. }
  129. /**
  130. * {@inheritdoc}
  131. */
  132. public function setSizes(array $sizes)
  133. {
  134. $this->sizes = $sizes;
  135. return $this;
  136. }
  137. /**
  138. * {@inheritdoc}
  139. */
  140. public function setDates(array $dates)
  141. {
  142. $this->dates = $dates;
  143. return $this;
  144. }
  145. /**
  146. * {@inheritdoc}
  147. */
  148. public function setFilters(array $filters)
  149. {
  150. $this->filters = $filters;
  151. return $this;
  152. }
  153. /**
  154. * {@inheritdoc}
  155. */
  156. public function setSort($sort)
  157. {
  158. $this->sort = $sort;
  159. return $this;
  160. }
  161. /**
  162. * {@inheritdoc}
  163. */
  164. public function setPath(array $paths)
  165. {
  166. $this->paths = $paths;
  167. return $this;
  168. }
  169. /**
  170. * {@inheritdoc}
  171. */
  172. public function setNotPath(array $notPaths)
  173. {
  174. $this->notPaths = $notPaths;
  175. return $this;
  176. }
  177. /**
  178. * {@inheritdoc}
  179. */
  180. public function ignoreUnreadableDirs($ignore = true)
  181. {
  182. $this->ignoreUnreadableDirs = (Boolean) $ignore;
  183. return $this;
  184. }
  185. /**
  186. * Returns whether the adapter is supported in the current environment.
  187. *
  188. * This method should be implemented in all adapters. Do not implement
  189. * isSupported in the adapters as the generic implementation provides a cache
  190. * layer.
  191. *
  192. * @see isSupported
  193. *
  194. * @return Boolean Whether the adapter is supported
  195. */
  196. abstract protected function canBeUsed();
  197. }