Overview

Packages

  • CONTENIDO
  • Core
    • Authentication
    • Backend
    • Cache
    • CEC
    • Chain
    • ContentType
    • Database
    • Debug
    • Exception
    • Frontend
      • Search
      • URI
      • Util
    • GenericDB
      • Model
    • GUI
      • HTML
    • I18N
    • LayoutHandler
    • Log
    • Security
    • Session
    • Util
    • Validation
    • Versioning
    • XML
  • Module
    • ContentRssCreator
    • ContentSitemapHtml
    • ContentSitemapXml
    • ContentUserForum
    • NavigationTop
    • ScriptCookieDirective
  • mpAutoloaderClassMap
  • None
  • Plugin
    • ContentAllocation
    • CronjobOverview
    • FormAssistant
    • FrontendLogic
    • FrontendUsers
    • Linkchecker
    • ModRewrite
    • Newsletter
    • Repository
      • FrontendNavigation
      • KeywordDensity
    • SearchSolr
    • SmartyWrapper
    • UrlShortener
    • UserForum
    • Workflow
  • PluginManager
  • Setup
    • Form
    • GUI
    • Helper
      • Environment
      • Filesystem
      • MySQL
      • PHP
    • UpgradeJob

Classes

  • mpClassMapFileCreator
  • mpClassMapFileCreatorContenido
  • mpClassTypeFinder
  • Overview
  • Package
  • Class
  • Tree
  • Deprecated
  • Todo
  1: <?php
  2: /**
  3:  * Contains class type token finder.
  4:  *
  5:  * @category    Development
  6:  * @package     mpAutoloaderClassMap
  7:  * @author        Murat Purc <murat@purc.de>
  8:  * @copyright   Copyright (c) 2009-2010 Murat Purc (http://www.purc.de)
  9:  * @license     http://www.gnu.org/licenses/gpl-2.0.html - GNU General Public License, version 2
 10:  * @version     $Id$
 11:  */
 12: 
 13: 
 14: /**
 15:  * Class to find class type tokens
 16:  *
 17:  * @category    Development
 18:  * @package     mpAutoloaderClassMap
 19:  * @author        Murat Purc <murat@purc.de>
 20:  */
 21: class mpClassTypeFinder
 22: {
 23:     /**
 24:      * List of directories to ignore (note: is case insensitive)
 25:      * @var  array
 26:      */
 27:     protected $_excludeDirs = array('.svn', '.cvs');
 28: 
 29:     /**
 30:      * List of files to ignore, regex pattern is also accepted (note: is case insensitive)
 31:      * @var  array
 32:      */
 33:     protected $_excludeFiles = array('/^~*.\.php$/', '/^~*.\.inc$/');
 34: 
 35:     /**
 36:      * List of file extensions to parse (note: is case insensitive)
 37:      * @var  array
 38:      */
 39:     protected $_extensionsToParse = array('.php', '.inc');
 40: 
 41:     /**
 42:      * Flag to enable debugging, all messages will be collected in property _debugMessages,
 43:      * if enabled
 44:      * @var  bool
 45:      */
 46:     protected $_enableDebug = false;
 47: 
 48:     /**
 49:      * List of debugging messages, will e filled, if debugging is active
 50:      * @var  array
 51:      */
 52:     protected $_debugMessages = array();
 53: 
 54: 
 55:     /**
 56:      * Initializes class with passed options
 57:      *
 58:      * @param   array  $options  Assoziative options array as follows:
 59:      *                           - excludeDirs: (array)  List of directories to exclude, optional.
 60:      *                               Default values are '.svn' and '.cvs'.
 61:      *                           - excludeFiles: (array)  List of files to exclude, optional.
 62:      *                               Default values are '/^~*.\.php$/' and '/^~*.\.inc$/'.
 63:      *                           - extensionsToParse: (array)  List of file extensions to parse, optional.
 64:      *                               Default values are '.php' and '.inc'.
 65:      *                           - enableDebug: (bool)  Flag to enable debugging, optional.
 66:      *                               Default value is false.
 67:      */
 68:     public function __construct(array $options=array())
 69:     {
 70:         if (isset($options['excludeDirs']) && is_array($options['excludeDirs'])) {
 71:             $this->setExcludeDirs($options['excludeDirs']);
 72:         }
 73:         if (isset($options['excludeFiles']) && is_array($options['excludeFiles'])) {
 74:             $this->setExcludeFiles($options['excludeFiles']);
 75:         }
 76:         if (isset($options['extensionsToParse']) && is_array($options['extensionsToParse'])) {
 77:             $this->setExtensionsToParse($options['extensionsToParse']);
 78:         }
 79:         if (isset($options['enableDebug']) && is_bool($options['enableDebug'])) {
 80:             $this->_enableDebug = $options['enableDebug'];
 81:         }
 82:     }
 83: 
 84: 
 85:     /**
 86:      * Sets directories to exclude
 87:      *
 88:      * @param   array  $excludeDirs
 89:      * @return  void
 90:      */
 91:     public function setExcludeDirs(array $excludeDirs)
 92:     {
 93:         $this->_excludeDirs = $excludeDirs;
 94:     }
 95: 
 96: 
 97:     /**
 98:      * Returns list of directories to exclude
 99:      *
100:      * @return  array
101:      */
102:     public function getExcludeDirs()
103:     {
104:         return $this->_excludeDirs;
105:     }
106: 
107: 
108:     /**
109:      * Sets files to exclude
110:      *
111:      * @param   array  $excludeFiles  Feasible values are
112:      *                                - temp.php (single file name)
113:      *                                - ~*.php (with * wildcard)
114:      *                                  Will be replaced against regex '/^~.*\.php$/'
115:      */
116:     public function setExcludeFiles(array $excludeFiles)
117:     {
118:         foreach ($excludeFiles as $pos => $entry) {
119:             if (strpos($entry, '*') !== false) {
120:                 $entry = '/^' . str_replace('*', '.*', preg_quote($entry)) . '$/';
121:                 $excludeFiles[$pos] = $entry;
122:             }
123:         }
124:         $this->_excludeFiles = $excludeFiles;
125:     }
126: 
127: 
128:     /**
129:      * Returns list of files to exclude
130:      *
131:      * @return  array
132:      */
133:     public function getExcludeFiles()
134:     {
135:         return $this->_excludeFiles;
136:     }
137: 
138: 
139:     /**
140:      * Sets file extensions to parse
141:      *
142:      * @param   array  $extensionsToParse
143:      */
144:     public function setExtensionsToParse(array $extensionsToParse)
145:     {
146:         $this->_extensionsToParse = $extensionsToParse;
147:     }
148: 
149: 
150:     /**
151:      * Returns list of file extension to parse
152:      *
153:      * @return  array
154:      */
155:     public function getExtensionsToParse()
156:     {
157:         return $this->_extensionsToParse;
158:     }
159: 
160: 
161:     /**
162:      * Detects all available class type tokens in found files inside passed directory.
163:      *
164:      * @param   SplFileInfo    $fileInfo
165:      * @param   bool           $recursive  Flag to parse directory recursive
166:      * @return  array|NULL     Either a assoziative array where the key is the class
167:      *                         type token and the value is the path or NULL.
168:      */
169:     public function findInDir(SplFileInfo $fileInfo, $recursive=true)
170:     {
171:         if (!$fileInfo->isDir() || !$fileInfo->isReadable()) {
172:             $this->_debug('findInDir: Invalid/Not readable directory ' . $fileInfo->getPathname());
173:             return NULL;
174:         }
175:         $this->_debug('findInDir: Processing dir ' . $fileInfo->getPathname() . ' (realpath: ' . $fileInfo->getRealPath() . ')');
176: 
177:         $classTypeTokens = array();
178: 
179:         $iterator = $this->_getDirIterator($fileInfo, $recursive);
180: 
181:         foreach ($iterator as $file) {
182: 
183:             if ($this->_isFileToProccess($file)) {
184:                 if ($foundTokens = $this->findInFile($file)) {
185:                      $classTypeTokens = array_merge($classTypeTokens, $foundTokens);
186:                 }
187:             }
188:         }
189: 
190:         return (count($classTypeTokens) > 0) ? $classTypeTokens : NULL;
191:     }
192: 
193: 
194:     /**
195:      * Detects all available class type tokens in passed file
196:      *
197:      * @param   SplFileInfo    $fileInfo
198:      * @return  array|NULL     Either a assoziative array where the key is the class
199:      *                         type token and the value is the path or NULL.
200:      */
201:     public function findInFile(SplFileInfo $fileInfo)
202:     {
203:         if (!$fileInfo->isFile() || !$fileInfo->isReadable()) {
204:             $this->_debug('findInFile: Invalid/Not readable file ' . $fileInfo->getPathname());
205:             return NULL;
206:         }
207:         $this->_debug('findInFile: Processing file ' . $fileInfo->getPathname() . ' (realpath: ' . $fileInfo->getRealPath() . ')');
208: 
209:         $classTypeTokens = array();
210: 
211:         $tokens  = token_get_all(file_get_contents($fileInfo->getRealPath()));
212:         $prevTokenFound = false;
213:         foreach ($tokens as $p => $token) {
214:             if ($token[0] == T_INTERFACE) {
215:                 $this->_debug('findInFile: T_INTERFACE token found (token pos ' . $p . ')');
216:                 $prevTokenFound = true;
217:             // } elseif ($token[0] == T_ABSTRACT) {
218:             //     $this->_debug('findInFile: T_ABSTRACT token found (token pos ' . $p . ')');
219:             //     $prevTokenFound = true;
220:             } elseif ($token[0] == T_CLASS) {
221:                 $this->_debug('findInFile: T_CLASS token found (token pos ' . $p . ')');
222:                 $prevTokenFound = true;
223:             }
224:             if ($prevTokenFound && $token[0] !== T_STRING) {
225:                 continue;
226:             } elseif ($prevTokenFound && $token[0] == T_STRING) {
227:                 $classTypeTokens[$token[1]] = $this->_normalizePathSeparator($fileInfo->getRealPath());
228:                 $prevTokenFound = false;
229:             }
230:         }
231: 
232:         return (count($classTypeTokens) > 0) ? $classTypeTokens : NULL;
233:     }
234: 
235: 
236:     /**
237:      * Returns list of debug messages
238:      *
239:      * @return  array
240:      */
241:     public function getDebugMessages()
242:     {
243:         return $this->_debugMessages;
244:     }
245: 
246: 
247:     /**
248:      * Returns debug messages in a formatted way.
249:      *
250:      * @param   string  $delemiter  Delemiter between each message
251:      * @param   string  $wrap       String with %s type specifier used to wrap all
252:      *                              messages
253:      * @throws cInvalidArgumentException if the given wrap does not contain %s
254:      * @return  string  Formatted string
255:      */
256:     public function getFormattedDebugMessages($delemiter="\n", $wrap='%s')
257:     {
258:         if (strpos($wrap, '%s') === false) {
259:             throw new cInvalidArgumentException('Missing type specifier %s in parameter wrap!');
260:         }
261:         $messages = implode($delemiter, $this->_debugMessages);
262:         return sprintf($wrap, $messages);
263:     }
264: 
265: 
266:     /**
267:      * Adds passed message to debug list, if debugging is enabled
268:      *
269:      * @param   string  $msg
270:      */
271:     protected function _debug($msg)
272:     {
273:         if ($this->_enableDebug) {
274:             $this->_debugMessages[] = $msg;
275:         }
276:     }
277: 
278: 
279:     /**
280:      * Returns directory iterator depending on $recursive parameter value
281:      *
282:      * @param   SplFileInfo  $fileInfo
283:      * @param   bool         $recursive
284:      * @return  RecursiveIteratorIterator|DirectoryIterator
285:      */
286:     protected function _getDirIterator(SplFileInfo $fileInfo, $recursive)
287:     {
288:         if ($recursive === true) {
289:             return new RecursiveIteratorIterator(
290:                 new RecursiveDirectoryIterator($fileInfo->getRealPath()),
291:                 RecursiveIteratorIterator::SELF_FIRST
292:             );
293:         } else {
294:             return new DirectoryIterator($fileInfo->getRealPath());
295:         }
296:     }
297: 
298: 
299:     /**
300:      * Checks if file is to proccess
301:      *
302:      * @param   SplFileInfo  $file
303:      * @return  bool
304:      */
305:     protected function _isFileToProccess(SplFileInfo $file)
306:     {
307:         if ($this->_isDirToExclude($file)) {
308:             $this->_debug('_isFileToProccess: Dir to exclude ' . $file->getPathname() . ' (realpath: ' . $file->getRealPath() . ')');
309:             return false;
310:         }
311:         if ($this->_isFileToExclude($file)) {
312:             $this->_debug('_isFileToProccess: File to exclude ' . $file->getPathname() . ' (realpath: ' . $file->getRealPath() . ')');
313:             return false;
314:         }
315:         if ($this->_isFileToParse($file)) {
316:             $this->_debug('_isFileToProccess: File to parse ' . $file->getPathname() . ' (realpath: ' . $file->getRealPath() . ')');
317:             return true;
318:         }
319:         return false;
320:     }
321: 
322: 
323:     /**
324:      * Checks if directory is to exclude
325:      *
326:      * @param   SplFileInfo  $file
327:      * @return  bool
328:      */
329:     protected function _isDirToExclude(SplFileInfo $file)
330:     {
331:         $path = strtolower($this->_normalizePathSeparator($file->getRealPath()));
332: 
333:         foreach ($this->_excludeDirs as $item) {
334:             if (strpos($path, $item) !== false) {
335:                 return true;
336:             }
337:         }
338:         return false;
339:     }
340: 
341: 
342:     /**
343:      * Checks if file is to exclude
344:      *
345:      * @param   SplFileInfo  $file
346:      * @return  bool
347:      */
348:     protected function _isFileToExclude(SplFileInfo $file)
349:     {
350:         $path = strtolower($this->_normalizePathSeparator($file->getRealPath()));
351: 
352:         foreach ($this->_excludeFiles as $item) {
353:             if (strlen($item) > 2 && substr($item, 0, 2) == '/^') {
354:                 if (preg_match($item, $path)) {
355:                     return true;
356:                 }
357:             } else if (strpos($path, $item) !== false) {
358:                 return true;
359:             }
360:         }
361:         return false;
362:     }
363: 
364: 
365:     /**
366:      * Checks if file is to parse (if file extension matches)
367:      *
368:      * @param   SplFileInfo  $file
369:      * @return  bool
370:      */
371:     protected function _isFileToParse(SplFileInfo $file)
372:     {
373:         $path = strtolower($this->_normalizePathSeparator($file->getRealPath()));
374: 
375:         foreach ($this->_extensionsToParse as $item) {
376:             if (substr($path, -strlen($item)) == $item) {
377:                 return true;
378:             }
379:         }
380:         return false;
381:     }
382: 
383: 
384:     /**
385:      * Replaces windows style directory separator (backslash against slash)
386:      *
387:      * @param   string
388:      * @return  string
389:      */
390:     protected function _normalizePathSeparator($path)
391:     {
392:         if (DIRECTORY_SEPARATOR == '\\') {
393:             $path = str_replace(DIRECTORY_SEPARATOR, '/', $path);
394:         }
395:         return $path;
396:     }
397: 
398: }
399: 
CMS CONTENIDO 4.9.11 API documentation generated by ApiGen 2.8.0