Lucas
Lucas

Reputation: 100

how to add a PHP library to TYPO3 extension without namespace?

I'm tyring to implement a custom TYPO3 extension to execute some php code. With my main class "hello world!" ist already working and i understand the use of namespaces. But now I found a php Library that suits my needs. I pasted the lib folder in the "Classes" folder of my extension. But now I'm getting class not found errors because none of the lib classes have a namespace.

Unfortunately I couldn't find any tutorial/doc on how to add a library to a typo3 extension while dynamically adding a namespace to every class. I tried to override every class with a namespace but somehow this cant be the solution

here is a sample of my Main class that is working but as soon as I try to call "ServiceMailman" i get namespace error, well, because they have none

namespace Htwg\GiMailman;

require_once 'Service/ServiceMailman.php';

class GiMailman{
    public function getMailinglists() {
        $mm = new ServiceMailman('http://localhost', '', '');
    }
}   

I'm looking for a way to add a php library to the "Classes" folder without adding a namespace to every library class.

Update: I installed the library on an externel path and added it to the composer.json in the classmap entry:

"autoload": {
    "psr-4": {
        "Htwg\\GiMailman\\": "Classes/"
    },
    "classmap": ["/opt/lampp/lib/php/Services"]
}

and it shows up in the autoload_classmap.php:

// autoload_classmap.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    ...
    'Services_Mailman' => $baseDir . '/../../../../lib/php/Services/Mailman.php',
    'Services_Mailman_Exception' => $baseDir . '/../../../../lib/php/Services/Mailman/Exception.php',
);

But when i try to class the class in my Main php class it still can't be found:

namespace Htwg\GiMailman;

//require_once 'Services/Mailman.php';

class GiMailman{
    public function getMailinglists() {

        $mm = new \Service_Mailman('http://localhost:8001/3.1', '', 'password');

        return "getMailinglists";

    }
}

Upvotes: 0

Views: 1117

Answers (2)

Thomas Löffler
Thomas Löffler

Reputation: 6174

If you're running composer there are two ways:

  1. The library is available on https://packagist.org/ => Require it in your composer.json file
  2. The library needs to be copied into e.g. Library/ServiceMailman/. After that you set it into your composer.json in the autoload section "classmap", if no namespaces exists for this library. More: https://getcomposer.org/doc/04-schema.md#classmap (If the library has namespaces, it should be in autoload section "psr-4")

If you're not running composer and want to include it in your TYPO3 extension easily, there is a good tutorial: https://insight.helhum.io/post/148112375750/how-to-use-php-libraries-in-legacy-extensions

Upvotes: 0

Rudy Gnodde
Rudy Gnodde

Reputation: 4575

Any PHP classes that do not use namespaces are in the top level namespace. So you can use them like:

$mm = new \ServiceMailman('http://localhost', '', '');

You should not add external libraries to you Classes directory. Classes in this directory are autoloaded with the correct namespace for your extension (Vendor/ExtensionName). As external libraries have a different, or in your case no, namespace, this will cause problems. Usually we put external libraries in Resources/Private/Php/LibraryName. You will then need to require or include the library.

If you're using composer it is however better not to include external libraries inside your extension, but let composer worry about it if possible. That way you don't have to worry about autoloading (so you don't need to require or include any files manually) and any dependencies for the external library are also automatically resolved. Either require the library in your global composer.json or, if you install the extension that requires it through composer, add it to the composer.json of the extension.

Upvotes: 1

Related Questions