TYPO3 7.6 - How can I autoload composer packages? - php

I've created a TYPO3 7.6 extension with the extension builder. Now I want to add and autoload composer installed packages. I read that ext_autoload.php won't be loaded since TYPO3 version 7 in general. For this you should use the ext_emconf.php OR the composer.json in the root path of your extension.
So I've setup the following composer.json and installed it with the composer.phar.
{
"require": {
"hybridauth/hybridauth": "v3.0.0-rc.1"
},
"config": {
"vendor-dir": "Vendor"
},
"autoload": {
"psr-4": {
"Vendor\\Package\\": "Classes"
},
"classmap": [
"Vendor/hybridauth/hybridauth/src"
]
}
}
Now there is the Vendor/hybridauth/hybridauth/src as expected but the autoloader of TYPO3 doesn't find for example the Vendor/hybridauth/hybridauth/src/Hybridauth.php defined class Hybridauth. I've checked it with:
\TYPO3\CMS\Extbase\Utility\DebuggerUtility::var_dump(get_declared_classes());
What did I miss? What do I have to add/change/rethink?

Your json file content like below.
{
"require": {
"hybridauth/hybridauth": "v3.0.0-rc.1"
},
"config": {
"vendor-dir": "Vendor"
},
"autoload": {
"psr-4": {
"vendor\\extensionName\\": "Classes"
},
"classmap": [
"Vendor/hybridauth/hybridauth/src/"
],
"files": [
"Vendor/hybridauth/hybridauth/src/Hybridauth.php"
]
}
}

It was my fault. TYPO3 already autoloads already the psr-4 called environments. But I had to have add the autoloader of Hybridauth itself as well like:
<?php
namespace Vendor\Package\Service;
require_once dirname(dirname(dirname(__FILE__))) . '/Vendor/autoload.php';
class SomeService extends \TYPO3\CMS\Sv\AbstractAuthenticationService{
// ...
}

Related

Laravel modules in subfolder and composer

I'm using Laravel modules by nwidart and I want to create module in subfolder. According issue on its Github this is not possible but is possible to change paths in composer.json. So I've created new module Role2 and moved it to subfolder Administration. Then I've changed module's composer.json:
{
"name": "nwidart/role2",
"description": "",
"authors": [
{
"name": "Nicolas Widart",
"email": "n.widart#gmail.com"
}
],
"extra": {
"laravel": {
"providers": [
"Modules\\Administration\\Role2\\Providers\\Role2ServiceProvider"
],
"aliases": {
}
}
},
"autoload": {
"psr-4": {
"Modules\\Administration\\Role2\\": ""
}
}
}
But after command composer dumpautoload I see nothing in command php artisan module:list and also in vendor/composer/autoload_psr4.php there is no path to this module. What I'm doing wrong or what is missing to generate multiple composer (one in root and one in module)? Or is there some simple option to have module in subfolder?
There is what I have in root composer.json:
...
"autoload": {
"classmap": [
"database",
"vendor",
"app/Easyk"
],
"exclude-from-classmap": [
"vendor/swiftmailer"
],
"psr-4": {
"App\\": "app",
"Modules\\": "Modules",
"Easyk\\": "app/Easyk",
"Asipem\\OAuth2ClientManagement\\": "packages/asipem/oauth2-client-management/src",
"Asipem\\CAPRequestDispatcher\\": "packages/asipem/cap-request-dispatcher/src"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
],
"psr-4": {
"Tests\\Unit\\": "tests/unit"
}
},
...
autoload -> psr-4 key from composer.json is used to define psr-4 root namespaces for the application.
What happens is you define a namespace and the directory location for Autoloader to look for the files under that namespace.
As in your root composer.json
"psr-4": {
"App\\": "app",
"Modules\\": "Modules",
"Easyk\\": "app/Easyk",
"Asipem\\OAuth2ClientManagement\\": "packages/asipem/oauth2-client-management/src",
"Asipem\\CAPRequestDispatcher\\": "packages/asipem/cap-request-dispatcher/src"
}
App is the base namespace and app is its base directory. By defining it, you tell autoloader that all files with namespace App will be residing inside the directory 'app'. After that, Autoloader will traverse the app directory in the PSR-4 way for file path following the namespace trail after base namespace App.
For example: Controller App\Controller\ExampleController will tell Autoloader that the class defination will be found at app/Controller/ExampleController.
So you need to specify the base directory location of your module against the namespace Modules\\Administration\\Role2\\ which is the base source directory of your module folder.
Hope it helped.

Class not being autoloaded

I've made a little library for myself and I'm trying to autoload it into my laravel project, it installs fine but whenever I try to use the class it simply says it's not found.
I've checked all the classmap files in vendor/composer and it doesn't seem to be in any of them.
This is my composer.json for my lib:
{
"name": "my-user/aspect-parser",
"version": "1.0.0",
"type": "package",
"require": {
"nesbot/carbon": "^1.22"
},
"autoload": {
"psr-4": {
"AspectParser\\": "src/"
}
}
}
My file structure is:
AspectParser
src
Parser.php
It was an issue with the type in composer.json. I've changed it to library and it adds to the autoload classmap.

Add an external library to symfony

I'm trying to add an external library to symfony.
I've tried this on the app/autoload.php:
$loader->add('LibCokeId',__DIR__ . '/../vendor/libcokeid/libcokeid/lib');
However when I try to use it in a controller:
use libCokeId\LibCokeId
Libcokeid::init()
I get the miss use statement error.
Any help?
In the situation where you have a library that doesn't use composer and you can't retrieve it from packagist, you can manipulate the Composer autoload.
Simply add the class in the composer.json files, as example:
"autoload": {
"psr-0": { "": "src/" },
"files": [
"vendor/folder/my_custom_lib/myFiles.php",
"vendor/libcokeid/libcokeid/lib/libCokeId/LibCokeId.php"
]
},
OR you can Autoload the whole folder in composer.json:
"autoload": {
"psr-0": { "": "src/" },
"classmap": [
"vendor/libcokeid/libcokeid/lib"
],
},
Remember to make a composer install after setting this.
Hope this help.

Using Composer Autoloader with PSR-4

I'm looking at examples, and I cannot get my code to work.
Directory Structure
app
src
company
FileExport
FileExport.php
FileExportInterface.php
Validator
vendor
...
My composer.json
"require": {
"monolog/monolog": "1.9.1",
"ilya/belt": "2.1.1"
},
"autoload": {
"psr-4": {"Company\\": "src"}
}
Namespace is Company\FileExport.
Classes in vendor work fine, but not mine. I've run composer update as well.
Your autoload should look like so
"autoload": {
"psr-4": {"Company\\": "src/company/"}
}

Composer package class not found

I'm trying to create a composer package but I'm getting a class not found error. I'm using Laravel 4 but I'm trying to create a generic PHP package.
I created the package in my vendor/ directory.
vendor/
theninthnode/
defaqto/
src/
Defaqto.php
vendor/
composer.json
Here is my composer.json file
{
"name": "theninthnode/defaqto",
"description": "",
"authors": [
{
"name": "Billy Jones",
"email": "billyjones26#gmail.com"
}
],
"minimum-stability": "dev",
"require": {
"php": ">=5.3.0"
},
"autoload": {
"classmap": ["src"]
}
}
And my class:
<?php
class Defaqto
{
....
}
When I try to call the package class from my app like so
$defaqto = new Defaqto();
I just get
Class 'Defaqto' not found
I've tried composer dump-autoload from within my package and also my applications root.
I also tried adding theninthnode/defaqto to my root composer.json file.
Am I missing something?

Categories