How to debug Symfony command using Xdebug and phpStorm? - debugging

First of all let's start saying I am running the whole project in a Docker container and that means I don't have anything installed on the host.
Having that I am trying to debug a Symfony command which is kind of CLI script with the difference it's not being called as php script.php but as bin/console command.
What does the console file has inside it? See below:
#!/usr/bin/env php
<?php
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Input\ArgvInput;
use Symfony\Component\Debug\Debug;
set_time_limit(0);
/** #var Composer\Autoload\ClassLoader $loader */
$loader = require __DIR__.'/../app/autoload.php';
$input = new ArgvInput();
$env = $input->getParameterOption(['--env', '-e'], getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(['--no-debug', '']) && $env !== 'prod';
if ($debug) {
    Debug::enable();
}
$kernel = new AppKernel($env, $debug);
$application = new Application($kernel);
$application->run($input);
I have Xdebug installed and configured, I have phpStorm configured as well and that means using the browser (and the extension to enable/disable) I can debug whatever I want.
Typically this is how the command is executed from the console (bash):
<path_to_symfony>/bin/console quote:notify -d 120 -e <some_email>
Options are optional here and that means I can call the same as:
<path_to_symfony>/bin/console quote:notify
It will work both ways. I am using the docs from here to debug such command. Now this is how I am setting up the IDE to debug a Symfony command:
Go to Run/Debug Configuration
Click on Add button (little green + symbol)
Choose PHP Script
Name it as SF Command
At File box I wrote bin/console
At Arguments box I wrote quote:notify (which is my command)
But the IDE says first: Error: file not specified or invalid and then when I try to execute the command it says it's not a valid configuration. (also notice the little "X" which should means something is wrong)
What I am missing here? Can I get some help here? I am using the latest version of phpStorm.

Related

How to get Artisan::call('command:name') output in a variable?

Is it possible to get $output as follows:
$output = Artisan::call('command:name');
I have tried many solutions in different posts but it didn't work on my Laravel 5.2.
You can call the output method on Artisan
Artisan::call('command:name');
$output = Artisan::output();
Make sure you are using one of the available output methods like $this->line('output'); in the actual Artisan command. More info in the docs.
There are several ways to accomplish this, but as your are using such old version of Laravel maybe the best for your case is one that will not require a rewrite when you finally migrate to a newer version. Have you tried perhaps the vanilla PHP methods system, shell_exec and passthru?
system will return just the last line of the command on succes and FALSE on failure
shell_exec will return the entire output but without a way to fetch the output status code
passthru will not return any output instead it will output all on stdout. Although it can be put into a variable using output cache methods (i.e. ob_start and ob_get_contents)
In any case you should call those methods using as argument the CLI version of the command you wish to run:
$output = shell_exec("php artisan command:here");
P.S. If you by any chance have a user input that you want to pass as parameter to a artisan command, make sure you escape it with escapeshellcmd first.

Escape variable in Jenkins shell execution

We use Jenkins to run some Wordpress CLI stuff for us, and I would like to pass in some PHP to the Wordpress CLI. For example:
cd wordpress && wp core config --skip-check --dbhost=dbhost --dbname=dbname --dbuser=user --dbpass=pass --extra-php <<PHP
define('HOST', $_SERVER['HTTP_HOST']);
PHP
Jenkins interprets the $_SERVER and I'm left with ['HTTP_HOST']. How can I escape $_SERVER?
I've tried doing define('HOST', \$_SERVER['HTTP_HOST']);, but that still gets interpreted as a variable.
Figured it out... Wrap the first section of defining HEREDOC in double quotes.
cd wordpress && wp core config --skip-check --dbhost=dbhost --dbname=dbname --dbuser=user --dbpass=pass --extra-php <<"PHP"
define('HOST', $_SERVER['HTTP_HOST']);
PHP
It no longer interprets $_SERVER.

How to pass variable in bash for php command

On bash funcion I need pass varible por a external command (php)
panel=$(php -r '$ini_array = parse_ini_file("/root/.name/name.ini");echo $ini_array['panel'];')
Work properly.
But I need pass path on variable and key of array also.
Try several form but all fails. '', "", ...
php -r '$ini_array = parse_ini_file(`$sPath`);echo $ini_array["$sKey"];'
It's possible?
Pass the variable through the environment, and retrieve it from the environment on the PHP side.
sPath=$sPath sKey=$sKey \
php -r '$ini_array = parse_ini_file($_ENV["sPath"]);echo $ini_array[$_ENV["sKey"]];'
The initial sPath=$sPath and sKey=$sKey are not necessary if you have used export to put your shell variables in the environment already.

Running Shells as cronjobs

I am trying to work out how to setup a Shell to run as a cron, I have created a TaskEmailerShell.php file at /app/Console/Command/TaskEmailerShell.php with the following code:
App::uses('CakeEmail', 'Network/Email');
class TaskEmailerShell extends AppShell {
public $uses = array('Task');
public function main()
{
// GENERATE AND SEND THE EMAIL
// ------------------------------------------------------------>
$email = new CakeEmail();
etc etc
I have followed the instructions here and when the cron runs I get this error:
/home/village/public_html/app/Console/cakeshell: line 14: cake: command not found
My cron command is:
/home/village/public_html/app/Console/cakeshell TaskEmailer -cli /usr/bin -console /Cake/Console -app /home/village/public_html/app >> /home/village/public_html/emailer_log.log
Any suggestions where I am going wrong?
The cakeshell script you're referring to there requires the cake console binary to be somewhere in PATH, or (at least from looking at the usage example), for you to have specified the full path to the Console with the -console argument. It looks like you've got a relative path there.
Personally I recommend just calling the cake console directly from your cron job. As other needs arise you may end up writing a script like cakeshell to wrap the console. You could try something like :
# m h dom mon dow command
*/5 * * * * /home/village/public_html/lib/Cake/Console/cake -app '/home/village/public_html/app/' TaskEmailer >> /home/village/public_html/emailer_log.log
Command not found usually means your PATH is the problem. Programs run from cron might not even have PATH set. Any script started by cron must set PATH near the beginning or it won't find any of the programs it is supposed to run.
The error occurs at line 14 in the file cakeshell: $cmd. cmd is set at line 4.
#!/bin/bash
TERM=dumb
export TERM
cmd="/opt/lampp/htdocs/MyApp/app/Console/cake"
while [ $# -ne 0 ]; do
if [ "$1" = "-cli" ] || [ "$1" = "-console" ]; then
PATH=$PATH:$2
shift
else
cmd="${cmd} $1"
fi
shift
done
$cmd
Originally line 4 is
cmd="cake"
Change it to absolute path like:
cmd="/opt/lampp/htdocs/MyApp/app/Console/cake"
where MyApp is the application directory. I have faced this problem even after PATH settings. Then mentioning the absolute path solved it.

Doctrine 2 CLI commands from within PHP

Hey all. I am writing a program that will transform some data in our database, and then call Doctrine to build YAML files from said Mysql Database structure. I have Doctrine working from within PHP. However I can't figure out how to call the CLI commands from within PHP. Following is the Doctrine 2 CLI command that does what I need.
php ./doctrine orm:convert-mapping --filter="users" --from-database yml ./test
This command works from the Linux command line, but how to I do this same thing via Doctrine objects? I don't want to just use the PHP exec statement to send a command to the shell. I wish to use the Doctrine object model.
Don!:
Apparently this is not a very common programming method. However, I have used Doctrine from PHP by calling it via the PHP EXEC command. I know you said that you would not like to do it this way. However, it actually works quite well. Below is an example of such a solution.
$cmd_string = "php ./doctrine orm:generate-entities --generate-annotations=1 --regenerate-entities=1 $this->entity_file_dir";
$result = array();
exec($cmd_string, &$result);
Hope this helps,
-Don!
I stumbled upon this question when trying to execute a command directly from a PHP script, without using the CLI.
Particularly, I was needing to call orm:ensure-production-settings.
Each Doctrine command has its own class: http://www.doctrine-project.org/api/orm/2.4/namespace-Doctrine.ORM.Tools.Console.Command.html
I solved it the following way:
$entityManager = ...; // Get the entity manager somehow in your application
// Creates the helper set
$helperSet = \Doctrine\ORM\Tools\Console\ConsoleRunner::createHelperSet($entityManager);
// Initializes the desired command and sets the helper set
// In your case it should be ConvertMappingCommand instead
$command = new \Doctrine\ORM\Tools\Console\Command\EnsureProductionSettingsCommand();
$command->setHelperSet($helperSet);
// Initializes the input
// Alternatives: http://api.symfony.com/2.0/Symfony/Component/Console/Input.html
$input = new \Symfony\Component\Console\Input\ArgvInput(); // Input coming from the CLI arguments
// Initializes the output
// Alternatives: http://api.symfony.com/2.0/Symfony/Component/Console/Output.html
$output = new \Symfony\Component\Console\Output\ConsoleOutput(); // Prints the output in the console
// Runs the command
$command->run($input, $output);
I'm new to Doctrine so I'm not exactly sure how this works, but it does. Any comment is appreciated.

Resources