Example #1
0
 /**
  * Prints a source tokens report
  *
  * @param array  $report  Report data to produce
  * @param string $base    Base directory of data source
  * @param int    $verbose Verbose level (0: none, 1: warnings, ...)
  *
  * @return void
  */
 public function generate($report, $base, $verbose)
 {
     foreach ($report as $filename => $verbose) {
         $reflect = new PHP_Reflect();
         $tokens = $reflect->scan($filename);
         $this->printTHeader($base, $filename, $verbose);
         if ($verbose == 1) {
             foreach ($tokens as $token) {
                 if ($token[0] == 'T_WHITESPACE') {
                     $text = '';
                 } else {
                     $text = str_replace(array("\r", "\n"), '', $token[1]);
                     if (strlen($text) > 40) {
                         $text = explode("\n", wordwrap($text, 40));
                         $text = $text[0];
                     }
                 }
                 printf("%5d  %-30s %s\n", $token[2], $token[0], $text);
             }
         }
         $total = $reflect->getLinesOfCode();
         $this->printTFooter($total);
     }
     echo PHP_EOL;
 }
 protected function setUp()
 {
     $reflect = new PHP_Reflect();
     $tokens = $reflect->scan(TEST_FILES_PATH . 'source.php');
     foreach ($tokens as $id => $token) {
         if ($token[0] == 'T_FUNCTION') {
             $this->functions[] = new PHP_Reflect_Token_FUNCTION($token[1], $token[2], $id, $tokens);
         }
     }
 }
 protected function setUp()
 {
     $reflect = new PHP_Reflect();
     $tokens = $reflect->scan(TEST_FILES_PATH . 'source5.php');
     $i = 0;
     foreach ($tokens as $id => $token) {
         if ($token[0] == 'T_STRING' && $token[1] == 'namespace' || $token[0] == 'T_NAMESPACE') {
             $this->ns = new PHP_Reflect_Token_NAMESPACE($token[1], $token[2], $id, $tokens);
         }
     }
 }
 protected function setUp()
 {
     $reflect = new PHP_Reflect();
     $tokens = $reflect->scan(TEST_FILES_PATH . 'source4.php');
     $i = 0;
     foreach ($tokens as $id => $token) {
         if ($token[0] == 'T_CLASS') {
             $this->class = new PHP_Reflect_Token_CLASS($token[1], $token[2], $id, $tokens);
         } elseif ($token[0] == 'T_INTERFACE') {
             $this->interfaces[$i] = new PHP_Reflect_Token_INTERFACE($token[1], $token[2], $id, $tokens);
             $i++;
         }
     }
 }
 public function get_actions()
 {
     $location = JPATH_BASE . '/app/components/';
     $Directory = new RecursiveDirectoryIterator($location);
     $Iterator = new RecursiveIteratorIterator($Directory);
     $Regex = new RegexIterator($Iterator, '/^.+controller.+/i', RecursiveRegexIterator::GET_MATCH);
     $options = array('properties' => array('class' => array('methods')));
     $reflect = new PHP_Reflect($options);
     $classes = array();
     foreach ($Regex as $path) {
         $source = $path[0];
         $reflect->scan($source);
         $cl = $reflect->getClasses();
         foreach ($cl['\\'] as $k => $cc) {
             foreach ($cc['methods'] as $km => $m) {
                 $classes[$k . $km] = array('title' => sprintf('%s::%s', $k, $km), 'acl_group' => $k, 'acl_name' => $km, 'created_at' => JCURRENT_SERVER_TIME);
             }
         }
     }
     //_xdump($classes);
     $acls_list = new modelUsersAclRules();
     $acls_list->insert_array($classes);
     echo sprintf('Вставлено %s правил', count($classes));
 }
 /**
  * Parse a single file
  * searching for namespaces, interfaces, traits, classes,
  * functions, constants, globals, includes and conditional code
  *
  * @param string $source Source filename
  *
  * @return void
  */
 protected function scan($source)
 {
     $cache = PHP_CompatInfo_Cache::getInstance($this->options['cacheDriver'], $this->options);
     $cached = $cache->isCached($source);
     if ($cached) {
         $results = $cache->getCache($source);
         $this->excludes = $results['excludes'];
         $this->includes = $results['includes'];
         $this->versions = $results['versions'];
         $this->extensions = $results['extensions'];
         $this->namespaces = $results['namespaces'];
         $this->traits = $results['traits'];
         $this->interfaces = $results['interfaces'];
         $this->classes = $results['classes'];
         $this->functions = $results['functions'];
         $this->constants = $results['constants'];
         $this->globals = $results['globals'];
         $this->tokens = $results['tokens'];
         $conditions = $results['conditions'];
     } else {
         $this->excludes = array();
         $this->includes = array();
         $this->versions = array('4.0.0', '');
         $this->extensions = array();
         $this->namespaces = array();
         $this->traits = array();
         $this->interfaces = array();
         $this->classes = array();
         $this->functions = array();
         $this->constants = array();
         $this->globals = array();
         $this->tokens = array();
         $conditions = false;
         /**
          * @link http://www.php.net/manual/en/tokens.php
          *       List of Parser Tokens
          */
         $options = array('containers' => array('core' => 'internalFunctions', 'token' => 'tokens', 'glob' => 'globals'), 'properties' => array('interface' => array('keywords', 'methods', 'parent'), 'class' => array('keywords', 'methods', 'parent', 'interfaces'), 'function' => array('keywords', 'visibility', 'arguments'), 'require_once' => array(), 'require' => array(), 'include_once' => array(), 'include' => array()));
         $reflect = new PHP_Reflect($options);
         // internal functions
         $reflect->connect('T_STRING', 'PHP_CompatInfo_Token_STRING', array('PHP_CompatInfo_TokenParser', 'parseTokenString'));
         // user constants
         $reflect->connect('T_CONSTANT_ENCAPSED_STRING', 'PHP_CompatInfo_Token_CONSTANT_ENCAPSED_STRING', array('PHP_CompatInfo_TokenParser', 'parseTokenConstant'));
         // globals and super globals
         $reflect->connect('T_VARIABLE', 'PHP_Reflect_Token_VARIABLE', array('PHP_CompatInfo_TokenParser', 'parseTokenGlobals'));
         // language features / tokens
         $reflect->connect('T_CATCH', 'PHP_Reflect_Token_CATCH', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_CLONE', 'PHP_Reflect_Token_CLONE', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_INSTANCEOF', 'PHP_Reflect_Token_INSTANCEOF', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_THROW', 'PHP_Reflect_Token_THROW', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_TRY', 'PHP_Reflect_Token_TRY', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_FINALLY', 'PHP_Reflect_Token_FINALLY', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_HALT_COMPILER', 'PHP_Reflect_Token_HALT_COMPILER', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_GOTO', 'PHP_Reflect_Token_GOTO', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_UNSET_CAST', 'PHP_Reflect_Token_UNSET_CAST', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_INSTEADOF', 'PHP_Reflect_Token_INSTEADOF', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_YIELD', 'PHP_Reflect_Token_YIELD', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_OBJECT_OPERATOR', 'PHP_CompatInfo_Token_OBJECT_OPERATOR', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->connect('T_OPEN_SQUARE', 'PHP_CompatInfo_Token_OPEN_SQUARE', array('PHP_CompatInfo_TokenParser', 'parseTokenFeatures'));
         $reflect->scan($source);
         $this->_namespaces = $reflect->getNamespaces(PHP_Reflect::NAMESPACES_ALL);
         /**
          * @link http://www.php.net/manual/en/language.namespaces.php
          *       Namespaces
          */
         $namespaces = $reflect->getNamespaces();
         if ($namespaces === null) {
             // adds (at least) global namespace
             $ns = '\\';
             $namespaces = array($ns => array());
             $defaultVersion = $reflect->isNamespaceWarning() ? '5.3.0' : '4.0.0';
         } else {
             $ns = true;
             $defaultVersion = '5.3.0';
         }
         $this->getInfo('namespaces', $defaultVersion, $namespaces, $source, $ns);
         /**
          * @link http://www.php.net/manual/en/language.control-structures.php
          *       Control Structures
          */
         $includes = $reflect->getIncludes(true);
         foreach ($includes as $key => $values) {
             $this->includes[$key] = array_keys($values);
         }
         foreach (array_keys($namespaces) as $ns) {
             /**
              * @link http://www.php.net/manual/en/language.oop5.traits.php
              *       Traits
              */
             $traits = $reflect->getTraits($ns);
             $this->getInfo('traits', '5.4.0', $traits, $source, $ns);
             /**
              * @link http://www.php.net/manual/en/language.oop5.interfaces.php
              *       Object Interfaces
              */
             $interfaces = $reflect->getInterfaces($ns);
             $this->getInfo('interfaces', '5.0.0', $interfaces, $source, $ns);
             /**
              * @link http://www.php.net/manual/en/language.oop5.php
              *       Classes and Objects
              */
             $classes = $reflect->getClasses($ns);
             $this->getInfo('classes', '4.0.0', $classes, $source, $ns);
             /**
              * @link http://www.php.net/manual/en/language.constants.php
              *       Constants
              */
             $constants = $reflect->getConstants(false, null, $ns);
             $this->getInfo('constants', '4.0.0', $constants, $source, $ns);
             /**
              * @link http://www.php.net/manual/en/functions.user-defined.php
              *       User-defined functions
              * @link http://www.php.net/manual/en/functions.internal.php
              *       Internal (built-in) functions
              */
             $userFunctions = (array) $reflect->getFunctions($ns);
             $coreFunctions = (array) $reflect->getInternalFunctions($ns);
             $functions = array_merge_recursive($userFunctions, $coreFunctions);
             $this->getInfo('functions', '4.0.0', $functions, $source, $ns);
             // language features
             $tokens = (array) $reflect->offsetGet(array('tokens' => $ns));
             $this->getInfo('tokens', '5.0.0', $tokens, $source, $ns);
             /**
              * @link http://www.php.net/manual/en/language.variables.superglobals.php
              *       Superglobals
              */
             $globals = (array) $reflect->getGlobals(true, null, $ns);
             foreach ($globals as $glob => $gdata) {
                 foreach ($gdata as $name => $data) {
                     $data['name'] = $name;
                     $global = array($glob => $data);
                     $this->getInfo('globals', '4.0.0', $global, $source, $ns);
                 }
             }
         }
         // additional search for constants on global namespace
         $constants = $reflect->getConstants(false, null);
         $this->getInfo('constants', '4.0.0', $constants, $source, '\\');
         // updates current source versions only if element is not excluded
         $keys = array('namespaces', 'traits', 'interfaces', 'classes', 'functions', 'constants', 'globals', 'tokens');
         foreach ($keys as $key) {
             foreach ($this->{$key} as $ext => $items) {
                 foreach ($items as $name => $data) {
                     if ($data['excluded'] === false) {
                         $this->updateVersion($data['versions'][0], $this->versions[0]);
                         $this->updateVersion($data['versions'][1], $this->versions[1]);
                     }
                 }
             }
         }
     }
     $this->results[$source] = array('excludes' => $this->excludes, 'includes' => $this->includes, 'versions' => $this->versions, 'extensions' => $this->extensions, 'namespaces' => $this->namespaces, 'traits' => $this->traits, 'interfaces' => $this->interfaces, 'classes' => $this->classes, 'functions' => $this->functions, 'constants' => $this->constants, 'globals' => $this->globals, 'tokens' => $this->tokens, 'conditions' => $conditions);
     if ($conditions === false) {
         // search for conditional code
         $this->results[$source]['conditions'] = $this->getConditions(null, $source);
     }
     if (!$cached) {
         // write results in a cache to improve speed for later uses
         $cache->setCache($source, $this->results[$source]);
     }
 }