Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions
Zend_File_ClassFileLocator Class Reference
Inheritance diagram for Zend_File_ClassFileLocator:

Public Member Functions

 __construct ($dirOrIterator='.')
 
 accept ()
 

Detailed Description

Definition at line 30 of file ClassFileLocator.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $dirOrIterator = '.')

Create an instance of the locator iterator

Expects either a directory, or a DirectoryIterator (or its recursive variant) instance.

Parameters
string | DirectoryIterator$dirOrIterator

Definition at line 40 of file ClassFileLocator.php.

41  {
42  if (is_string($dirOrIterator)) {
43  if (!is_dir($dirOrIterator)) {
44  throw new InvalidArgumentException('Expected a valid directory name');
45  }
46 
47  $dirOrIterator = new RecursiveDirectoryIterator($dirOrIterator);
48  }
49  if (!$dirOrIterator instanceof DirectoryIterator) {
50  throw new InvalidArgumentException('Expected a DirectoryIterator');
51  }
52 
53  if ($dirOrIterator instanceof RecursiveIterator) {
54  $iterator = new RecursiveIteratorIterator($dirOrIterator);
55  } else {
56  $iterator = $dirOrIterator;
57  }
58 
59  parent::__construct($iterator);
60  $this->setInfoClass('Zend_File_PhpClassFile');
61 
62  // Forward-compat with PHP 5.3
63  if (version_compare(PHP_VERSION, '5.3.0', '<')) {
64  if (!defined('T_NAMESPACE')) {
65  define('T_NAMESPACE', 'namespace');
66  }
67  if (!defined('T_NS_SEPARATOR')) {
68  define('T_NS_SEPARATOR', '\\');
69  }
70  }
71  }

Member Function Documentation

◆ accept()

accept ( )

Filter for files containing PHP classes, interfaces, or abstracts

Returns
bool

Definition at line 78 of file ClassFileLocator.php.

79  {
80  $file = $this->getInnerIterator()->current();
81  // If we somehow have something other than an SplFileInfo object, just
82  // return false
83  if (!$file instanceof SplFileInfo) {
84  return false;
85  }
86 
87  // If we have a directory, it's not a file, so return false
88  if (!$file->isFile()) {
89  return false;
90  }
91 
92  // If not a PHP file, skip
93  if ($file->getBasename('.php') == $file->getBasename()) {
94  return false;
95  }
96 
97  $contents = file_get_contents($file->getRealPath());
98  $tokens = token_get_all($contents);
99  $count = count($tokens);
100  $t_trait = defined('T_TRAIT') ? T_TRAIT : -1; // For preserve PHP 5.3 compatibility
101  for ($i = 0; $i < $count; $i++) {
102  $token = $tokens[$i];
103  if (!is_array($token)) {
104  // single character token found; skip
105  $i++;
106  continue;
107  }
108  switch ($token[0]) {
109  case T_NAMESPACE:
110  // Namespace found; grab it for later
111  $namespace = '';
112  for ($i++; $i < $count; $i++) {
113  $token = $tokens[$i];
114  if (is_string($token)) {
115  if (';' === $token) {
116  $saveNamespace = false;
117  break;
118  }
119  if ('{' === $token) {
120  $saveNamespace = true;
121  break;
122  }
123  continue;
124  }
125  list($type, $content, $line) = $token;
126  switch ($type) {
127  case T_STRING:
128  case T_NS_SEPARATOR:
129  $namespace .= $content;
130  break;
131  }
132  }
133  if ($saveNamespace) {
134  $savedNamespace = $namespace;
135  }
136  break;
137  case $t_trait:
138  case T_CLASS:
139  case T_INTERFACE:
140  // Abstract class, class, interface or trait found
141 
142  // Get the classname
143  for ($i++; $i < $count; $i++) {
144  $token = $tokens[$i];
145  if (is_string($token)) {
146  continue;
147  }
148  list($type, $content, $line) = $token;
149  if (T_STRING == $type) {
150  // If a classname was found, set it in the object, and
151  // return boolean true (found)
152  if (!isset($namespace) || null === $namespace) {
153  if (isset($saveNamespace) && $saveNamespace) {
154  $namespace = $savedNamespace;
155  } else {
156  $namespace = null;
157  }
158 
159  }
160  $class = (null === $namespace) ? $content : $namespace . '\\' . $content;
161  $file->addClass($class);
162  $namespace = null;
163  break;
164  }
165  }
166  break;
167  default:
168  break;
169  }
170  }
171  $classes = $file->getClasses();
172  if (!empty($classes)) {
173  return true;
174  }
175  // No class-type tokens found; return false
176  return false;
177  }
$contents
Definition: website.php:14
$count
Definition: recent.phtml:13
$type
Definition: item.phtml:13
$_option $_optionId $class
Definition: date.phtml:13
$i
Definition: gallery.phtml:31
$tokens
Definition: cards_list.phtml:9

The documentation for this class was generated from the following file: