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

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.

Related

Laravel 9 fatal error in Scheduler with Traits

I have a serious problem and I have tried everything to solve it without success. I am trying to call the Traits ParamTrait and MsgTrait from a Scheduler (Command) called MsgCron. Traits work fine for me throughout the app except Command.
Here my code MsgCron.php
<?php
namespace App\Traits;
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
use App\Models\Cita;
class MsgCron extends Command
{
protected $signature = 'msg:cron';
protected $description = 'Command description';
public function __construct()
{
parent::__construct();
}
use ParamTrait;
use MsgTrait;
public function handle()
{
$dia = date("d");
$mes = date("n");
$ano = date("Y");
Log::channel('balancemed')->info($dia.$mes.$ano);
return 0;
}
}
My composer.json:
"autoload": {
"files": [
"app/helpers.php",
"app/Traits/MsgTrait.php",
"app/Traits/ParamTrait.php",
"app/Console/Commands/MsgCron.php"
],
Running the php artisan schedule:run command gives me a fatal error:
ubuntu:~/environment/balancemed $ php artisan schedule:run
PHP Fatal error: Trait "App\Console\Commands\ParamTrait" not found in /home/ubuntu/environment/balancemed/app/Console/Commands/MsgCron.php on line 12
PHP Stack trace:
PHP 1. {main}() /home/ubuntu/environment/balancemed/artisan:0
PHP 2. require() /home/ubuntu/environment/balancemed/artisan:18
PHP 3. ComposerAutoloaderInita6e7bc892212647700001d53e511801f::getLoader() /home/ubuntu/environment/balancemed/vendor/autoload.php:7
PHP 4. composerRequirea6e7bc892212647700001d53e511801f($fileIdentifier = 'c272475c5c4e0eb0a144305d0ca49ad6', $file = '/home/ubuntu/environment/balancemed/vendor/composer/../../app/Console/Commands/MsgCron.php') /home/ubuntu/environment/balancemed/vendor/composer/autoload_real.php:61
PHP 5. require() /home/ubuntu/environment/balancemed/vendor/composer/autoload_real.php:71
Al ejecutar el comando composer dumpautoload me arroja el siguiente error:
ubuntu:~/environment/balancemed $ composer dumpautoload
Generating optimized autoload files
Class App\Http\Controllers\MsgTrait located in ./app/Traits/MsgTrait.php does not comply with psr-4 autoloading standard. Skipping.
Class App\Http\Controllers\ParamTrait located in ./app/Traits/ParamTrait.php does not comply with psr-4 autoloading standard. Skipping.
> Illuminate\Foundation\ComposerScripts::postAutoloadDump
PHP Fatal error: Trait "App\Console\Commands\ParamTrait" not found in /home/ubuntu/environment/balancemed/app/Console/Commands/MsgCron.php on line 12
Fatal error: Trait "App\Console\Commands\ParamTrait" not found in /home/ubuntu/environment/balancemed/app/Console/Commands/MsgCron.php on line 12
I have tried with php artisan optimize and nothing. It also gives me an error.
Can somebody help me ?
You should add your trait path to the MsgCron.php
use App\Http\Traits\MsgTrait;
use App\Http\Traits\ParamTrait;
Your Class Should Be Like This
use App\Http\Traits\MsgTrait;
use App\Http\Traits\ParamTrait;
class MsgCron extends Command
{
use MsgTrait;
use ParamTrait;
protected $signature = 'msg:cron';
protected $description = 'Command description';
...
}

Laravel - Elasticsearch ClientBuilder Not found

I am using Laravel 5.7 and Elasticsearch/elasticsearch ^6.1
I installed elasticsearch via composer and my class is as follows
use Elasticsearch\ClientBuilder;
trait Elasticable {
public abstract function getType();
public abstract function getIndex();
public function getClient() {
$elastic= Elasticsearch\ClientBuilder::create()
->setHosts(\Config::get('app.esserver'))
->build();
return $elastic;
}
However, I keep getting
Symfony\Component\Debug\Exception\FatalThrowableError: Class
'App\Traits\Elasticsearch\ClientBuilder' not found in file
Somehow Laravel / PHP is not recognizing Elasticsearch namespace or some autoload issue
I have tried composer dump-autoload and that does not help

PHPUnit Error: Call to undefined method Controller::request()

On running this test, I get Error: Call to undefined method OrderControllerTest::request()
<?php
use PHPUnit\Framework\TestCase;
class OrderControllerTest extends TestCase
{
public function testupload() {
$a='foo';
$output = $this->request('POST',['Order', 'upload',$a] );
}
}
PHPUNIT Version: 7.2.4. Appreciate any help
For the kenjis/ci-phpunit-test package if you run phpunit from the application/tests folder so it picks up on the included phpunit.xml and included TestCase Class then they should run.
cd application/tests
then, if you are using the composer installed version of phpunit in your project:
../../vendor/bin/phpunit
or if you're using the globally (apt etc.) installed version simply:
phpunit
see http://blog.a-way-out.net/blog/2015/06/12/codeigniter3-phpunit/#how-to-run-tests
The package kenjis/ci-phpunit-test extends the standard PHPUnit package so what you need to do is to extend the CIPHPUnitTestCase instead like in example below.
<?php
class OrderControllerTest extends CIPHPUnitTestCase
{
public function testupload() {
$a='foo';
$output = $this->request('POST',['Order', 'upload',$a] );
}
}
You may need to configure your IDE so that it can find CIPHPUnitTestCase class.

"Convert" package to load in Symfony

I have THIS package (a payment gateway), which I would like to use in Symfony 3.0.1
Unfortunately I get this error:
ClassNotFoundException in AppKernel.php line 21: Attempted to load class "SofortBundle" from namespace "Sofort\SofortLib".
Did you forget a "use" statement for another namespace?
In the sofort\sofortlib-php folder i created the file SofortBundle.php with this content
<?php
namespace Sofort\SofortLib;
use Symfony\Component\HttpKernel\Bundle\Bundle as BaseBundle;
class SofortBundle extends BaseBundle
{
}
and I loaded the Bundle in AppKernel.php:
new Sofort\SofortLib\SofortBundle(),
But that only leads to above exception.
What am I missing?
Don't copy packages to your custom folder. Install package as described:
In composer.json add:
"require": {
"sofort/sofortlib-php": "3.*"
}
Run composer update sofort/sofortlib-php
In your code you can use the library like this:
use \Sofort\SofortLib\Billcode;
class MyClass
{
function doSomething($configkey) {
$SofortLibBillcode = new Billcode($configkey);
...
}
}

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?

Categories