How to get the running path of a Symfony Console application? - php

Is there any way to get the running path in a Symfony Console application? For example (assuming php interpreter in PATH):
cd /tmp
php /home/user/myapplication/app/console.php mycommand
Should return /tmp as console.php was launched from /tmp.

getcwd() will do what you need. You can execute app/console from any directory, and PHP will know which one it is.
I used the following example to verify this.
<?php
namespace Acme\DemoBundle\Command;
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;
class DemoCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
->setName('demo:cwd')
->setDescription('Get Current Working Directory')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln(getcwd());
}
}

Related

Execute bundle command line

I created a small symfony4 bundle to manage Mysql database backup.
I created a packagist folder to implement it easily.
after install, my package path is:
webDirectory\vendor\fpasquer\symfony-backup-bundle\BackupSymfonyBundle.php
I'm able to use every class from this bundle excepted commands.
This is one command:
<?php
namespace Fpasquer\BackupSymfony\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class BackupExportCommand extends AbstractBackup
{
protected static $defaultName = 'Fpasquer:BackupSymfony:extract';
...
}
When I run it :
php bin/console Fpasquer:BackupSymfony:extract
I get this exception:
There are no commands defined in the "Fpasquer:BackupSymfony" namespace.
I'm sure my bundle is installed correctly because in my app:controller I'm able to use DependencyInjection from this bundle
Do you have any idea what's wrong?

Laravel 5.4 - SoapClient not loaded from Job

I have a problem getting PHP's SoapClient to work in my Laravel Job.
I created queueable job where I import the SoapClient with use SoapClient but Laravel is not able to find it.
But when I use the SoapClient in my Controllers it works flawlessly.
I checked phpini(): SoapClient is definitely enabled.
Any Ideas?
My Job Code:
<?php
namespace App\Jobs;
use SoapClient;
use Tymon\JWTAuth\Exceptions\JWTException;
use JWTAuth;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class ProcessQueuedRenderRequests implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public function __construct()
{
}
public function handle()
{
$soap = new SoapClient("http://mywsdl");
...
Errorlog:
[[2017-11-22 18:17:50] local.ERROR: Symfony\Component\Debug\Exception\FatalThrowableError: Class 'SoapClient' not found in /var/www/app/Jobs/ProcessQueuedRenderRequests.php:44
I use Docker with the Laradock configuration.
The joblistener ist started in this way:
docker-compose exec workspace bash
php artisan queue:listen
use
$soap = new \SoapClient("http://mywsdl");
I had the same issue once.
Found the problem. Soap was not installed in the workspace container.
When I start the joblistener like this it works:
docker-compose exec php-fpm bash
php artisan queue:listen
Still a bit strange since should also be installed in the workspace container according to my .env file.

Laravel ReflectionException in Controller

I'm trying to inject the class HotelsTransformer without success with the next code:
UserTransformer
<?php
namespace App\Transformers;
class UserTransformer extends Transformer
{
...
}
HotelsTransformer
<?php
namespace App\Transformers;
class HotelsTransformer extends Transformer
{
...
}
ApiHotelsController
<?php
namespace App\Http\Controllers\Api;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use \App\Hotel;
use \App\Transformers\HotelsTransformer;
class ApiHotelsController extends ApiController
{
protected $HotelsTransformer;
public function __construct(HotelsTransformer $HotelsTransformer)
{
$this->HotelsTransformer = $HotelsTransformer;
dd($this->HotelsTransformer);
}
When I inject UserTransformer, it's all OK, but when I change UserTransformer with HotelsTransformer it throws me this error.
I don't know why is this happening, because I cloned UserTransformer and change its name but same error persists.
Check your following namespace. May be it does not exist or namespace path is not correct
use \App\Transformers\HotelsTransformer;
Try to run composer dumpauto command.
Try running these commands from your terminal (from the root directory of your project)
// use sudo if it asks for the root permission
composer update
composer dump-autoload
php artisan config:clear
Rerun the app/project and try again.
These 2 commands will refresh composer loaded classes and clear the cache to make the project run freshly, hope it helps.!
Ok, I have solved it, the filename was wrong, changed:
app/Transformers/HotelsTranformer.php
to:
app/Transformers/HotelsTranformer.php
1 hour spent on that like a crazy, good job.

Symfony2: InvalidArgumentException with Command folder

I have a Symfony2 application. In one bundle, I have a command class inside a Command folder; then, when I try to execute:
php app/console doctrine:database:[something]
I get:
[InvalidArgumentException]
There are no commands defined in the "doctrine:database" namespace.
BUT, if I rename the Command folder (Command_ for example), the exception does not appear and all runs well, the problem is that I cannot use the command if the Command folder's name isn't 'Command'.
Here's may code:
RunMigrationXCommand.php:
<?php
#!/usr/bin/env php
// app/console
use Symfony\Component\Console\Application;
use eCommerce\MigrationBundle\Command\MigrationXCommand;
$application = new Application();
$application->add(new MigrationXCommand);
$application->run();
And MigrationXCommand.php:
class MigrationXCommand extends ContainerAwareCommand {
protected function configure() {
parent::configure();
$this
->setName('migrationX')
->setDescription('Migration BBDD')
->addArgument(
'argument', InputArgument::OPTIONAL, 'What do you want to migrate?'
);
}
protected function execute(InputInterface $input, OutputInterface $output) {
// ...
}
Any fix?

symfony 2 command issue

I am trying to run a php script form the command line.
sudo vim UpdateLatestIssuesCommand.php
But its giving me the following error:
PHP Fatal error: Class 'Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand' not found in /Applications/MAMP/htdocs/imagine-publishing/src/Imagine/CorporateBundle/Command/UpdateLatestIssuesCommand.php on line 12
I cant figure out why I am getting this error because the file its says it cant find is actually there.
Heres my code:
<?php
namespace Imagine\CorporateBundle\Command;
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;
class UpdateLatestIssuesCommand extends ContainerAwareCommand
{
You have to run the command like this
php app/console demo:greet Fabien
where, demo:greet is the name of the command, Fabien is the argument
Reason
Because, console component has to bootstrap required resources for you before it runs the command.
FYI: Console Component

Categories