Class for command controller not put to autoload - php

I have a composer based TYPO3 7.6 installation and want to create an Extbase extension with a command controller.
The controller is registered in ext_localconf.php but the commmand controller isn't found because the class is not found in typo3/sysext/extbase/Classes/Mvc/Cli/CommandManager.php in public function getAvailableCommands()
Namespace is also set: namespace Foo\FooT3monitoringNotification\Command;
The class is here Classes/Command/NotificationCommandController.php.
I've cleared all TYPO3 caches and did composer dump-autoload. Any idea, what I missed to do or what I can do to find out, why my class isn't put to autoload?

As your newly created extension is not installed via composer, you need to define where to look for the classes. Therefore you need to add an autoload section to your root composer.json (that' means not in your extension, but in your TYPO3 distribution root folder):
"autoload": {
"psr-4": {
"Foo\\FooT3monitoringNotification\\": "web/typo3conf/ext/foo_t3monitoring_notification/Classes"
}
}
More information: https://usetypo3.com/typo3-and-composer.html#c67

Related

How can I check if PHP Composer autoload is working?

I am trying to transfer a small web application, which uses PHP Composer, from an old out of date server to a new server.
On the new server, I have run composer install to install the dependencies from the composer.lock file.
The web application requires a config file to set itself up (not that it does a lot of setting up), which includes the line
require_once __DIR__ . '/vendor/autoload.php';
…which should locate the autoloader relative to the config file, and make the packages installed by Composer known to the system.
However, this doesn't seem to be working properly, as when the web app tries to use SwiftMailer (which was installed via Composer), it says:
PHP Fatal error: Uncaught Error: Call to undefined method Swift_SmtpTransport::newInstance()
…in the file that calls it, which obviously suggests that it does not know about it and cannot find it.
Is there a way that I can meaningfully check what the result of the autoloader include is and see what it is (or is not) doing?
Make sure to add a use statement to your classes. You can arrange your classes and give your classes namespaces then allow composer to also auto-load your classes.
You can tell composer how your classes are arranged according to namespaces as follows:
In the autoload section of your composer.json file edit it to know how the namespaces of your classes is arranged
"autoload": {
"psr-4": {
"App\\": "src/",
"Mynamespace\\": "lib/src/ProjectSrc/"
}
},
The autoload section now tells composer the Classes in the ProjectSrc folder will have a root namespace of Mynamespace. From this information composer can determine all other sub namespaces in your directory tree. This will not happen automatically composer has to rebuild its mapping of classes with the composer dump-autoload command.

Source from vendor loaded and compiled as files from src directory

I am trying the new Symfony4 (framework bundle) and I wanted to move some my reusable classes from src directory to vendor one (loaded via composer PSR-4).
So I created a package (new git repository), added classes there (into src directory), added PSR-4 configuration into package's composer.json like this:
"autoload": {
"psr-4": { "\\": "src/" }
}
and I ran composer require <name_of_my_package> and I dumped autoload.
All went smooth, but when I wanted to access any class from my package - for my surprise - Symfony cannot find and use any of these classes (even though they are in autoload files of composer).
I think that I need to somehow add these classes to Symfony container builder, but I am not sure how.
Thanks for all help.

Composer require-dev requireing dependencies in different packages require-dev

I have some tests that are namespaced being autoloaded in package A using
"autoload-dev": {
"psr-4": {
"Vendor\\PackageA\\PhpUnit\\": "tests/PhpUnit"
}
},
This works fine.
I have another package, package B which also have namespaced tests that require one of the namespaced tests in package A
"autoload-dev": {
"psr-4": {
"Vendor\\PackageB\\PhpUnit\\": "tests/PhpUnit"
}
},
However, when I try and include the file in Package B, the class is not found
use Vendor\PackageA\PhpUnit\MyTestFromA;
class MyTestFromB extends MyTestFromA
{
Making me think that the autoload-dev stuff from other packages is not being loaded.
PHP Fatal error: Class 'Vendor\PackageA\PhpUnit\MyTestFromA' not found in /full/path/to/PackageBClass.php on line 3
When I try and import a file that is being autoloaded using autoload from package B rather than autoload-dev, I get no error.
How can I overcome this?
Part of me is thinking to make a package just for the tests and have it autoloaded in both without autoload-dev but I want to confirm first.
Solution: Composer autoload-dev does not work.
Take a look at the docs. It says: "autoload-dev (root only)". root only means it only applies to the root package. As you included the package, the shown composer.json file is not the root package and the autoload-dev section is thus ignored.

PHPUnit class not found in test

I have an existing PHP project. I need to test a class MyClass. I created a test under the same package.
When running with IntelliJ 2016.1 I get error Fatal error: Class 'MyClass' not found in
This is example of the test
<?php
class MyClassTest extends PHPUnit_Framework_TestCase
In Run Configuration I entered Custom working directory as the Composer vendor package.
PHPUnit is loaded from the include path
Under Settings->PHP I added the phpunit Composer package and my class package to the include path.
Under Settings->PHP->PHPUnit default config file and default bootstrap are unchecked.
What else should I configure so I can run MyClassTest without errors and test the class MyClass?
If the class is part of a composer package, or you use the composer autoloader for your project files, you can set up vendor/autoload.php as bootstrap file.
If you have set up a custom autoloader for project classes, you need to create your own boostrap.php file for PHPUnit and register the autoloader there.

Custom composer autoload directory, loading multiple packages

Im developing a group of packag within a Laravel 5 app.
all the packages have the following structure.
Folder structure:
Vendor/Package/src
composer.json
"autoload": {
"psr-4": {
"Vendor\\Package\\": "src/"
}
},
I would like to be able to have a separate folder outside of the vendors/ folder that contains the packages, under development. As they are the basis for the application. NOTE: These packages will not be downloaded through composer, just auto-loaded with composer.
e.g.
\Laravel-App
\app
\bootstrap
\config
\database
\public
...
\src
The \src directory containing all the packages mentioned above.
It's not relevant that there is a composer.json inside each package, because the are not managed by composer (, yet). Their autoload definition will only be used for an autoload dump by Composer, if the package is installed/updated.
Here there are not handled by Composer, just sitting around in a folder.
There are multiple ways of solving the autoloading problem.
You could:
use the PHP autoload functions to register your own autoloader (during bootstrap)
use Laravel's Autoloader to add the path to these packages (during app bootstrap, somewhere after laravel autoloader init)
use Composer's Autoloader to add the paths to these packages (during app bootstrap, after Composer init)
but the easiest way would be to define the mapping in the autoload section of the composer.json file of your main application. Then they will be included in the dumped autoloader, after install or update of your Composer managed dependencies.

Categories