I have a small console command where I'd like to read some environment variables, but it does not seem to read the vars from the .env file or the server configs in the console (php file works)
The code
protected function execute(InputInterface $input, OutputInterface $output)
{
$io = new SymfonyStyle($input, $output);
$debug = $input->getArgument('debug') === 'y' ? true : false;
$this->project = $input->getArgument('project');
$start = new DateTime();
$debug ? $io->text('<fg=green>Starting upload</>') : null;
dump(getenv('APP_ENV'));
dump(getenv('MAILER_USERNAME'));
die;
...
}
Commands to test
php bin/console app:make-backup
Output:
Starting upload
false
false
php bin/console app:make-backup --env=prod
Output:
Starting upload
"prod"
false
php bin/console app:make-backup --env=dev
Output:
Starting upload
"dev"
false
.env File
APP_ENV=dev
MAILER_USERNAME=info#xxx.com
I don't see where I am doing wrong? Issue exists on nginx and apache server, BUT using getenv('MAILER_USERNAME') in any php-file works.
I think your best bet would be to go ahead and inject the env variables. Trying to access them directly can be a bit tricky.
# config/services.yaml
services:
App\Command\MyCommand:
$appEnv: '%env(APP_ENV)%'
$mailerUsername: '%env(MAILER_USERNAME)%'
# src\Command\MyCommand
class MyCommand extends Command {
protected static $defaultName = 'app:mine';
private $appEnv;
private $mailerUsername;
public function __construct($appEnv,$mailerUsername) {
parent::__construct();
$this->appEnv = $appEnv;
$this->mailerUsername = $mailerUsername;
}
protected function execute(InputInterface $input, OutputInterface $output) {
$output->writeln(("My Command " . $this->appEnv . " " . $this->mailerUsername));
}
}
Related
I currently have a symfony project that uses Foundation for Emails to create responsive emails.
The Foundation framework uses the command 'npm run build' to tranform files. I tried doing a service parse my content using the Process class but I must be using it wrong as it does not execute 'npm run build'. Here is my faulty code :
<?php
/**
* Created by PhpStorm.
* User: jeremie
* Date: 28/12/17
* Time: 16:59
*/
namespace Acme\Open4XMLParserBundle\Services;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Process\Process;
/**
* #todo : code this
*/
class FoundationParser
{
protected $foundationLocation;
protected $process;
/**
* FoundationParser constructor.
* #param $foundationLocation
*/
public function __construct($foundationLocation)
{
$this->foundationLocation = $foundationLocation;
$this->process = new Process('npm run build', $this->foundationLocation);
}
/**
* Run npm run build if needed
*/
public function initFoundation()
{
//make sure that 'npm run build' is running and execute it if not
if (!$this->process->isRunning()) {
$this->process->start();
}
}
public function saveFoundationContent($foundationContent, $filename)
{
//save the file in foundation/src/pages
$fileSystem = new Filesystem();
$fileLocation = $this->foundationLocation . '/src/pages/' . $filename;
if (!$fileSystem->exists($fileLocation)) {
$fileSystem->dumpFile($fileLocation, $foundationContent);
$fileSystem->chmod($fileLocation, 0664);
}
}
public function retrieveValidHtml($fileName)
{
$fileLocation = $this->foundationLocation . '/dist/' . $fileName;
while (true) {
try {
$result = file_get_contents($fileLocation);
if ($result !== false){
return $result;
}
} catch (\Exception $e) {
}
}
}
}
And I use my service like this :
$foundationParser = $this->container->get('open4xmlparser.foundationparser');
$foundationParser->initFoundation();
$foundationParser->saveFoundationContent($foundationContent, 'test.html');
$response = new Response($foundationParser->retrieveValidHtml('test.html'));
$response->headers->set('Content-Type', 'text/html');
$response->send();
And it tells me that 'test.html' does not exist. Any idea on how to do what I want?
What I finally decided to do is a Symfony command that launch my program in an infinite loop(it is never supposed to stop). Instead of using a service I executed 'npm run build' directly in the while loop.
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln([
'',
'Running program',
'===============',
''
]);
$runBuild = new Process('npm run build', $this->getContainer()->getParameter('foundation_location'));
while (true) {
if (!$runBuild->isRunning()){
$output->writeln([
'',
'Executing npm run build',
''
]);
$runBuild->start();
}
}
}
You seem to have different paths in the saveFoundationContent and retrieveValidHtml for the target file.
// saveFoundationContent()
$fileLocation = $this->foundationLocation . '/src/pages/' . $filename;
// retrieveValidHtml()
$fileLocation = $this->foundationLocation . '/dist/' . $fileName;
Obviously, retrieveValidHtml() cannot find the file in the location.
Tip: Store the subdirectory path as a class variable (or constant):
class FoundationParser
{
private $subdir = "/dist/"; // or "/src/pages/";
…
public function retrieveValidHtml($fileName)
{
$fileLocation = sprintf("%s%s%s", $this->foundationLocation, $this->subdir, $fileName);
…
}
}
After a slight modification of my units, I wanted the update with a simple php app/console doctrine: update --force. But no action executed and in addition no response. I then did a php app/check.php meaning me no problems (Your system is ready to run Symfony2 projects). I do not understand and it doesn't provide an error. Here's what I've done:
Command: ********: ***** ProjetSymphony $ php app / console***
Answer (none): ******* **** $ ProjetSymphony***
If someone has an idea.
Screen :
Try with:
php app/console doctrine:schema:update --force
Maybe it's only a syntaxis error.
Also, if anyone tries to run php app/console in a newer symfony version (for example symfony 3.0), you will get an error: no file found because the file was moved to 'bin' folder. Now to run from the console, you have to use php bin/console instead. Just in case this change confused anyone who started to learn symfony and updated to 3.0.
I finally found my mistake. I had a command file that prevented the execution of my order (CreateUserCommand.php)
If someone wants to explain to me why this cosait file an error during the execution of my order ...
Here is the file :
<?php
namespace FP\UserBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use FOS\UserBundle\Model\User;
use FOS\UserBundle\Command\CreateUserCommand as BaseCommand;
class CreateUserCommand extends BaseCommand
{
/**
* #see Command
*/
protected function configure()
{
exit;
echo "tes";
parent::configure();
$this
->setName('fp:user:create')
->getDefinition()->addArguments(array(
new InputArgument('age', InputArgument::REQUIRED, 'The age')
))
;
}
/**
* #see Command
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
exit;
echo "tes";
$username = $input->getArgument('username');
$email = $input->getArgument('email');
$password = $input->getArgument('password');
$age = $input->getArgument('age');
$inactive = $input->getOption('inactive');
$superadmin = $input->getOption('super-admin');
$manipulator = $this->getContainer()->get('fos_user.util.user_manipulator');
$manipulator->setAge($age);
$manipulator->create($username, $password, $email, !$inactive, $superadmin);
$output->writeln(sprintf('Created user <comment>%s</comment>', $username));
}
/**
* #see Command
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
exit;
echo "tes";
parent::interact($input, $output);
if (!$input->getArgument('age')) {
$age = $this->getHelper('dialog')->askAndValidate(
$output,
'Please choose a age:',
function($age) {
if (empty($age)) {
throw new \Exception('Lastname can not be empty');
}
return $age;
}
);
$input->setArgument('age', $age);
}
}
}
I want to set up an order allowing me to make clear: cache test mode, then do a drop database, drop scheama, add scheme, add fixtures in test mode.
class BaseCommand extends \Symfony\Component\Console\Command\Command {
//put your code here
protected function configure()
{
$this
->setName('mycommand:test')
->setDescription('Launch test')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$command_first_migration = $this->getApplication()->find('cache:clear');
$arguments_first_migration = array(
'command' => 'cache:clean',
'--env' => 'test'
);
$input_first_migration = new ArrayInput($arguments_first_migration);
try {
$returnCode = $command_first_migration->run($input_first_migration, $output);
} catch (\Doctrine\DBAL\Migrations\MigrationException $ex) {
echo "MigrationExcepion !!!! ";
}
}
}
but I have this result :
clearing the case for the dev environment with debug true
How to pass the test in dev environment?
thank you
You can't set the --env=test since the Kernel and the environment are already created when you run php app/console mycommand:test.
The only way is to specify the env when you run your command :
php app/console mycommand:test --env=test
Is there a way to run a console command from a Symfony 2 test case? I want to run the doctrine commands for creating and dropping schemas.
This documentation chapter explains how to run commands from different places. Mind, that using exec() for your needs is quite dirty solution...
The right way of executing console command in Symfony2 is as below:
Option one
use Symfony\Bundle\FrameworkBundle\Console\Application as App;
use Symfony\Component\Console\Tester\CommandTester;
class YourTest extends WebTestCase
{
public function setUp()
{
$kernel = $this->createKernel();
$kernel->boot();
$application = new App($kernel);
$application->add(new YourCommand());
$command = $application->find('your:command:name');
$commandTester = new CommandTester($command);
$commandTester->execute(array('command' => $command->getName()));
}
}
Option two
use Symfony\Component\Console\Input\StringInput;
use Symfony\Bundle\FrameworkBundle\Console\Application;
class YourClass extends WebTestCase
{
protected static $application;
public function setUp()
{
self::runCommand('your:command:name');
// you can also specify an environment:
// self::runCommand('your:command:name --env=test');
}
protected static function runCommand($command)
{
$command = sprintf('%s --quiet', $command);
return self::getApplication()->run(new StringInput($command));
}
protected static function getApplication()
{
if (null === self::$application) {
$client = static::createClient();
self::$application = new Application($client->getKernel());
self::$application->setAutoExit(false);
}
return self::$application;
}
}
P.S. Guys, don't shame Symfony2 with calling exec()...
The docs tell you the suggested way to do it. The example code is pasted below:
protected function execute(InputInterface $input, OutputInterface $output)
{
$command = $this->getApplication()->find('demo:greet');
$arguments = array(
'command' => 'demo:greet',
'name' => 'Fabien',
'--yell' => true,
);
$input = new ArrayInput($arguments);
$returnCode = $command->run($input, $output);
// ...
}
Yes, if your directory structure looks like
/symfony
/app
/src
then you would run
phpunit -c app/phpunit.xml.dist
from your unit tests you can run php commands either by using
passthru("php app/console [...]") (http://php.net/manual/en/function.passthru.php)
exec("php app/console [...]") (http://www.php.net/manual/en/function.exec.php)
or by putting the command in back ticks
php app/consode [...]
If you are running the unit tests from a directory other than symofny, you'll have to adjust the relative path to the app directory for it to work.
To run it from the app:
// the document root should be the web folder
$root = $_SERVER['DOCUMENT_ROOT'];
passthru("php $root/../app/console [...]");
The documentation has been updated since my last answer to reflect the proper Symfony 2 way of calling an existing command:
http://symfony.com/doc/current/components/console/introduction.html#calling-an-existing-command
Is it possible to dump a database using doctrine 2? I have read that symfony has a library which extends doctrine to do it but How could I use it in my zendframework project with Bisna Doctrine 2 Integration?
For Symfony2:
Type
php app/console doctrine:schema:create --dump-sql
in the command line
This is an old thread but I was just doing something similar in Symfony and decided to develop an actual command for it. That's more of a Symfony way of doing it and gives you more control on the output as well as allowing you access to the parameters, so you don't have to parse Yaml using bash script :)
namespace Fancy\Command;
use Fancy\Command\AbstractCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Exception\IOExceptionInterface;
class DatabaseDumpCommand extends AbstractCommand
{
/** #var OutputInterface */
private $output;
/** #var InputInterface */
private $input;
private $database;
private $username;
private $password;
private $path;
/** filesystem utility */
private $fs;
protected function configure()
{
$this->setName('fancy-pants:database:dump')
->setDescription('Dump database.')
->addArgument('file', InputArgument::REQUIRED, 'Absolute path for the file you need to dump database to.');
}
/**
* #param InputInterface $input
* #param OutputInterface $output
* #return int|null|void
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$this->database = $this->getContainer()->getParameter('database_name') ;
$this->username = $this->getContainer()->getParameter('database_user') ;
$this->password = $this->getContainer()->getParameter('database_password') ;
$this->path = $input->getArgument('file') ;
$this->fs = new Filesystem() ;
$this->output->writeln(sprintf('<comment>Dumping <fg=green>%s</fg=green> to <fg=green>%s</fg=green> </comment>', $this->database, $this->path ));
$this->createDirectoryIfRequired();
$this->dumpDatabase();
$output->writeln('<comment>All done.</comment>');
}
private function createDirectoryIfRequired() {
if (! $this->fs->exists($this->path)){
$this->fs->mkdir(dirname($this->path));
}
}
private function dumpDatabase()
{
$cmd = sprintf('mysqldump -B %s -u %s --password=%s' // > %s'
, $this->database
, $this->username
, $this->password
);
$result = $this->runCommand($cmd);
if($result['exit_status'] > 0) {
throw new \Exception('Could not dump database: ' . var_export($result['output'], true));
}
$this->fs->dumpFile($this->path, $result);
}
/**
* Runs a system command, returns the output, what more do you NEED?
*
* #param $command
* #param $streamOutput
* #param $outputInterface mixed
* #return array
*/
protected function runCommand($command)
{
$command .=" >&1";
exec($command, $output, $exit_status);
return array(
"output" => $output
, "exit_status" => $exit_status
);
}
}
and AbstractCommand is just a class that extends symfony's ContainerAwareCommand:
namespace Fancy\Command;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
abstract class AbstractCommand extends ContainerAwareCommand
{
}
Doctrine has no database-dump feature.
I agree it would be nice, but it's also not the ORM's goal.
You could dump the database using
a PHP script
a system mysqldump
phpMyAdmin
Here's an article explaining those solutions.
I created a small script that read the parameters from app/config/parameters.yml and output all the data from a MySQL database to a file (with current datetime used as name).
Save this in the root of your Symfony project (e.g. mysqldump.sh):
#!/bin/bash
# See http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in/23905052#23905052
ROOT=$(readlink -f $(dirname "$0"))
cd $ROOT
# Get database parameters
dbname=$(grep "database_name" ./app/config/parameters.yml | cut -d " " -f 6)
dbuser=$(grep "database_user" ./app/config/parameters.yml | cut -d " " -f 6)
dbpassword=$(grep "database_password" ./app/config/parameters.yml | cut -d " " -f 6)
filename="$(date '+%Y-%m-%d_%H-%M-%S').sql"
echo "Export $dbname database"
mysqldump -B "$dbname" -u "$dbuser" --password="$dbpassword" > "$filename"
echo "Output file :"
ls -lh "$filename"
Result when running the script:
$ bash mysqldump.sh
Export […] database
Warning: Using a password on the command line interface can be insecure.
Output file :
-rw-rw-r-- 1 […] […] 1,8M march 1 14:39 2016-03-01_14-39-08.sql
Depend on your database. if you use mysql, create a php command to utilise mysqldump
like running this
mysqldump -u YourUser -p YourDatabaseName > wantedsqlfile.sql
For a more generic doctrine way:
protected function execute(InputInterface $input, OutputInterface $output)
{
$conn = $this->getDoctrineConnection('default');
$path = $input->getArgument('filepath');
if (! is_dir(dirname($path))) {
$fs = new Filesystem();
$fs->mkdir(dirname($path));
}
$cmd = sprintf('mysqldump -u %s --password=%s %s %s > %s',
$conn->getUsername(),
$conn->getPassword(),
$conn->getDatabase(),
implode(' ', ['variables', 'config']),
$path
);
exec($cmd, $output, $exit_status);
}