[李景山php]每天laravel-20160909|Dispatcher-9

來源:互聯網
上載者:User

標籤:interface   function   classes   return   public   

namespace Illuminate\Filesystem;use Symfony\Component\Finder\Finder;// use Finderclass ClassFinder{    /**     * Find all the class and interface names in a given directory.     *     * @param  string  $directory     * @return array     */    public function findClasses($directory)    {// find all class and interface name maybe is too exaggerated        $classes = [];// class listener        foreach (Finder::create()->in($directory)->name(‘*.php‘) as $file) {            $classes[] = $this->findClass($file->getRealPath());        }// check all of this .php files name, if has .class.php ,        return array_filter($classes);// if only on parameter ,then they will delete this value that it is null.    }    /**     * Extract the class name from the file at the given path.     *     * @param  string  $path     * @return string|null     */    public function findClass($path)    {// extract  means get something from other thing.       // like get class name by file path,        $namespace = null;// set a null namespace        $tokens = token_get_all(file_get_contents($path));// get all string like a php code        foreach ($tokens as $key => $token) {            if ($this->tokenIsNamespace($token)) {// if has namespace                $namespace = $this->getNamespace($key + 2, $tokens);// get it            } elseif ($this->tokenIsClassOrInterface($token)) {// if this class is a interface                return ltrim($namespace.‘\\‘.$this->getClass($key + 2, $tokens), ‘\\‘);// use it self to make ltrim            }        }    }    /**     * Find the namespace in the tokens starting at a given key.     *     * @param  int  $key     * @param  array  $tokens     * @return string|null     */    protected function getNamespace($key, array $tokens)    {// Find the namespace in tokens starting at a given key.        $namespace = null;// a namespace        $tokenCount = count($tokens);// get this array count        for ($i = $key; $i < $tokenCount; $i++) {// if has tokens            if ($this->isPartOfNamespace($tokens[$i])) {// if this is a  namespace ,even it is a array                $namespace .= $tokens[$i][1];//            } elseif ($tokens[$i] == ‘;‘) {// if                return $namespace;// null return            }        }    }    /**     * Find the class in the tokens starting at a given key.     *     * @param  int  $key     * @param  array  $tokens     * @return string|null     */    protected function getClass($key, array $tokens)    {        $class = null;        $tokenCount = count($tokens);        for ($i = $key; $i < $tokenCount; $i++) {            if ($this->isPartOfClass($tokens[$i])) {                $class .= $tokens[$i][1];            } elseif ($this->isWhitespace($tokens[$i])) {                return $class;            }        }    }// same too    /**     * Determine if the given token is a namespace keyword.     *     * @param  array|string  $token     * @return bool     */    protected function tokenIsNamespace($token)    {        return is_array($token) && $token[0] == T_NAMESPACE;// is a array then check this token[0] is a T_NAMESPACE    }// determine given token is a namespace keyword    /**     * Determine if the given token is a class or interface keyword.     *     * @param  array|string  $token     * @return bool     */    protected function tokenIsClassOrInterface($token)    {        return is_array($token) && ($token[0] == T_CLASS || $token[0] == T_INTERFACE);    }//Determine if the given token is a class or interface keyword.   // first this token is array, and the first is a T_CLASS    /**     * Determine if the given token is part of the namespace.     *     * @param  array|string  $token     * @return bool     */    protected function isPartOfNamespace($token)    {        return is_array($token) && ($token[0] == T_STRING || $token[0] == T_NS_SEPARATOR);    }// check given token is part of the namesapce.    /**     * Determine if the given token is part of the class.     *     * @param  array|string  $token     * @return bool     */    protected function isPartOfClass($token)    {        return is_array($token) && $token[0] == T_STRING;    }// check class    /**     * Determine if the given token is whitespace.     *     * @param  array|string  $token     * @return bool     */    protected function isWhitespace($token)    {        return is_array($token) && $token[0] == T_WHITESPACE;    }// check is Whitespace}


本文出自 “專註php” 部落格,請務必保留此出處http://jingshanls.blog.51cto.com/3357095/1795466

[李景山php]每天laravel-20160909|Dispatcher-9

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.