Core  3.2
PHP API documentation
 All Data Structures Namespaces Files Functions Variables Pages
classAutoloaderIgnoreDotD.php
Go to the documentation of this file.
1 <?php
2 /*
3  * @author Anakeen
4  * @package FDL
5 */
6 
7 namespace Dcp;
8 
9 class autoloaderIgnoreDotD extends \FilterIterator
10 {
11  private $_ignoreGlobList = false;
12 
13  public function accept()
14  {
15  $c = $this->current()->getPathname();
16 
17  if (substr($c, -4) != '.php') return false;
18  if (preg_match('/^Method/', basename($c))) return false;
19 
20  if ($this->_inAutoloaderIgnore($c)) return false;
21 
22  return is_readable($c);
23  }
24  /**
25  * Check filenames againts glob patterns from .autoloader-ignore.d/* files and
26  * evaluate if the given filename should be ignored.
27  *
28  * @param string $fileName The filename to check
29  * @return bool bool(true) if the filename is ignored, bool(false) if the filename is not ignored
30  */
31  private function _inAutoloaderIgnore($fileName)
32  {
33  if ($this->_ignoreGlobList === false) {
34  $this->_ignoreGlobList = $this->_parseAutoloaderIgnoreDotD();
35  }
36  if (strpos($fileName, './') === 0) {
37  $fileName = substr($fileName, 2);
38  }
39  foreach ($this->_ignoreGlobList as $glob) {
40  if (fnmatch($glob, $fileName)) {
41  return true;
42  }
43  }
44  return false;
45  }
46  /**
47  * Load and parse an autoloader ignore file returning the list of globs.
48  *
49  * @param string $ignoreFile Path to the autoloader ignore file to parse
50  * @throws DirectoriesAutoloaderException
51  * @return array|bool bool(false) on error, list of glob patterns on success
52  */
53  private function _parseAutoloaderIgnoreFile($ignoreFile)
54  {
55  $globList = file($ignoreFile);
56  if ($globList === false) {
57  throw new DirectoriesAutoloaderException(sprintf("Error opening autoloader ignore file '%s'.", $ignoreFile));
58  }
59  $globList = array_filter(array_map(function ($e)
60  {
61  /* Remove trailing CR+LF */
62  return rtrim($e, "\r\n");
63  }
64  , $globList) , function ($e)
65  {
66  /* Skip blank and comment lines */
67  return !preg_match('/^\s*(#.*)?$/', $e);
68  });
69  $globList = array_map(function ($e)
70  {
71  /*
72  * Strip leading './' from glob patterns as they are
73  * already considered relatives to the current directory.
74  */
75  if (strpos($e, './') === 0) {
76  $e = substr($e, 2);
77  }
78  return $e;
79  }
80  , $globList);
81  return $globList;
82  }
83  /**
84  * Load and parse ignore files from `$wpub/.autoloader-ignore.d` dir
85  *
86  * @throws DirectoriesAutoloaderException
87  * @return array|bool bool(false) on error, or list of glob patterns on success
88  */
89  private function _parseAutoloaderIgnoreDotD()
90  {
91  global $pubdir;
92  $globList = array();
93  $dir = $pubdir . DIRECTORY_SEPARATOR . '.autoloader-ignore.d';
94  if (!is_dir($dir)) {
95  return $globList;
96  }
97  $dh = opendir($dir);
98  if ($dh === false) {
99  throw new DirectoriesAutoloaderException(sprintf("Error opening '.autoloader-ignore.d' directory '%s'.", $dir));
100  }
101  while (($file = readdir($dh)) !== false) {
102  if ($file == '.' || $file == '..') {
103  continue;
104  }
105  $globList = array_merge($globList, $this->_parseAutoloaderIgnoreFile($dir . DIRECTORY_SEPARATOR . $file));
106  }
107  closedir($dh);
108  return $globList;
109  }
110 }
global $pubdir
Definition: vault_init.php:18
$file
$dir
Definition: resizeimg.php:144
← centre documentaire © anakeen