DukeAnn
做一个优雅的程序员

yii2-cookbook之增强IDE自动补全[翻译]

原文地址: https://github.com/samdark/yii2-cookbook/blob/master/book/ide-autocompletion.md

由于能够带来非常舒服的开发体验,使用IDE在猴子中非常普遍.它能提示拼写和语法错误,以及提供编码建议,当然还有大家最喜欢的代码自动补全.总的来说,YII2.0对代码自动补全的机制支持的还是比较好的,但是对于一些自定义的components比如Yii::$app->mycomponent->something, IDE的自动补全还无能为力.

使用自定义的Yii class

最好的办法是在自己的项目文件中给IDE一点提示,首先看下项目中默认的index.php:

<?php

// comment out the following two lines when deployed to production
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', 'dev');

require(__DIR__ . '/../vendor/autoload.php');
require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

$config = require(__DIR__ . '/../config/web.php');

(new yii\web\Application($config))->run();

其中require(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');这行可以用我们自定义Yii.php代替(注:作者的意思可能是把vendor/yiisoft/yii2/Yii.php这个文件替换掉,但我觉得不推荐直接改YII2的源代码,因为万一以后框架升级Yii.php可能就被覆盖掉了.我推荐requir一个app目录下的自己写的Yii.php,然后下面代码中的文件路径也要改写一下).自定义的Yii.php的内容如下:

<?php
/**
 * Yii bootstrap file.
 * Used for enhanced IDE code autocompletion.
 */
class Yii extends \yii\BaseYii
{
    /**
     * @var BaseApplication|WebApplication|ConsoleApplication the application instance
     */
    public static $app;
}

spl_autoload_register(['Yii', 'autoload'], true, true);
Yii::$classMap = include(__DIR__ . '/vendor/yiisoft/yii2/classes.php');
Yii::$container = new yii\di\Container;

/**
 * Class BaseApplication
 * Used for properties that are identical for both WebApplication and ConsoleApplication
 *
 * @property \app\components\RbacManager $authManager The auth manager for this application. Null is returned if auth manager is not configured. This property is read-only. Extended component.
 * @property \app\components\Mailer $mailer The mailer component. This property is read-only. Extended component.
 */
abstract class BaseApplication extends yii\base\Application
{
}

/**
 * Class WebApplication
 * Include only Web application related components here
 *
 * @property \app\components\User $user The user component. This property is read-only. Extended component.
 * @property \app\components\MyResponse $response The response component. This property is read-only. Extended component.
 * @property \app\components\ErrorHandler $errorHandler The error handler application component. This property is read-only. Extended component.
 */
class WebApplication extends yii\web\Application
{
}

/**
 * Class ConsoleApplication
 * Include only Console application related components here
 *
 * @property \app\components\ConsoleUser $user The user component. This property is read-only. Extended component.
 */
class ConsoleApplication extends yii\console\Application
{
}

在上面的PHPdoc中的BaseApplication, WebApplication, ConsoleApplication就被加入IDE的代码自动补全中了.你自定义的component可以写在注释的@property中.

注意: 为避免多重启动(原文Multiple Implementations),PHPStorm会将vendor/yiisoft/yii2/Yii.php中的内容标记为警告或者是无格式文本(注:这里不太懂,PHPStorm没用过,请用过的同学补充)

定制自己的component

举个例子,要定制自己的Yii::$app->user->identity,那么app\components\User要这么写:

<?php

namespace app\components;

use Yii;

/**
 * @inheritdoc
 *
 * @property \app\models\User|\yii\web\IdentityInterface|null $identity The identity object associated with the currently logged-in user. null is returned if the user is not logged in (not authenticated).
 */
class User extends \yii\web\User
{
}

最后,配置文件里要这么写:

return [
    ...
    'components' => [
        /**
         * User
         */
        'user' => [
            'class' => 'app\components\User',
            'identityClass' => 'app\models\User',
        ],
        /**
         * Custom Component
         */
        'response' => [
            'class' => 'app\components\MyResponse',
        ],
    ],
];
DukeAnn的笔记本:DukeAnn的博客 » yii2-cookbook之增强IDE自动补全[翻译]
分享到: 更多 (0)

评论 1

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
  1. #1

    <?php// comment out the following two lines when deployed to productiondefined(‘YII_DEBUG’) or define(‘YII_DEBUG’, true);defined(‘YII_ENV’) or define(‘YII_ENV’, ‘dev’);require(__DIR__ . ‘/../vendor/autoload.php’);//require(__DIR__ . ‘/../vendor/yiisoft/yii2/Yii.php’);require(__DIR__ . ‘/../ide_helper.php’);$config = require(__DIR__ . ‘/../config/web.php’);(new yiiwebApplication($config))->run();

    ADKi3年前 (2016-05-24)回复