{"id":21352575,"url":"https://github.com/ostark/yii2-artisan-bridge","last_synced_at":"2025-07-12T21:30:55.480Z","repository":{"id":57033558,"uuid":"129929716","full_name":"ostark/yii2-artisan-bridge","owner":"ostark","description":"Brings the ease of Artisan commands and the power of Symfony console to Craft3 and Yii2.","archived":false,"fork":false,"pushed_at":"2024-04-19T07:29:05.000Z","size":21,"stargazers_count":8,"open_issues_count":0,"forks_count":2,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-24T21:56:54.386Z","etag":null,"topics":["cli","console","craftcms","laravel","symfony","yii2"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ostark.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null}},"created_at":"2018-04-17T15:55:13.000Z","updated_at":"2024-10-14T13:28:13.000Z","dependencies_parsed_at":"2024-04-19T08:45:21.693Z","dependency_job_id":null,"html_url":"https://github.com/ostark/yii2-artisan-bridge","commit_stats":{"total_commits":17,"total_committers":1,"mean_commits":17.0,"dds":0.0,"last_synced_commit":"da8e75e0da4a9249cc77e785042f19ffa9c3a667"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/ostark/yii2-artisan-bridge","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ostark%2Fyii2-artisan-bridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ostark%2Fyii2-artisan-bridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ostark%2Fyii2-artisan-bridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ostark%2Fyii2-artisan-bridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ostark","download_url":"https://codeload.github.com/ostark/yii2-artisan-bridge/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ostark%2Fyii2-artisan-bridge/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263005069,"owners_count":23398336,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cli","console","craftcms","laravel","symfony","yii2"],"created_at":"2024-11-22T03:14:10.742Z","updated_at":"2025-07-12T21:30:55.236Z","avatar_url":"https://github.com/ostark.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Yii2 Artisan Bridge\n\nThis library brings the ease of Artisan commands and the power of Symfony console to Yii2 and Craft 5.\n\n## Install\n\nRequire the package:\n```\ncomposer require ostark/yii2-artisan-bridge\n```\n\n### Configure actions for a Craft 5 plugin\n```php\n\u003c?php namespace you\\PluginName;\n\nuse Craft;\nuse craft\\base\\Plugin as BasePlugin;\nuse ostark\\Yii2ArtisanBridge\\ActionGroup;\nuse ostark\\Yii2ArtisanBridge\\Bridge;\nuse you\\PluginName\\actions\\ActionOne;\nuse you\\PluginName\\actions\\ActionTwo;\n\n/**\n * Class Plugin\n */\nclass Plugin extends BasePlugin\n{\n    /**\n     * Initialize Plugins\n     */\n    public function init()\n    {\n        parent::init();\n\n        if (Craft::$app instanceof \\yii\\console\\Application) {\n\n            $group = (new ActionGroup('group-name', 'What this group is about.'))\n                -\u003esetActions([\n                    'action1'  =\u003e ActionOne::class,\n                    'action2'  =\u003e ActionTwo::class,\n                ])\n                -\u003esetDefaultAction('action1')\n                -\u003esetOptions([\n                    'one' =\u003e 'option-one',\n                    'two' =\u003e 'option-two',\n                    'option-without-alias'\n                ]);\n            \n            Bridge::registerGroup($group);\n        }\n    }\n}\n\n```\n\n### Write your Actions (Commands)\n\nYou write one class per action. Your actual instructions live in the `run()` method, similar to \n`execute()` in Symfony or `handle()` in Laravel. Command arguments map to the arguments of the `run()` method.\n\nOptions and option aliases are registered in Commands::register($prefix, $actions, `$options`). To access an option, \nit must be declared as a public property in the Action class. \n\n```php\n\u003c?php namespace you\\PluginName\\actions;\n\nuse Craft;\nuse ostark\\Yii2ArtisanBridge\\base\\Action as BaseAction;\nuse yii\\console\\ExitCode;\n\nclass ActionOne extends BaseAction {\n\n    public $optionOne = 'default-value';\n    \n    /**\n      * Ask some question\n      *\n      * @param string $name\n      * @return bool\n      */\n    public function run($name) {\n    \n        $this-\u003etitle(\"Hello {$name}, 'option-one' is '{$this-\u003eoptionOne}'\");\n        \n        $answer = $this-\u003echoice(\"What's your favorite animal?\", ['Dog','Cat','Elephant']);\n        \n        if ($answer === 'Elephant') {\n            $this-\u003esuccessBlock(\"'$answer' is correct.\");\n            return ExitCode::OK;\n        } else {\n            $this-\u003eerrorBlock(\"'$answer' is the wrong.\");\n            return ExitCode::UNSPECIFIED_ERROR;\n        }\n    \n    }\n}   \n```\n\n### Artisan helper methods\n\n**Prompting for input**\n\n```php\n$name = $this-\u003eask('What is your name?', $default = null)`\n```\n\n```php\n$name = $this-\u003eanticipate('What is your name?', ['Taylor', 'Fabien', 'Brad', 'Brandon']);\n```\n\n```php\nif ($this-\u003econfirm('Do you wish to continue?')) {\n    // continue\n}\n```\n\n\n**Writing output**\n\n```php\n$this-\u003einfo('Display this on the screen');\n$this-\u003eerror('Something went wrong!');\n```\n\n```php\n$headers = ['Name', 'Email'];\n$rows    = [['First name', 'First email'], ['Second name', 'Second email']];\n\n$this-\u003etable($headers, $rows);\n```\n\n\n\n### Symfony block style\n\n```php\n$this-\u003etitle('Title style block');\n$this-\u003esection('Section style block');\n$this-\u003elisting(['One','Two','Three'];\n\n$this-\u003esuccessBlock('Yeah!');\n$this-\u003eerrorBlock('Oh no!');\n\n// Custom blocks\n$this-\u003eblock($messages, $type = null, $style = null, $prefix = ' ', $padding = true, $escape = true);\n\n```\n\n\n### Symfony progress bar\n\n```php\n$items = range(1,10);\n\n$bar = $this-\u003eoutput-\u003ecreateProgressBar(count($items));\n\n// Custom format\n$bar-\u003esetFormat('%message%' . PHP_EOL . '%bar% %percent:3s% %' . PHP_EOL . 'time:  %elapsed:6s%/%estimated:-6s%' . PHP_EOL.PHP_EOL);\n$bar-\u003esetBarCharacter('\u003cinfo\u003e'.$bar-\u003egetBarCharacter().'\u003c/info\u003e');\n$bar-\u003esetBarWidth(80);\n\nforeach ($items as $i) {\n    sleep(1);\n    $bar-\u003eadvance();\n    $bar-\u003esetMessage(\"My bar, some progress... $i\");\n}\n\n$bar-\u003efinish();\n```\n\n## Custom formatter\n\nRegister a custom OutputFormatterStyle \n```php\n// in your init()\n\\yii\\base\\Event::on(\n    Commands::class,\n    Commands::EVENT_BEFORE_ACTION,\n    function (ActionEvent $event) {\n        $style = new OutputFormatterStyle('white', 'cyan');\n        $event-\u003eaction-\u003eoutput-\u003egetFormatter()-\u003esetStyle('ocean', $style);\n    }\n);\n```\n\nApply the style\n```php\n$this-\u003etitle('Title in \u003cocean\u003eblue\u003c/ocean\u003e');\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fostark%2Fyii2-artisan-bridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fostark%2Fyii2-artisan-bridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fostark%2Fyii2-artisan-bridge/lists"}