survos/command-bundle

通过网页界面运行 Symfony 控制台命令

资助包维护!
kbond

安装次数: 2,515

依赖项: 0

建议者: 0

安全性: 0

星标: 3

关注者: 3

分支: 0

公开问题: 0

类型:symfony-bundle

1.5.340 2024-09-03 14:36 UTC

This package is auto-updated.

Last update: 2024-09-24 13:45:06 UTC


README

通过网页界面运行 Symfony 命令行程序,以便更容易进行调试。

用途

当调试 Symfony 网页时,使用 assert()、dump() 和 dd() 是快速便捷的调试工具。但它们在使用控制台命令时通常很难使用。

例如,在官方 Symfony Demo 中,有一个命令可以将用户列表发送到电子邮件地址。

bin/console app:list-users --send-to=admin@example.com

使用 Symfony 的调试工具栏进行调试会更加容易,因此通过添加此包使其可用。

composer req survos/command-bundle

现在前往 /admin/commands,查看可用选项

img.png

选择 list-users,并填写电子邮件。

img_1.png

提交表单并打开调试工具栏

img_2.png

使用导出和断言,这会更加有用。

使用 Symfony Demo 的示例

symfony new --demo command-demo && cd command-demo
# bump to the latest version of Symfony 6.3, use whatever version of you have installed
git clone git@github.com:tacman/symfony-demo.git
sed -i 's/"php": "8.1.0"//' composer.json 
sed -i 's/"require": "6.3.*"/"require": "^6.4"/' composer.json
composer config minimum-stability dev
composer config extra.symfony.allow-contrib true
composer update 
composer req survos/command-bundle
bin/console --version

yarn install && yarn dev
symfony server:start -d
symfony open:local  --path admin/commands

使用新的 6.4 项目和 Bootstrap 的示例(无需构建步骤)

symfony new command-64 --webapp && cd command-64 
composer config minimum-stability dev
composer config extra.symfony.allow-contrib true
sed -i 's/"php": "8.1.0"//' composer.json 
composer req symfony/asset-mapper:^6.4
composer req symfony/stimulus-bundle:2.x-dev
bin/console make:controller -i AppController
symfony server:start -d
symfony open:local --path=/app
bin/console --version

composer req survos/command-bundle
bin/console make:command app:test
bin/console make:command app:import

# make it prettier with bootstrap, but not necessary
bin/console importmap:require bootstrap
echo "import 'bootstrap/dist/css/bootstrap.min.css'" >> assets/app.js

cat > config/packages/twig.yaml << END
twig:
    default_path: '%kernel.project_dir%/templates'
    form_themes:
        - 'bootstrap_5_layout.html.twig'
        - 'bootstrap_5_horizontal_layout.html.twig'

when@test:
    twig:
        strict_variables: true
END

symfony server:start -d
symfony open:local  --path admin/commands

使用 Symfony 7 的示例

使用新的 6.4 项目和 Bootstrap 的示例(无需构建步骤)

symfony new test-7 --version=next --php=8.2 && cd test-7
sed -i 's/"6.4.*"/"^7.0"/' composer.json
composer config minimum-stability dev
composer config prefer-stable false
composer config extra.symfony.allow-contrib true
composer update
composer config repositories.knp_menu_bundle '{"type": "vcs", "url": "git@github.com:tacman/KnpMenuBundle.git"}'
composer require knplabs/knp-menu-bundle
composer req survos/bootstrap-bundle
composer req symfony/maker-bundle symfony/debug-bundle --dev
bin/console make:controller -i AppController
symfony server:start -d
symfony open:local --path=/app

composer require --dev symfony/profiler-pack

composer req doctrine/orm:^2.16-dev doctrine/doctrine-bundle:^2.11-dev symfony/twig-bundle -w
composer require stof/doctrine-extensions-bundle:^1.8



composer req symfony/maker-bundle symfony/debug-bundle --dev
bin/console make:controller -i AppController
symfony server:start -d
symfony open:local --path=/app

echo "DATABASE_URL=sqlite:///%kernel.project_dir%/var/data.db" > .env.local
symfony new command-7 --webapp --version=next --php=8.2 && cd command-7
composer config repositories.knp_menu_bundle '{"type": "vcs", "url": "git@github.com:tacman/KnpMenuBundle.git"}'
composer require knplabs/knp-menu-bundle

composer req zenstruck/console-extra
composer req survos/command-bundle
composer req symfony/asset-mapper:^6.4
composer req symfony/stimulus-bundle:2.x-dev
bin/console make:controller -i AppController
symfony server:start -d
symfony open:local --path=/app
bin/console --version


Now go to /admin/commands

## Using Invokable Commands

I love zenstruck's extra-console, which allows defining the arguments and options via attributes, so you can create smaller console commands.  This bundle (command-bundle) already uses extra-console, so this works with no further installation.

Here's a command that lists the posts.

```bash
cat > src/Command/ListPostsCommand.php <<END
<?php

namespace App\Command;

use App\Entity\Post;
use App\Entity\Tag;
use App\Repository\PostRepository;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Style\StyleInterface;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Zenstruck\Console\Attribute\Option;
use Zenstruck\Console\ConfigureWithAttributes;
use Zenstruck\Console\InvokableServiceCommand;
use Zenstruck\Console\IO;
use Zenstruck\Console\RunsCommands;
use Zenstruck\Console\RunsProcesses;

#[AsCommand('app:list-posts', 'List the posts')]
final class ListPostsCommand extends InvokableServiceCommand
{

    use ConfigureWithAttributes;
    use RunsCommands;
    use RunsProcesses;


    public function __invoke(
        IO                        $io,
        PostRepository            $postRepository,
        PropertyAccessorInterface $accessor,
        StyleInterface $symfonyStyle,
        #[Option(description: 'Limit the number of posts')]
        int                       $limit = 50
    ): void
    {

        $headers = ['id', 'title', 'author', 'tags', 'comments'];
        $createUserArray = static function (Post $post) use ($headers, $accessor) {
            //
            return array_map(fn($header) => match ($header) {
                'author' => $post->getAuthor()->getFullName(),
                'tags' => join(',', $post->getTags()->map(fn(Tag $tag) => $tag->getName())->toArray()),
                'comments' => $post->getComments()->count(),
                default => $accessor->getValue($post, $header)
            }, $headers);
        };

        $criteria = [];
        $posts = array_map($createUserArray, $postRepository->findBy($criteria, [], $limit));
        $symfonyStyle->table($headers, $posts);
    }
}
END

使用 castor

symfony new castor-command-demo --webapp && cd castor-command-demo
sed -i "s|# MAILER_DSN|MAILER_DSN|" .env
bin/console make:command app:castor-test
cat > castor.php <<'END'
<?php

use Castor\Attribute\AsTask;

use function Castor\io;
use function Castor\capture;
use function Castor\import;

import(__DIR__ . '/src/Command/CastorTestCommand.php');

#[AsTask(description: 'Welcome to Castor!')]
function hello(): void
{
    $currentUser = capture('whoami');

    io()->title(sprintf('Hello %s!', $currentUser));
}
END

向 AppCastorTest.php 添加属性

#[\Castor\Attribute\AsSymfonyTask()]
castor list