示例#1
0
 /**
  * Create token collection directly from code.
  *
  * @param string $code PHP code
  *
  * @return Tokens
  */
 public static function fromCode($code)
 {
     $codeHash = crc32($code);
     if (self::hasCache($codeHash)) {
         $tokens = self::getCache($codeHash);
         // generate the code to recalculate the hash
         $tokens->generateCode();
         if ($codeHash === $tokens->codeHash) {
             $tokens->clearEmptyTokens();
             return $tokens;
         }
     }
     $tokens = token_get_all($code);
     foreach ($tokens as $index => $tokenPrototype) {
         $tokens[$index] = new Token($tokenPrototype);
     }
     $collection = self::fromArray($tokens);
     $transformers = Transformers::create();
     $transformers->transform($collection);
     $collection->changeCodeHash($codeHash);
     return $collection;
 }
示例#2
0
 /**
  * Get token name.
  *
  * @return null|string token name
  */
 public function getName()
 {
     if (!isset($this->id)) {
         return;
     }
     $transformers = Transformers::create();
     if ($transformers->hasCustomToken($this->id)) {
         return $transformers->getCustomToken($this->id);
     }
     return token_name($this->id);
 }