yiisoft / yii-core

Yii Framework 3.0 core
https://www.yiiframework.com/
433 stars 75 forks source link

Separate the part of Composer Autoload Psr from `aliases` #98

Closed kids-return closed 5 years ago

kids-return commented 5 years ago

According to the definition of the alias for the document, the alias should only be used to handle Paths and URLs. The value of the alias can only be a string See: https://www.yiiframework.com/doc/guide/2.0/en/concept-aliases

And Composer Autoload Psr map can be an array After separation, we will be able to implement more useful features and comply with Psr E.g: Supports multiple directories with namespace, which can be used for migration and base template combination. Of course there are many See: https://github.com/yiisoft/db/issues/29#issuecomment-440586266

I think can add an PsrAliases.php file. You can use it like the alias component. The property is read-only, and the content inside is completely generated according to composer

The original alias is configured by config.

    'aliases' => [
        '__class'   => yii\base\Aliases::class,
        '@root'     => YII_ROOT,
        '@vendor'   => '@root/vendor',
        '@public'   => '@root/public',
        '@runtime'  => '@root/runtime',
        '@bower'    => '@vendor/bower-asset',
        '@npm'      => '@vendor/npm-asset',
        '@yii'      => '@vendor/yiisoft/yii-core/src',
        '@yiisoft'  => '@vendor/yiisoft',
    ],

Because aliases are basically only used for paths, it will be more intuitive.

public $sourcePath = '@yiisoft/yii-jquery/src/assets';
or
public $sourcePath = '@vendor/yiisoft/yii-jquery/src/assets';

Users can configure custom aliases based on their own needs. I am working on the relevant changes, and if I allow me to perfect it. And submit PR. Is there a good suggestion if you name it?

schmunk42 commented 5 years ago

The autoloading feature is very useful when developing extensions meant for vendor in another folder or when creating extensions.

kids-return commented 5 years ago

Forgive me for not understanding, can you tell me your opinion? 🥺 or 👍 👎

hiqsol commented 5 years ago

@kids-return Sorry, I didn't understand. Can you please give more explanation of your idea.

schmunk42 commented 5 years ago

@kids-return There's no simply yes or no from my side. I was just pointing out a useful feature at the moment, which should be kept, if there's no better alternative. It looks like your proposal touches that part.

kids-return commented 5 years ago

before

            'aliases' => yii\base\Aliases#9
            (
                [yii\base\Aliases:aliases] => [
                    '@yii' => [
                        '@yii/widgets' => '/data/wwwroot/yii3/vendor/yiisoft/view/src/widgets'
                        '@yii/web/tests' => '/data/wwwroot/yii3/vendor/yiisoft/yii-web/tests'
                        '@yii/web' => '/data/wwwroot/yii3/vendor/yiisoft/yii-web/src'
                        '@yii/view' => '/data/wwwroot/yii3/vendor/yiisoft/view/src/view'
                        '@yii/tests' => '/data/wwwroot/yii3/vendor/yiisoft/yii-core/tests'
                        '@yii/swiftmailer' => '/data/wwwroot/yii3/vendor/yiisoft/yii-swiftmailer/src'
                        '@yii/rest' => '/data/wwwroot/yii3/vendor/yiisoft/yii-rest/src'
                        '@yii/log' => '/data/wwwroot/yii3/vendor/yiisoft/log/src'
                        '@yii/jquery' => '/data/wwwroot/yii3/vendor/yiisoft/yii-jquery/src'
                        '@yii/di' => '/data/wwwroot/yii3/vendor/yiisoft/di/src'
                        '@yii/db/tests' => '/data/wwwroot/yii3/vendor/yiisoft/db/tests'
                        '@yii/db' => '/data/wwwroot/yii3/vendor/yiisoft/db/src'
                        '@yii/console' => '/data/wwwroot/yii3/vendor/yiisoft/yii-console/src'
                        '@yii/captcha' => '/data/wwwroot/yii3/vendor/yiisoft/yii-captcha/src'
                        '@yii/cache/tests' => '/data/wwwroot/yii3/vendor/yiisoft/cache/tests'
                        '@yii/cache' => '/data/wwwroot/yii3/vendor/yiisoft/cache/src'
                        '@yii/bootstrap4' => '/data/wwwroot/yii3/vendor/yiisoft/yii-bootstrap4/src'
                        '@yii/app' => '/data/wwwroot/yii3/vendor/yiisoft/yii-base-cli/src'
                        '@yii/activerecord/tests' => '/data/wwwroot/yii3/vendor/yiisoft/active-record/tests'
                        '@yii/activerecord' => '/data/wwwroot/yii3/vendor/yiisoft/active-record/src'
                        '@yii' => '/data/wwwroot/yii3/vendor/yiisoft/yii-core/src'
                    ]
                    '@Psr' => [
                        '@Psr/SimpleCache' => '/data/wwwroot/yii3/vendor/psr/simple-cache/src'
                        '@Psr/Log' => '/data/wwwroot/yii3/vendor/psr/log/Psr/Log'
                        '@Psr/Http/Message' => '/data/wwwroot/yii3/vendor/psr/http-message/src'
                        '@Psr/Container' => '/data/wwwroot/yii3/vendor/psr/container/src'
                    ]
                    '@Doctrine' => [
                        '@Doctrine/Common/Lexer' => '/data/wwwroot/yii3/vendor/doctrine/lexer/lib/Doctrine/Common/Lexer'
                    ]
                    '@Egulias' => [
                        '@Egulias/EmailValidator' => '/data/wwwroot/yii3/vendor/egulias/email-validator/EmailValidator'
                    ]
                    '@Dotenv' => '/data/wwwroot/yii3/vendor/vlucas/phpdotenv/src'
                    '@hiqdev' => [
                        '@hiqdev/composer/config' => '/data/wwwroot/yii3/vendor/hiqdev/composer-config-plugin/src'
                    ]
                    '@root' => '/data/wwwroot/yii3'
                    '@vendor' => '/data/wwwroot/yii3/vendor'
                    '@public' => '/data/wwwroot/yii3/public'
                    '@runtime' => '/data/wwwroot/yii3/runtime'
                    '@bower' => '/data/wwwroot/yii3/vendor/bower-asset'
                    '@npm' => '/data/wwwroot/yii3/vendor/npm-asset'
                    '@app' => '/data/wwwroot/yii3/vendor/yiisoft/yii-base-cli/src'
                    '@web' => ''
                ]
            )

after

            'aliases' => yii\base\Aliases#9
            (
                [yii\base\Aliases:aliases] => [
                    '@root' => '/data/wwwroot/yii3'
                    '@vendor' => '@root/vendor'
                    '@public' => '@root/public'
                    '@runtime' => '@root/runtime'
                    '@bower' => '@vendor/bower-asset'
                    '@npm' => '@vendor/npm-asset'
                    '@app' => '/data/wwwroot/yii3/vendor/yiisoft/yii-base-cli/src'
                    '@web' => '/'
                    '@yii' => '/data/wwwroot/yii3/vendor/yiisoft/yii-core/src'
                    '@yiisoft' => '/data/wwwroot/yii3/vendor/yiisoft'
                    '@vendor2' => '/data/wwwroot/yii3/vendor2/yiisoft'
                ]
            )

            'psrAliases' => yii\base\PsrAliases#9
            (
                [yii\base\Aliases:aliases] => [
                    '@yii' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-core/src'
                    ]
                    '@yii/tests' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-core/tests'
                    ]
                    '@yii/rest' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-rest/src'
                    ]
                    '@yii/view' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/view/src/view'
                    ]
                    '@yii/widgets' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/view/src/widgets'
                    ]
                    '@Psr/Http/Message' => [
                        0 => '/data/wwwroot/yii3/vendor/psr/http-message/src'
                    ]
                    '@yii/web' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-web/src'
                    ]
                    '@yii/web/tests' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-web/tests'
                    ]
                    '@yii/console' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-console/src'
                    ]
                    '@Psr/Container' => [
                        0 => '/data/wwwroot/yii3/vendor/psr/container/src'
                    ]
                    '@yii/di' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/di/src'
                    ]
                    '@Psr/Log' => [
                        0 => '/data/wwwroot/yii3/vendor/psr/log/Psr/Log'
                    ]
                    '@yii/log' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/log/src'
                    ]
                    '@Psr/SimpleCache' => [
                        0 => '/data/wwwroot/yii3/vendor/psr/simple-cache/src'
                    ]
                    '@yii/cache' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/cache/src'
                    ]
                    '@yii/cache/tests' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/cache/tests'
                    ]
                    '@yii/db' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/db/src'
                    ]
                    '@yii/db/tests' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/db/tests'
                    ]
                    '@yii/activerecord' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/active-record/src'
                    ]
                    '@yii/activerecord/tests' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/active-record/tests'
                    ]
                    '@yii/jquery' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-jquery/src'
                    ]
                    '@yii/captcha' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-captcha/src'
                    ]
                    '@yii/bootstrap4' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-bootstrap4/src'
                    ]
                    '@Doctrine/Common/Lexer' => [
                        0 => '/data/wwwroot/yii3/vendor/doctrine/lexer/lib/Doctrine/Common/Lexer'
                    ]
                    '@Egulias/EmailValidator' => [
                        0 => '/data/wwwroot/yii3/vendor/egulias/email-validator/EmailValidator'
                    ]
                    '@yii/swiftmailer' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-swiftmailer/src'
                    ]
                    '@Dotenv' => [
                        0 => '/data/wwwroot/yii3/vendor/vlucas/phpdotenv/src'
                    ]
                    '@hiqdev/composer/config' => [
                        0 => '/data/wwwroot/yii3/vendor/hiqdev/composer-config-plugin/src'
                    ]
                    '@yii/app' => [
                        0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-base-web/src'
                        1 => '/data/wwwroot/yii3/vendor/yiisoft/yii-base-cli/src'
                    ]
                ]
            )
'@yii/app' => [
   0 => '/data/wwwroot/yii3/vendor/yiisoft/yii-base-web/src'
   1 => '/data/wwwroot/yii3/vendor/yiisoft/yii-base-cli/src'
]

modify This behavior is wrong

'@yii/app' => '/data/wwwroot/yii3/vendor/yiisoft/yii-base-cli/src'

composer support! what if the user needs to use multiple directories?

kids-return commented 5 years ago

old

yii migrate/up --migration-path=@leonsw/wallet/migrations,@leonsw/user/migrations,@leonsw/post/migrations,@leonsw/tag/migrations

now

yii migrate/up --migration-path=@leonsw/migrations

use $psrAliases->get('@leonsw/migrations') Can get multiple directories I think this is the right thing.

Aliases are used to represent file paths or URLs so that you don't have to hard-code absolute paths or URLs in your project. An alias must start with the @ character to be differentiated from normal file paths and URLs. Alias defined without leading @ will be prefixed with @ character.

I suggest you look at the above again.

kids-return commented 5 years ago

i18n

$psrAliases->get('@leonsw/messages')
samdark commented 5 years ago

Done.