PageRenderTime 47ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/_app/core/tags/get_files/pi.get_files.php

https://bitbucket.org/sirestudios/fortis-wellness
PHP | 165 lines | 56 code | 26 blank | 83 comment | 8 complexity | 1c0afb6ae4759fda85461913c89faec3 MD5 | raw file
Possible License(s): JSON
  1. <?php
  2. use Symfony\Component\Finder\Finder as Finder;
  3. class Plugin_Get_Files extends Plugin
  4. {
  5. public function index()
  6. {
  7. /*
  8. |--------------------------------------------------------------------------
  9. | Paramers
  10. |--------------------------------------------------------------------------
  11. |
  12. | Match overrides Extension. Exclusion applies in both cases.
  13. |
  14. */
  15. $match = $this->fetchParam('match', false);
  16. $exclude = $this->fetchParam('exclude', false);
  17. $extension = $this->fetchParam('extension', false);
  18. $in = $this->fetchParam('in', false);
  19. $not_in = $this->fetchParam('not_in', false);
  20. $file_size = $this->fetchParam('file_size', false);
  21. $file_date = $this->fetchParam('file_date', false);
  22. $depth = $this->fetchParam('depth', false);
  23. if ($file_size) {
  24. $file_size = Helper::explodeOptions($file_size);
  25. }
  26. if ($extension) {
  27. $extension = Helper::explodeOptions($extension);
  28. }
  29. /*
  30. |--------------------------------------------------------------------------
  31. | Finder
  32. |--------------------------------------------------------------------------
  33. |
  34. | Get_Files implements most of the Symfony Finder component as a clean
  35. | tag wrapper mapped to matched filenames.
  36. |
  37. */
  38. $finder = new Finder();
  39. $finder->in($in);
  40. // Finder doesn't respect multiple glob options,
  41. // so this will need to wait until later.
  42. //
  43. // $match = str_replace('{{', '{', $match);
  44. // $match = str_replace('}}', '}', $match);
  45. /*
  46. |--------------------------------------------------------------------------
  47. | Name
  48. |--------------------------------------------------------------------------
  49. |
  50. | Match is the "native" Finder name() method, which is supposed to
  51. | implement string, glob, and regex. The glob support is only partial,
  52. | so "extension" is a looped *single* glob rule iterator.
  53. |
  54. */
  55. if ($match) {
  56. $finder->name($match);
  57. } elseif ($extension) {
  58. foreach ($extension as $ext) {
  59. $finder->name("*.{$ext}");
  60. }
  61. }
  62. /*
  63. |--------------------------------------------------------------------------
  64. | Exclude
  65. |--------------------------------------------------------------------------
  66. |
  67. | Exclude directories from matching. Remapped to "not in" to allow more
  68. | intuitive differentiation between filename and directory matching.
  69. |
  70. */
  71. if ($not_in) {
  72. $finder->exclude($not_in);
  73. }
  74. /*
  75. |--------------------------------------------------------------------------
  76. | Not Name
  77. |--------------------------------------------------------------------------
  78. |
  79. | Exclude files matching a given pattern: string, regex, or glob.
  80. |
  81. */
  82. if ($exclude) {
  83. $finder->notName($exclude);
  84. }
  85. /*
  86. |--------------------------------------------------------------------------
  87. | File Size
  88. |--------------------------------------------------------------------------
  89. |
  90. | Restrict files by size. Can be chained and allows comparison operators.
  91. |
  92. */
  93. if ($file_size) {
  94. foreach($file_size as $size) {
  95. $finder->size($size);
  96. }
  97. }
  98. /*
  99. |--------------------------------------------------------------------------
  100. | File Date
  101. |--------------------------------------------------------------------------
  102. |
  103. | Restrict files by last modified date. Can use comparison operators, and
  104. | since/after is aliased to >, and until/before to <.
  105. |
  106. */
  107. if ($file_date) {
  108. $finder->date($file_date);
  109. }
  110. /*
  111. |--------------------------------------------------------------------------
  112. | Depth
  113. |--------------------------------------------------------------------------
  114. |
  115. | Recursively traverse directories, starting at 0.
  116. |
  117. */
  118. if ($depth) {
  119. $finder->depth($depth);
  120. }
  121. $matches = $finder->files()->followLinks();
  122. /*
  123. |--------------------------------------------------------------------------
  124. | Return and Parse
  125. |--------------------------------------------------------------------------
  126. |
  127. | This tag returns the matched filenames mapped to {{ file }}.
  128. |
  129. */
  130. $files = array();
  131. foreach ($matches as $file) {
  132. $files[] = array(
  133. 'file' => '/' . $in . '/' . $file->getRelativePathname()
  134. );
  135. // $files[] = YAML::parse($file->getContents());
  136. }
  137. return Parse::tagLoop($this->content, $files);
  138. }
  139. }