/** * Iterate over all files in the given directory searching for classes. * * @param \Iterator|string $dir The directory to search in or an iterator * * @return array A class map array */ public static function createMap($dir) { if (is_string($dir)) { $dir = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($dir)); } $map = array(); foreach ($dir as $file) { if (!$file->isFile()) { continue; } $path = $file->getRealPath(); if (pathinfo($path, PATHINFO_EXTENSION) !== 'php') { continue; } $classes = self::findClasses($path); if (PHP_VERSION_ID >= 70000) { // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 gc_mem_caches(); } foreach ($classes as $class) { $map[$class] = $path; } } return $map; }
/** * {@inheritdoc} */ public function extract($resource, MessageCatalogue $catalog) { $files = $this->extractFiles($resource); foreach ($files as $file) { $this->parseTokens(token_get_all(file_get_contents($file)), $catalog); if (PHP_VERSION_ID >= 70000) { // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 gc_mem_caches(); } } }
/** * Loads from annotations from a file. * * @param string $file A PHP file path * @param string|null $type The resource type * * @return RouteCollection A RouteCollection instance * * @throws \InvalidArgumentException When the file does not exist or its routes cannot be parsed */ public function load($file, $type = null) { $path = $this->locator->locate($file); $collection = new RouteCollection(); if ($class = $this->findClass($path)) { $collection->addResource(new FileResource($path)); $collection->addCollection($this->loader->load($class, $type)); } if (PHP_VERSION_ID >= 70000) { // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 gc_mem_caches(); } return $collection; }
/** * Removes comments from a PHP source string. * * We don't use the PHP php_strip_whitespace() function * as we want the content to be readable and well-formatted. * * @param string $source A PHP string * * @return string The PHP string with the comments removed */ public static function stripComments($source) { if (!function_exists('token_get_all')) { return $source; } $rawChunk = ''; $output = ''; $tokens = token_get_all($source); $ignoreSpace = false; for ($i = 0; isset($tokens[$i]); ++$i) { $token = $tokens[$i]; if (!isset($token[1]) || 'b"' === $token) { $rawChunk .= $token; } elseif (T_START_HEREDOC === $token[0]) { $output .= $rawChunk . $token[1]; do { $token = $tokens[++$i]; $output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token; } while ($token[0] !== T_END_HEREDOC); $rawChunk = ''; } elseif (T_WHITESPACE === $token[0]) { if ($ignoreSpace) { $ignoreSpace = false; continue; } // replace multiple new lines with a single newline $rawChunk .= preg_replace(array('/\\n{2,}/S'), "\n", $token[1]); } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) { $ignoreSpace = true; } else { $rawChunk .= $token[1]; // The PHP-open tag already has a new-line if (T_OPEN_TAG === $token[0]) { $ignoreSpace = true; } } } $output .= $rawChunk; if (PHP_VERSION_ID >= 70000) { // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 unset($tokens, $rawChunk); gc_mem_caches(); } return $output; }
/** * Adds brackets around each namespace if it's not already the case. * * @param string $source Namespace string * * @return string Namespaces with brackets */ public static function fixNamespaceDeclarations($source) { if (!function_exists('token_get_all') || !self::$useTokenizer) { if (preg_match('/(^|\\s)namespace(.*?)\\s*;/', $source)) { $source = preg_replace('/(^|\\s)namespace(.*?)\\s*;/', "\$1namespace\$2\n{", $source) . "}\n"; } return $source; } $rawChunk = ''; $output = ''; $inNamespace = false; $tokens = token_get_all($source); for ($i = 0; isset($tokens[$i]); ++$i) { $token = $tokens[$i]; if (!isset($token[1]) || 'b"' === $token) { $rawChunk .= $token; } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) { // strip comments continue; } elseif (T_NAMESPACE === $token[0]) { if ($inNamespace) { $rawChunk .= "}\n"; } $rawChunk .= $token[1]; // namespace name and whitespaces while (isset($tokens[++$i][1]) && in_array($tokens[$i][0], array(T_WHITESPACE, T_NS_SEPARATOR, T_STRING))) { $rawChunk .= $tokens[$i][1]; } if ('{' === $tokens[$i]) { $inNamespace = false; --$i; } else { $rawChunk = rtrim($rawChunk) . "\n{"; $inNamespace = true; } } elseif (T_START_HEREDOC === $token[0]) { $output .= self::compressCode($rawChunk) . $token[1]; do { $token = $tokens[++$i]; $output .= isset($token[1]) && 'b"' !== $token ? $token[1] : $token; } while ($token[0] !== T_END_HEREDOC); $output .= "\n"; $rawChunk = ''; } elseif (T_CONSTANT_ENCAPSED_STRING === $token[0]) { $output .= self::compressCode($rawChunk) . $token[1]; $rawChunk = ''; } else { $rawChunk .= $token[1]; } } if ($inNamespace) { $rawChunk .= "}\n"; } $output .= self::compressCode($rawChunk); if (PHP_VERSION_ID >= 70000) { // PHP 7 memory manager will not release after token_get_all(), see https://bugs.php.net/70098 unset($tokens, $rawChunk); gc_mem_caches(); } return $output; }
<?php var_dump(gc_mem_caches());