protected function guessClassName($file, $ignoreProject = false)
    {
        // 假设为根目录
        if ($file[0] === '.') {
            $file = '\\' . ltrim($file, './');
        }

        list($dir, $className) = explode('src/', $file);

        // TODO V2 处理V2的插件形式
        if ($dir == '\\') {
            $dir = '';
        }

        $composerJson = ($dir ? ($dir . '/') : '') . 'composer.json';
        if (!is_file($composerJson)) {
            throw new Exception(sprintf('Composer file "%s" not found', $composerJson));
        }

        $json = json_decode(file_get_contents($composerJson), true);
        if ($ignoreProject && isset($json['type']) && $json['type'] == 'project') {
            return false;
        }

        if (!isset($json['autoload']['psr-4']) || !$json['autoload']['psr-4']) {
            throw new Exception('Missing psr-4 autoload config');
        }

        $namespace = key($json['autoload']['psr-4']);
        $className = rtrim($namespace, '\\') . '\\' . $className;

        // 移除结尾的.php扩展名,并替换目录为命名空间分隔符
        return strtr(substr($className, 0, -4), ['/' => '\\']);
    }
protected function guessClassName($file, $ignoreProject = false)
    {
        // 假设为根目录
        if ($file[0] === '.') {
            $file = '\\' . ltrim($file, './');
        }

        list($dir, $className) = explode('src/', $file);

        // TODO V2 处理V2的插件形式
        if ($dir == '\\') {
            $dir = '';
        }

        $composerJson = ($dir ? ($dir . '/') : '') . 'composer.json';
        if (!is_file($composerJson)) {
            throw new Exception(sprintf('Composer file "%s" not found', $composerJson));
        }

        $json = json_decode(file_get_contents($composerJson), true);
        if ($ignoreProject && isset($json['type']) && $json['type'] == 'project') {
            return false;
        }

        if (!isset($json['autoload']['psr-4']) || !$json['autoload']['psr-4']) {
            throw new Exception('Missing psr-4 autoload config');
        }

        $namespace = key($json['autoload']['psr-4']);
        $className = rtrim($namespace, '\\') . '\\' . $className;

        // 移除结尾的.php扩展名,并替换目录为命名空间分隔符
        return strtr(substr($className, 0, -4), ['/' => '\\']);
    }
protected function guessClassName ( $ file , $ ignoreProject = false ) { // 假设为根目录 if ( $ file [ 0 ] === '.' ) { $ file = '\\' . ltrim ( $ file , './' ) ; } list ( $ dir , $ className ) = explode ( 'src/' , $ file ) ; // TODO V2 处理V2的插件形式 if ( $ dir == '\\' ) { $ dir = '' ; } $ composerJson = ( $ dir ? ( $ dir . '/' ) : '' ) . 'composer.json' ; if ( ! is_file ( $ composerJson ) ) { throw new Exception ( sprintf ( 'Composer file "%s" not found' , $ composerJson ) ) ; } $ json = json_decode ( file_get_contents ( $ composerJson ) , true ) ; if ( $ ignoreProject && isset ( $ json [ 'type' ] ) && $ json [ 'type' ] == 'project' ) { return false ; } if ( ! isset ( $ json [ 'autoload' ] [ 'psr-4' ] ) || ! $ json [ 'autoload' ] [ 'psr-4' ] ) { throw new Exception ( 'Missing psr-4 autoload config' ) ; } $ namespace = key ( $ json [ 'autoload' ] [ 'psr-4' ] ) ; $ className = rtrim ( $ namespace , '\\' ) . '\\' . $ className ; // 移除结尾的.php扩展名,并替换目录为命名空间分隔符 return strtr ( substr ( $ className , 0 , - 4 ) , [ '/' => '\\' ] ) ; }
Guest class name by file name

@param string $file
@param bool $ignoreProject
@return string
@throws Exception
Guest class name by file name