iiifx-production / yii2-autocomplete-helper
Yii2 Autocomplete Helper
Installs: 23 000
Dependents: 2
Suggesters: 0
Security: 0
Stars: 42
Watchers: 6
Forks: 5
Open Issues: 0
Type:extension
Requires
- php: ^8.0
- yiisoft/yii2: ^2.0.36
Requires (Dev)
- phpunit/phpunit: ^9
- roave/security-advisories: dev-latest
This package is not auto-updated.
Last update: 2024-11-11 23:41:41 UTC
README
Autocompletion generator for custom components in Yii2.
[English documentation] [Документация на русском]
By default in Yii2 not working autocompletion for custom components. IDE sees no added components and this causes inconvenience in operation.
This extension allows you to automatically generate a file with the autocomplete PHPDoc blocks with which the IDE will recognize all of the components in the application configuration.
Installation
Using Composer:
composer require "iiifx-production/yii2-autocomplete-helper"
Configuration
After installation, you need to one-time set up component to work.
For Yii2 Basic, in @app/config/console.php:
'bootstrap' => ['log', 'autocomplete'], 'components' => [ 'autocomplete' => [ 'class' => 'iiifx\Yii2\Autocomplete\Component', ], # ... ]
For Yii2 Advanced, in @console/config/main.php:
'bootstrap' => ['log', 'autocomplete'], 'components' => [ 'autocomplete' => [ 'class' => 'iiifx\Yii2\Autocomplete\Component', ], # ... ]
Using
To generate autocompletion in the console:
php yii ide-components
Generator automatically detects the type of application, read all configuration files and generate the autocomplete file to the application root.
Yii2 IDE auto-completion helper Vitaliy IIIFX Khomenko, 2019 Success: /home/iiifx/Projects/myProject/_ide_components.php
Important: For IDE did not swear on the two copies of the Yii class must be main Yii class file marked as a text document - example. The main class is located on the way: @vendor/yiisoft/yii2/Yii.php
Advanced customization
Sometimes the structure of the application differs from the standard and the need to change the generator behavior.
The following are examples of possible configuration options.
Changing the name of the component
If you need to change the name of a autocomplete to another, it is quite simple:
'bootstrap' => ['log', 'new-component-name'], # <-- new component name 'components' => [ 'new-component-name' => [ # <-- new component name 'class' => 'iiifx\Yii2\Autocomplete\Component', ], # ... ]
When the generator run in the console you need to pass the correct component name:
php yii ide-components --component=new-component-name
Changing environment
By default, a generator start is only possible for YII_ENV = "dev" environment.
You can change the environment:
'bootstrap' => ['log', 'autocomplete'], 'components' => [ 'autocomplete' => [ 'class' => 'iiifx\Yii2\Autocomplete\Component', 'environment' => 'local', # <-- environment ], # ... ]
Changing the generator controller
By default, the generator uses a console controller to create autocompletion.
You can replace the default controller, extend it, or add your own implementation:
'bootstrap' => ['log', 'autocomplete'], 'components' => [ 'autocomplete' => [ 'class' => 'iiifx\Yii2\Autocomplete\Component', 'controllerMap' => [ 'ide-components' => 'iiifx\Yii2\Autocomplete\Controller', # <-- default controller 'my-custom-generator' => 'path\to\your\custom\Controller', # <-- your controller ], ], # ... ]
Now you can run your controller:
php yii my-custom-generator
Link to the controller by default: source/Controller.php.
Changing the autocompletion file
By default, autocompletion file will be named _ide_components.php and will be placed in the application root.
You can change the name and location of the file:
'bootstrap' => ['log', 'autocomplete'], 'components' => [ 'autocomplete' => [ 'class' => 'iiifx\Yii2\Autocomplete\Component', 'result' => '@app/new-file-name.php' # <-- name and path ], # ... ]
The file path must be relative to aliases framework. Example: @common/../new-file-name.php.
Special configuration files
Sometimes you need to manually specify the application configuration files from which you want to generate autocompletion.
In this case, the generator will not seek configuration, the generator immediately uses this list.
For Yii2 Advanced:
'bootstrap' => ['log', 'autocomplete'], 'components' => [ 'autocomplete' => [ 'class' => 'iiifx\Yii2\Autocomplete\Component', 'config' => [ '@common/config/main.php', # <-- config list '@common/config/main-local.php', '@console/config/main.php', '@console/config/main-local.php', '@backend/config/main.php', '@backend/config/main-local.php', '@frontend/config/main.php', '@frontend/config/main-local.php', ], ], # ... ]
For Yii2 Basic:
'bootstrap' => ['log', 'autocomplete'], 'components' => [ 'autocomplete' => [ 'class' => 'iiifx\Yii2\Autocomplete\Component', 'config' => [ '@app/config/console.php', # <-- config list '@app/config/web.php', ], ], # ... ]
Configuration groups
In big projects sometimes need to be able to generate different autocomplete files depending on the stage of development.
You can group configuration files and generate autocompletion only for a specific group.
'bootstrap' => ['log', 'autocomplete'], 'components' => [ 'autocomplete' => [ 'class' => 'iiifx\Yii2\Autocomplete\Component', 'config' => [ 'frontend' => [ '@common/config/main.php', # <-- frontend group '@common/config/main-local.php', '@frontend/config/main.php', '@frontend/config/main-local.php', ], 'backend' => [ '@common/config/main.php', # <-- backend group '@common/config/main-local.php', '@backend/config/main.php', '@backend/config/main-local.php', ], 'api' => [ '@common/config/main.php', # <-- api group '@common/config/main-local.php', '@common/../api/config/main.php', '@common/../api/config/main-local.php', ], ], ], # ... ]
Now you can generate autocompletion for the desired group:
php yii ide-components --config=api
Configuring Application Classes
Some projects can sometimes use overridden application classes for web and console.
You can do this through the appropriate setting in the config.
'bootstrap' => ['log', 'autocomplete'], 'components' => [ 'autocomplete' => [ 'class' => 'iiifx\Yii2\Autocomplete\Component', 'webAppClass' => '\full\namespace\to\WebApplicationClass', 'consoleAppClass' => '\full\namespace\to\ConsoleApplicationClass', ], # ... ]