Symfony2: InvalidArgumentException with Command folder - php

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?

Related

Illuminate\Console\Command not found when developing laravel 5.8 package

I am developing a package for Laravel 5.8. When I try to create a console command that extends Illuminate\Console\Command then "composer dump-autoload" fails with error message:
c:\Program Files (x86)\Ampps\www\ptest>composer dump-autoload
Generating optimized autoload files> Illuminate\Foundation\ComposerScripts::postAutoloadDump
> #php artisan package:discover --ansi
ReflectionException : Class TestVendor\TestPackage\TestCommand does not exist
at C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:790
786| if ($concrete instanceof Closure) {
787| return $concrete($this, $this->getLastParameterOverride());
788| }
789|
> 790| $reflector = new ReflectionClass($concrete);
791|
792| // If the type is not instantiable, the developer is attempting to resolve
793| // an abstract type such as an Interface or Abstract Class and there is
794| // no binding registered for the abstractions so we need to bail out.
Exception trace:
1 ReflectionClass::__construct("TestVendor\TestPackage\TestCommand")
C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:790
2 Illuminate\Container\Container::build("TestVendor\TestPackage\TestCommand")
C:\Program Files (x86)\Ampps\www\ptest\vendor\laravel\framework\src\Illuminate\Container\Container.php:667
I have tried to create the package by hand inside of C:\Program Files (x86)\Ampps\www\ptest\packages folder and I tried to use the packager https://github.com/Jeroen-G/laravel-packager but the result is identical in both cases.
TestCommand.php
<?php
namespace TestVendor\TestPackage;
use Illuminate\Console\Command;
class TestCommand extends Command {
protected $signature = 'test:hello';
protected $description = 'say hello';
public function __construct()
{
parent::__construct();
}
public function handle()
{
$this->info("hello!");
}
}
TestServiceProvider.php
<?php
namespace TestVendor\TestPackage;
use Illuminate\Support\ServiceProvider;
class TestServiceProvider extends ServiceProvider
{
public function boot()
{
if ($this->app->runningInConsole()) {
$this->bootForConsole();
}
}
public function register()
{
$this->mergeConfigFrom(__DIR__.'/../config/testpackage.php', 'testpackage');
$this->app->singleton('testpackage', function ($app) {
return new TestPackage;
});
}
public function provides()
{
return ['testpackage'];
}
protected function bootForConsole()
{
// Registering package commands.
$this->commands([TestCommand::class]);
}
}
When I execute the TestCommand.php file directly from command line it fails with the error message
PHP Fatal error: Class 'Illuminate\Console\Command' not found
I have checked other working packages inside "Vendor" folder and all have the same structure as my package. It seems as if autoloading does not work properly.
The "console" folder was outside of "src" folder. Therefore it could not be discovered.
remove vendor and node_modules folder
then run following commands
composer update
npm install
it should work fine.

Laravel custom command not working

I made a new command with:
php artisan make:console CrawlData
Then I changed two variables:
protected $signature = 'make:crawl';
protected $description = 'My crawling command';
The problem is that when I run:
php artisan make:crawl
It outputs:
[Symfony\Component\Console\Exception\CommandNotFoundException]
Command "make:crawl" is not defined.
You also need to register the command in the App\Console\Kernel class for it to be recognized:
protected $commands = [
...
\App\Console\Commands\CrawlData::class,
];
You can read more about that in the Registering Commands documentation.
Starting with Laravel 5.5 commands in app/Console/Commands are automatically registered.

Lumen make:command

I'm trying to execute code within my Lumen install via the command line. In full Laravel , I've read that you can use commands to achieve this via "make:command", but Lumen does not seem to support this command.
Is there anyway to enable this command? Failing that, what's the best way of running code from the CLI in Lumen?
Thanks
You can use the artisan CLI in Lumen as the same way as in Laravel but with fewer built-in commands. To see all built-in commands, use the php artisan command in Lumen.
Although there is no make:command command at Lumen, you can create your custom command:
Add new command class inside the app/Console/Commands folder, you can use the sample class template of the framework serve command
Register your custom command by adding your created class to the $commands member inside the app/Console/Kernel.php file.
Except the command generating, you can use the Laravel docs for commands when working with Lumen.
Here is a template for a new command.
You can just copy and paste this in to a new file and start working.
I tested it on lumen 5.7.0
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
class CommandName extends Command
{
/**
* The name and signature of the console command.
*
* #var string
*/
protected $signature = 'commandSignature';
/**
* The console command description.
*
* #var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* #return mixed
*/
public function handle()
{
$this->info('hello world.');
}
}
Then register it on the Kernel.php file.
/**
* The Artisan commands provided by your application.
*
* #var array
*/
protected $commands = [
\App\Console\Commands\CommandName::class
];
When you create your command class use this:
<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
Instead of what was described above about using serve command example

Run specific datafixture + Symfony2

I know you can run all datafixtures by running this command:
php app/console doctrine:fixtures:load
And that you can run a specific fixture by running this command:
php app/console doctrine:fixtures:load --fixtures=/path/to/fixture1
But now I'm trying the second command to load a specific datafixture like this:
php app/console doctrine:fixtures:load --fixtures=/src/VolleyScout/VolleyScoutBundle/DataFixtures/ORM/LoadRegionData
My LoadRegionData class is located at:
src/VolleyScout/VolleyScoutBundle/DataFixtures/ORM/LoadRegionData.php
When I run the command I always get the following error:
[InvalidArgumentException]
Could not find any fixtures to load in:
- /src/VolleyScout/VolleyScoutBundle/DataFixtures/ORM/LoadRegionData
LoadRegionData.php
<?php
namespace VolleyScout\VolleyScoutBundle\DataFixtures\ORM;
use Doctrine\Common\DataFixtures\FixtureInterface;
use Doctrine\Common\Persistence\ObjectManager;
use VolleyScout\VolleyScoutBundle\Entity\Regions;
class LoadRegionData implements FixtureInterface
{
/**
* {#inheritDoc}
*/
public function load(ObjectManager $manager)
{
$regions = ['West-Vlaanderen', 'Oost-Vlaanderen', 'Antwerpen', 'Vlaams-Brabant', 'Limburg', 'Vlaanderen'];
foreach($regions as $r){
$region = new Regions();
$region->setRegionName($r);
$region->setRegionDescription($r);
$manager->persist($region);
$manager->flush();
}
}
}
?>
try
php app/console doctrine:fixtures:load --fixtures=src/VolleyScout/VolleyScoutBundle/DataFixtures/ORM
Try it with the full path:
php app/console doctrine:fixtures:load --fixtures=/Users/foobar/projects/projectname/src/VolleyScout/VolleyScoutBundle/DataFixtures/ORM

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

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());
}
}

Categories