运行 使用自定义 CLI 命令的条令控制台
Run doctrine console using custom CLI command
我决定创建最小的 Silex 3、Doctrine2 堆栈,以便我可以制作简单的 REST API。不过我不确定如何处理学说控制台。
我想保持简单,所以在 project root
中我创建了名为 bin
的文件夹。我创建了简单的文件 console.php
,它应该 运行 各种 php 文件。
文件:bin\console.php
<?php
// When I run "php bin/console.php doctrine --version"
// The "doctrine" is name of the library that I want to use
// So that I can also do something like "php bin/console.php myLibrary --specialCommand"
$type = $argv[1];
// I want to remove the name of the library from the cli command though
unset($argv[1]);
// Update array indexes
$argv = array_values($argv);
// Choose file
if ($type == "doctrine") {
// Run doctrine
require_once __DIR__ . "/console/doctrine.php";
}
但我收到以下错误:
[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "doctrine" is not defined.
我知道这实际上是一种非常糟糕的做事方式,但是如果以后有人想知道答案,如果您想编辑 $argv
,您还必须更新 $argc
。
此代码按预期工作:
<?php
// When I run "php bin/console.php doctrine --version"
// The "doctrine" is name of the library that I want to use
// So that I can also do something like "php bin/console.php myLibrary --specialCommand"
$type = $argv[1];
// I want to remove the name of the library from the cli command though
unset($argv[1]);
// Update array indexes
$argv = array_values($argv);
$argc -= 1;
// Choose file
if ($type == "doctrine") {
// Run doctrine
require_once __DIR__ . "/console/doctrine.php";
}
这是我使用 Silex-Skeleton and Doctrine ORM Service Provider 的配置。
src/console.php
在此文件中,您需要添加 Doctrine CLI 命令。
<?php
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Console\Helper\HelperSet;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
$console = new Application('My Silex Application', 'n/a');
$console->getDefinition()->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev'));
$console->setDispatcher($app['dispatcher']);
$console
->register('cache:clear')
->setDescription('Clears the cache')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$cacheDir = $app['cache.path'];
$finder = Finder::create()->in($cacheDir)->notName('.gitignore');
$filesystem = new Filesystem();
$filesystem->remove($finder);
$output->writeln(sprintf("%s <info>success</info>", 'cache:clear'));
})
;
/*
* Doctrine CLI
*/
$helperSet = new HelperSet(array(
'db' => new ConnectionHelper($app['orm.em']->getConnection()),
'em' => new EntityManagerHelper($app['orm.em'])
));
$console->setHelperSet($helperSet);
Doctrine\ORM\Tools\Console\ConsoleRunner::addCommands($console);
return $console;
bin/console
#!/usr/bin/env php
<?php
require_once __DIR__.'/../vendor/autoload.php';
set_time_limit(0);
use Symfony\Component\Console\Input\ArgvInput;
$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$app = require __DIR__.'/../src/app.php';
require __DIR__.'/../config/'.$env.'.php';
$console = require __DIR__.'/../src/console.php';
$console->run();
src/app.php
<?php
use Silex\Application;
use Silex\Provider\AssetServiceProvider;
use Silex\Provider\TwigServiceProvider;
use Silex\Provider\ServiceControllerServiceProvider;
use Silex\Provider\HttpFragmentServiceProvider;
use Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProvider;
use Silex\Provider\DoctrineServiceProvider;
$app = new Application();
$app->register(new ServiceControllerServiceProvider());
$app->register(new AssetServiceProvider());
$app->register(new TwigServiceProvider());
$app->register(new HttpFragmentServiceProvider());
$app->register(new DoctrineServiceProvider());
$app->register(new DoctrineOrmServiceProvider);
$app['twig'] = $app->extend('twig', function ($twig, $app) {
// add custom globals, filters, tags, ...
return $twig;
});
// Doctrine DBAL
$app['db.options'] = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'xxxx',
'user' => 'xxxx',
'password' => 'xxxx',
'charset' => 'utf8',
'driverOptions' => array(1002 => 'SET NAMES utf8',),
);
// Doctrine ORM
$app["orm.em.options"] = array(
"mappings" => array(
array(
'type' => 'annotation',
"namespace" => "XYZ\Entity",
'path' => __DIR__ .'/XYZ/Entity',
"alias" => "AppBundle",
'use_simple_annotation_reader' => false
),
),
);
return $app;
composer.json
您需要添加一些依赖:
"symfony/console": "~2.8|3.0.*",
"doctrine/dbal": "~2.5.4",
"dflydev/doctrine-orm-service-provider": "^2.0",
最后执行控制台:
php bin/console
php bin/console orm:generate-entities src/
我决定创建最小的 Silex 3、Doctrine2 堆栈,以便我可以制作简单的 REST API。不过我不确定如何处理学说控制台。
我想保持简单,所以在 project root
中我创建了名为 bin
的文件夹。我创建了简单的文件 console.php
,它应该 运行 各种 php 文件。
文件:bin\console.php
<?php
// When I run "php bin/console.php doctrine --version"
// The "doctrine" is name of the library that I want to use
// So that I can also do something like "php bin/console.php myLibrary --specialCommand"
$type = $argv[1];
// I want to remove the name of the library from the cli command though
unset($argv[1]);
// Update array indexes
$argv = array_values($argv);
// Choose file
if ($type == "doctrine") {
// Run doctrine
require_once __DIR__ . "/console/doctrine.php";
}
但我收到以下错误:
[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "doctrine" is not defined.
我知道这实际上是一种非常糟糕的做事方式,但是如果以后有人想知道答案,如果您想编辑 $argv
,您还必须更新 $argc
。
此代码按预期工作:
<?php
// When I run "php bin/console.php doctrine --version"
// The "doctrine" is name of the library that I want to use
// So that I can also do something like "php bin/console.php myLibrary --specialCommand"
$type = $argv[1];
// I want to remove the name of the library from the cli command though
unset($argv[1]);
// Update array indexes
$argv = array_values($argv);
$argc -= 1;
// Choose file
if ($type == "doctrine") {
// Run doctrine
require_once __DIR__ . "/console/doctrine.php";
}
这是我使用 Silex-Skeleton and Doctrine ORM Service Provider 的配置。
src/console.php
在此文件中,您需要添加 Doctrine CLI 命令。
<?php
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Console\Helper\HelperSet;
use Doctrine\DBAL\Tools\Console\Helper\ConnectionHelper;
use Doctrine\ORM\Tools\Console\Helper\EntityManagerHelper;
use Doctrine\ORM\Tools\Console\ConsoleRunner;
$console = new Application('My Silex Application', 'n/a');
$console->getDefinition()->addOption(new InputOption('--env', '-e', InputOption::VALUE_REQUIRED, 'The Environment name.', 'dev'));
$console->setDispatcher($app['dispatcher']);
$console
->register('cache:clear')
->setDescription('Clears the cache')
->setCode(function (InputInterface $input, OutputInterface $output) use ($app) {
$cacheDir = $app['cache.path'];
$finder = Finder::create()->in($cacheDir)->notName('.gitignore');
$filesystem = new Filesystem();
$filesystem->remove($finder);
$output->writeln(sprintf("%s <info>success</info>", 'cache:clear'));
})
;
/*
* Doctrine CLI
*/
$helperSet = new HelperSet(array(
'db' => new ConnectionHelper($app['orm.em']->getConnection()),
'em' => new EntityManagerHelper($app['orm.em'])
));
$console->setHelperSet($helperSet);
Doctrine\ORM\Tools\Console\ConsoleRunner::addCommands($console);
return $console;
bin/console
#!/usr/bin/env php
<?php
require_once __DIR__.'/../vendor/autoload.php';
set_time_limit(0);
use Symfony\Component\Console\Input\ArgvInput;
$input = new ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$app = require __DIR__.'/../src/app.php';
require __DIR__.'/../config/'.$env.'.php';
$console = require __DIR__.'/../src/console.php';
$console->run();
src/app.php
<?php
use Silex\Application;
use Silex\Provider\AssetServiceProvider;
use Silex\Provider\TwigServiceProvider;
use Silex\Provider\ServiceControllerServiceProvider;
use Silex\Provider\HttpFragmentServiceProvider;
use Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProvider;
use Silex\Provider\DoctrineServiceProvider;
$app = new Application();
$app->register(new ServiceControllerServiceProvider());
$app->register(new AssetServiceProvider());
$app->register(new TwigServiceProvider());
$app->register(new HttpFragmentServiceProvider());
$app->register(new DoctrineServiceProvider());
$app->register(new DoctrineOrmServiceProvider);
$app['twig'] = $app->extend('twig', function ($twig, $app) {
// add custom globals, filters, tags, ...
return $twig;
});
// Doctrine DBAL
$app['db.options'] = array(
'driver' => 'pdo_mysql',
'host' => 'localhost',
'dbname' => 'xxxx',
'user' => 'xxxx',
'password' => 'xxxx',
'charset' => 'utf8',
'driverOptions' => array(1002 => 'SET NAMES utf8',),
);
// Doctrine ORM
$app["orm.em.options"] = array(
"mappings" => array(
array(
'type' => 'annotation',
"namespace" => "XYZ\Entity",
'path' => __DIR__ .'/XYZ/Entity',
"alias" => "AppBundle",
'use_simple_annotation_reader' => false
),
),
);
return $app;
composer.json
您需要添加一些依赖:
"symfony/console": "~2.8|3.0.*",
"doctrine/dbal": "~2.5.4",
"dflydev/doctrine-orm-service-provider": "^2.0",
最后执行控制台:
php bin/console
php bin/console orm:generate-entities src/