Symfony debug still enabled after cache:warmup --no-debug - symfony

I have Symfony 3.2.7 installed on a production server.
I issue the following commands:
bin/console cache:clear --no-warmup --no-debug --env=preview
bin/console cache:warmup --no-debug --env=preview
bin/console --version
I get the following output, showing that debug is enabled.
Symfony 3.2.7 (kernel: app, env: preview, debug: true)
What am I missing?

Nevermind.
Bad programmer on device.
The monolog.handlers.main.level value was set to debug.
Duh.
Sorry for the noise.

I've never used bin/console in a production server, but I don't think that information is reliable for your use case.
If you open the bin/console script in an editor you will see:
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(['--no-debug', '']) && $env !== 'prod';
As you can see the $debug parameter is determined by a combination of environment variables and parameters.
Every time you call this script this value is recalculated.

Related

Symfony cache:clear problem: No session available

The problem is very simple. The applications works as expected, no problems, sessions work, etc.
But when I try to clear the cache:
bin/console c:c
// Clearing the cache for the dev environment with debug true
In RequestStack.php line 105:
There is currently no session available.
Any ideas?
Symfony version 6, all the latest packages.
The issue was in one of the Services, trying to call
requestStack->getSession()
in the __construct method.
The key to finding this error was to get a stack trace, using the bin/console c:c -vvv

remove test database when tests ends (Symfony / PHP Unit)

I am using PHPUnit (9.5) with Symfony (5.3).
For my tests, I use the default test database config from config/packages/test/doctrine.yaml :
doctrine:
dbal:
# "TEST_TOKEN" is typically set by ParaTest
dbname_suffix: '_test%env(default::TEST_TOKEN)%'
So my tests use the same database as prod with the suffix '_test'.
I added some code to tests/bootstrap.php to automate database creation / reset before each test runs :
// delete database if exists, then create
passthru('php bin/console doctrine:database:drop --env=test --force --if-exists');
passthru('php bin/console doctrine:database:create --env=test');
// run migrations
passthru('php bin/console doctrine:migrations:migrate --env=test -n');
and I use dama/doctrine-test-bundle for automatic transactions for each tests.
That is working very well, but I have a question :
Is there a way to delete the database at the end of test run ? (like I did in bootstrap.php)
I understand that your bootstrap.php file is running before the test, you need a solution to launch something after your test.
First, create a command that drop the test database.
In anyway, be very careful that the code in your command stop all execution, if you aren't in an explicit test environment (because it means you are in a production environment).
Then, you can alter your composer.json file to launch the created command after your test in a chain of scripts.
Here is an exemple
"scripts": {
"test-and-remove": [
"#putenv APP_ENV=test",
"phpunit --configuration phpunit.xml",
"php bin/console app:drop-test-database"
],
Then you only have to launch your test via this new command:
composer test-and-remove

Translate Validation Constraint Messages on Symfony

In order to have error messages in different languages, I am following the instructions under this link:
https://symfony.com/doc/current/validation/translations.html
I made the validator files with yaml:
For instance:
# translations/validators.en.yml
author.name.not_blank: Please enter the name of the author.
Notice: I tried the extensions yaml and yml
And in src/entity/Data.php
/**
* #var string
*#Assert\NotBlank(message="author.name.not_blank")
*/
private $author;
I just get the message "author.name.not_blank" instead of "Please enter the name of the author."
I cleared the cache like this:
https://symfony.com/doc/2.7/console/usage.html
php app/console cache:clear --env=prod
I get the temporary message on the terminal "Clearing the cache for the prod environment with debug false"
So my questions are: Am I doing something wrong in clearing the cache?
I am using Symfony 2.7
Is there something I am missing ?
By the way, all other translations from files such as
translations/messages.en.yml
are functionning perfectly well.
Thank you very much!
I see your code is fine:
now you can check additional stuff:
app/config:
translator: { fallbacks: ["%locale%"] }
and be sure which environment are you in.
instead of
php app/console cache:clear --env=prod
try
php app/console cache:clear
hope this will helps.

Symfony2 - Removed FOS User Bundle manually and now cannot clear cache in production mode

I installed FOS Userbundle to learn from it, but decided I didn't need it anymore. I then did the following to remove it. After removing, I ran composer update.
removed the bundle from the vendor folder
removed from the appKernel
removed from the composer.json file
removed the entity User.php file
removed the settings from config.yml, security.yml, routing.yml
Now when I attempt to clear cache for production mode I get the following error below. Clearing cache in dev mode works fine.
Can show me what I am doing wrong or what I am missing to remove FOS User Bundle and be able to clear cache in production mode?
PHP Fatal error: Class 'FOS\UserBundle\EventListener\LastLoginListener' not found in /var/www/html/HealthFitness/vendor/symfony/symfony/src/Symfony/Component/EventDispatcher/ContainerAwareEventDispatcher.php on line 142
PHP Stack trace:
PHP 1. {main}() /var/www/html/HealthFitness/app/console:0
PHP 2. Symfony\Component\Console\Application->run() /var/www/html/HealthFitness/app/console:27
PHP 3. Symfony\Bundle\FrameworkBundle\Console\Application->doRun() /var/www/html/HealthFitness/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:121
PHP 4. Symfony\Component\DependencyInjection\Container->get() /var/www/html/HealthFitness/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:86
PHP 5. appProdProjectContainer->getEventDispatcherService() /var/www/html/HealthFitness/app/bootstrap.php.cache:2037
PHP 6. Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher->addSubscriberService() /var/www/html/HealthFitness/app/cache/prod/appProdProjectContainer.php:343
Your error indeed probably come from a cache error.
Have you this error executing php app/console cache:clear --env=prod ?
You can else delete app/cache/prod/* manually.
In case you can't remove the files manually this should work as well.
You could also use a --no-warmup and --no-optional-warmers switches:
cache:clear --no-warmup --no-optional-warmers --env=prod
that way it should not try to recreate the cache of non existing classes and then do a
cache:warmup --env=prod
Manually removing cache is faster though, but you still warm it up.

How to set the environment in a Symfony2 console command

Hopefully a simple question - how does one specify which environment to use when running a console command in Symfony2. I've created a few commands, however I would like to run them in the context of my 'staging' environment when on my staging server and my 'prod' environment when on my production server (the different environments define different database connections). How do I configure and pass this information to my console command?
You have two options that will help you out. You can specify the environment that the console will run in with the --env flag, and use --no-debug to disable debug mode.
php app/console --env=staging your:console:command or php app/console --env=prod your:console:command should do what you're looking for (the console runs in the dev environment with debug on by default).
You can look at the code of the app/console file for more info.
You can also use the SYMFONY_ENV environment variable to set a default environment distinct to 'dev' (e.g. export SYMFONY_ENV=prod in ~/.bash_profile)
To answer the question #croca had, to expand on what #Francesc Rosàs posted, and as #Problematic suggested.
If you look in app/console you should see $env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
All this does is checks the input arguments passed to the console for --env or -e, checks the default value from getenv('SYMFONY_ENV'), or sets it to dev if neither are supplied.
It is then passed to $kernel = new AppKernel($env, $debug);
You could essentially either make changes directly to app/console to achieve your application's specific functionality or copy app/console to a separate file such as app/exec, then process the $env variable how you prefer to determine the desired environment.
Simple Example: app/exec
#!/usr/bin/env php
<?php
/**
* disabled debug mode,
* set environment based on requesting address being local
* otherwise the server's IP address
* be sure to perform at least chmod(750) on the file to ensure it is executable
* otherwise it must be prefixed with your php executable
*/
set_time_limit(0);
require_once __DIR__.'/bootstrap.php.cache';
require_once __DIR__.'/AppKernel.php';
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
$input = new ArgvInput();
$env = ($_SERVER['REMOTE_ADDR'] === '127.0.0.1' || $_SERVER['REMOTE_ADDR'] === '::1' ? 'dev' :
($_SERVER['SERVER_ADDR'] === '192.168.2.1' ? 'test' : 'prod')
);
$kernel = new AppKernel($env, false);
$application = new Application($kernel);
$application->run($input);
Then call php app/exec namespace:command arguments --flags
Additionally you could process your own application instead of using the app/console AppKernel - instructions from Symfony can be found here: http://symfony.com/doc/current/components/console/introduction.html
The official documentation says:
By default, console commands run in the dev environment and you may want to change this for some commands.
So, as #Problematic suggest, you can specify the environment for your command using the flag "--env=your_env" or its shortcut "-e your_env".
I wonder whether it is possible to force the default environment for a command. I mean, without having to use the flags. Any idea?
There is another variant, not so convenient though, but works also with composer
SYMFONY_ENV=prod app/console cache:clear
Maybe you could look at the implementation of the cache:clear command. This may give you some clues on how to choose the environment from a command.
app/console help cache:clear

Resources