sebwite/idea-meta

一个简单易用、可扩展的PhpStorm高级元数据生成器,用于Laravel。类似于ide-helper:meta,支持额外的自定义生成器。还包括路由和配置

1.1.1 2016-06-25 12:27 UTC

This package is auto-updated.

Last update: 2024-09-13 03:52:24 UTC


README

sebwite/idea-meta 是Laravel 5框架的一个包。

该包遵循FIG标准PSR-1、PSR-2和PSR-4,以确保共享PHP代码之间的高互操作性。

简介

生成 .phpstorm.meta.php,类似于 barryvdh/laravel-ide-helper,但允许开发者轻松添加自己的生成器。目前支持Laravel 5

  • 应用绑定
  • 配置
  • 路由

快速安装

首先通过Composer安装包。

composer require sebwite/idea-meta

添加服务提供者

Sebwite\IdeaMeta\IdeaMetaServiceProvider::class

生成元数据文件

php artisan idea-meta

大功告成!

覆盖或添加生成器

所有 Meta 生成器都应该扩展 Sebwite\IdeaMeta\Metas\BaseMeta 抽象类。Meta 生成器可以通过例如使用绑定添加到 MetaRepository,例如:app('idea-meta')->add(App\MyCustomMeta::class)

这里有一个快速示例

class ConfigMeta extends BaseMeta
{
    protected $methods = [
        'config(\'\')',
        '\\Config::get(\'\')',
        'new \Illuminate\Contracts\Config\Repository',
        '\Illuminate\Contracts\Config\Repository::get(\'\')'
    ];

    public function getData()
    {
        return array_dot($this->app['config']->all());
    }
}
class ConfigMetaServiceProvider extends ServiceProvider {
    public function boot(){
        $this->app['idea-meta']->add(ConfigMeta::class);
    }
}

就这样!你现在可以为所有的 config 命令获得代码补全。

一些其他示例

路由
class RoutesMeta extends BaseMeta
{
    protected $methods = [
        'route(\'\')',
        '\Illuminate\Routing\RouteCollection::getByName(\'\')',
        '\\URL::route(\'\')',
        'new \Illuminate\Contracts\Routing\UrlGenerator',
        '\Illuminate\Contracts\Routing\UrlGenerator::route(\'\')',
    ];

    public function getData()
    {
        $routes = [ ];
        /** @var \Illuminate\Routing\Route[] */
        $_routes = $this->app[ 'router' ]->getRoutes()->getRoutes();
        foreach ($_routes as $route) {
            $routes[ $route->getName() ] = false; //$route->getUri();
        }
        return $routes;
    }
}
Codex
class CodexMeta extends BaseMeta
{
    protected $methods = [
        'new \Codex\Core\Codex',
        'codex(\'\')',
    ];

    public function getData()
    {
        return app('Codex\Core\Contracts\Codex')->getExtenableProperty('extensions');
    }

    public static function canRun()
    {
        return class_exists('Codex\Core\Contracts\Codex', false);
    }
}
Bitbucket
class BitbucketMeta extends BaseMeta
{
    protected $template = <<<'EOF'
@foreach($methods as $method)
    {!! $method !!} => [
        '' == '@',
        @foreach($data as $k => $v)
            '{!! $k !!}' instanceof {!! \Sebwite\Support\Str::ensureLeft($v, '\\') !!},
        @endforeach
    ],
@endforeach
EOF;

    protected $methods = [
        'new \Bitbucket\API\Api',
        '\Bitbucket\API\Api::api(\'\')',
    ];


    public function getData()
    {
        $classes = [];
        $apiClasses = [ 'GroupPrivileges', 'Groups',  'Invitations', 'Privileges', 'Repositories', 'Teams', 'User', 'Users' ];
        $apiDir = base_path(Path::join('vendor', 'gentle', 'bitbucket-api', 'lib', 'Bitbucket', 'API'));
        foreach (['Repositories', 'User', 'Users', 'Groups'] as $dir) {
            $files = array_merge(
                glob(Path::join($apiDir, $dir, '*.php')),
                glob(Path::join($apiDir, $dir, '*/*.php'))
            );
            foreach ($files as $filePath) {
                $ext = Path::getExtension($filePath);
                $rel = Path::makeRelative($filePath, $apiDir);
                $res = Str::removeRight($rel, '.' . $ext);
                $apiClasses[] = Str::replace($res, '/', '\\');
            }
        }



        #['BranchRestrictions', 'Changesets', 'Commits', 'Deploykeys']
        foreach ($apiClasses as $apiClass) {
            $classes[$apiClass] = 'Bitbucket\\API\\' . $apiClass;
        }
        return $classes;
    }

    public static function canRun()
    {
        return class_exists('Bitbucket\API\Api', false);
    }
}