using composer packages in extension.

Discussion forum for Extension Writers regarding Extension Development.
User avatar
MattF
Extensions Development Coordinator
Extensions Development Coordinator
Posts: 5823
Joined: Sat Jan 17, 2009 9:37 am
Location: Los Angeles, CA
Name: Matt Friedman

Re: using composer packages in extension.

Post by MattF »

david63 wrote:So far there have not been many extensions that have needed these dependencies and we have got ourselves (rightly or wrongly) into using Git as a means of accessing extensions in development.
Yep... It's dead wrong for anybody to be getting extensions directly from a git repository. Remember git is a repository for a project under development where other people can collaborate with the developer. As the download links usually say, it's the "source code".

Users should only ever install a released product. That's why, as Paul suggests, ext developers can make releases on their repo, and they can even upload the properly packaged distribution zips to those releases, so users download the right thing, easy to install, without having to tell them to rename folders or worse, run build scripts.
david63 wrote:I agree that expecting many users to "compile" with composer (most probably do not know what Composer is) in order to test and develop extensions is not ideal. As I see it there are three options:

1. Include vendor in Git
2. Don't include vendor in Git and expect users to use Composer
3. Have a download at some other place that includes vendor

Composer is easy to install in Xampp
Never do #1. As for #2 well, sure people should do that if they want to collaborate with you on your project.

#3 is the way to go, using Git tags and releases, just as we do:
https://github.com/phpbb-extensions/col ... s/releases
https://github.com/phpbb-extensions/pages/releases
Formerly known as VSEMy ExtensionsPlease do not PM me for support.
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: using composer packages in extension.

Post by 3Di »

#3 = That's what I am doing since not so much of time ago, though.

Makes things clear to me and users.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
User avatar
RMcGirr83
Former Team Member
Posts: 21987
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: using composer packages in extension.

Post by RMcGirr83 »

Well I got it to work but there is now a ton of stuff in the vendor directory. :?
Noname.jpg
Is that normal? I ran composer update when in the directory of the extension.

Also, not for nothing, but I looked at the releases for board3 portal and it would seem the releases don't have a composer.lock file? It is just a tad bit confusing to me.
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beerImage
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: using composer packages in extension.

Post by david63 »

I didn't get all that when I used it - that looks to me as if it has all phpBB packages in your extension.

I put the package that I needed into the require section of composer.json and then ran composer install from the command line of the extension's root.

What have you in the require section?
David
Remember: You only know what you know and - you don't know what you don't know!

I now no longer support any of my extensions but they will start to become available here
User avatar
RMcGirr83
Former Team Member
Posts: 21987
Joined: Wed Jun 22, 2005 4:33 pm
Location: Your display
Name: Rich McGirr

Re: using composer packages in extension.

Post by RMcGirr83 »

Found the culprit. Had require-dev epv in the composer.json file so I blame paul (as usual).
Former Modifications/Extensions Team Member | My extensions | github | All requests for support via PM will be ignored
Appreciate the extensions/mods/support then buy me a beerImage
MitjaStachowiak
Registered User
Posts: 38
Joined: Mon Dec 30, 2019 7:29 pm
Location: Reinheim, Germany
Contact:

Re: using composer packages in extension.

Post by MitjaStachowiak »

I added a vendor-package as described here to my extension but still cannot find out, how to load it. I'm using openpgp-php which relies on phpseclib. I downloaded phpseclib to myextension/vendor/phpseclib, added

Code: Select all

  "require": {
    "phpseclib/phpseclib": "*",
  },
to myextension/composer.json, cleared the cache and re-enabled my extension. But openpgp-php still cannot find the seclib-classes. This are the code-lines, where it crashes:

Code: Select all

use phpseclib\Crypt\RSA as Crypt_RSA;
use phpseclib\Math\BigInteger as Math_BigInteger;
define('CRYPT_RSA_ENCRYPTION_PKCS1', Crypt_RSA::ENCRYPTION_PKCS1);
I think, I am missing some (auto)load-script, that tells composer to load seclib before openpgp-php is used... This is the point, where I don't know, how to proceed.

The examples of openpgp-php are written for a wordpress-extension. This runs an autoload script before openpgp-php which looks like

Code: Select all

<?php

// autoload_real.php @generated by Composer

class ComposerAutoloaderInitf0736cd9a4e153527eb34c4b5acb5078
{
    private static $loader;

    public static function loadClassLoader($class)
    {
        if ('Composer\Autoload\ClassLoader' === $class) {
            require __DIR__ . '/ClassLoader.php';
        }
    }

    public static function getLoader()
    {
        if (null !== self::$loader) {
            return self::$loader;
        }

        spl_autoload_register(array('ComposerAutoloaderInitf0736cd9a4e153527eb34c4b5acb5078', 'loadClassLoader'), true, true);
        self::$loader = $loader = new \Composer\Autoload\ClassLoader();
        spl_autoload_unregister(array('ComposerAutoloaderInitf0736cd9a4e153527eb34c4b5acb5078', 'loadClassLoader'));

        $useStaticLoader = PHP_VERSION_ID >= 50600 && !defined('HHVM_VERSION') && (!function_exists('zend_loader_file_encoded') || !zend_loader_file_encoded());
        if ($useStaticLoader) {
            require_once __DIR__ . '/autoload_static.php';

            call_user_func(\Composer\Autoload\ComposerStaticInitf0736cd9a4e153527eb34c4b5acb5078::getInitializer($loader));
        } else {
            $map = require __DIR__ . '/autoload_namespaces.php';
            foreach ($map as $namespace => $path) {
                $loader->set($namespace, $path);
            }

            $map = require __DIR__ . '/autoload_psr4.php';
            foreach ($map as $namespace => $path) {
                $loader->setPsr4($namespace, $path);
            }

            $classMap = require __DIR__ . '/autoload_classmap.php';
            if ($classMap) {
                $loader->addClassMap($classMap);
            }
        }

        $loader->register(true);

        if ($useStaticLoader) {
            $includeFiles = Composer\Autoload\ComposerStaticInitf0736cd9a4e153527eb34c4b5acb5078::$files;
        } else {
            $includeFiles = require __DIR__ . '/autoload_files.php';
        }
        foreach ($includeFiles as $fileIdentifier => $file) {
            composerRequiref0736cd9a4e153527eb34c4b5acb5078($fileIdentifier, $file);
        }

        return $loader;
    }
}

function composerRequiref0736cd9a4e153527eb34c4b5acb5078($fileIdentifier, $file)
{
    if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) {
        require $file;

        $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true;
    }
}
I don't understand this script :shock: What would be the equivalent for a phpbb-extension?
User avatar
3Di
I've Been Banned!
Posts: 17538
Joined: Mon Apr 04, 2005 11:09 pm
Location: I'm with Ukraine 🇺🇦
Name: Marco
Contact:

Re: using composer packages in extension.

Post by 3Di »

MitjaStachowiak wrote: Fri Apr 02, 2021 9:51 pm .... to myextension/composer.json, cleared the cache and re-enabled my extension.
Before to install the extension you should installa that dependency via CLI, with
composer install

It will create the vendor folder and the .lock file, and the autoloader.

Then you can use something like $blah = /new blah/blah in your classes, that's basically it.
🆓 Free support for our extensions also provided here: phpBB Studio
🚀 Looking for a specific feature or alternative option? We will rock you!
Please PM me only to request paid works. Thx. Buy me a coffee -> Image
My development's activity º PhpStorm's proud user º Extensions, Scripts, MOD porting, Update/Upgrades
MitjaStachowiak
Registered User
Posts: 38
Joined: Mon Dec 30, 2019 7:29 pm
Location: Reinheim, Germany
Contact:

Re: using composer packages in extension.

Post by MitjaStachowiak »

Again, I didn't understand what to to. I tried $ /opt/lampp/bin/php bin/phpbbcli.php composer install which gives me

Command "composer" is not defined.

And why should I install this before my extension? My extension already is installed and works fine. I just want to add this openpgp-php to work around servers missing gnupg. My extension is in development...
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28451
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: using composer packages in extension.

Post by Paul »

you should just run composer install, it is not part of phpBB's cli. If you don't have composer installed, you should install it: https://getcomposer.org/doc/00-intro.md
You need to run that command to install the dependency, without it, the code for openpgp-php isn't there, and as such it won't work without running that command.
MitjaStachowiak
Registered User
Posts: 38
Joined: Mon Dec 30, 2019 7:29 pm
Location: Reinheim, Germany
Contact:

Re: using composer packages in extension.

Post by MitjaStachowiak »

So all the codes at phpBB/vendor/composer are just a minimal version, that manages the loading of installed packages but not the installation (which means creation of the autoload-files, etc.)?

If I run composer install outside of phpBB's CLI - how can it know, that there is a new package in my extension's sub-folder, for which it should create the load scripts?
Will it search for all vendor folders on my PC?

And finally - there are several vendor folders inside phpBB: The main one and the extension's ones. Is there a special configuration file, that holds this together? And how is this updated, when someone simply installs the release of my extension? For this purpose it should not be necessary to work with composer...
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28451
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: using composer packages in extension.

Post by Paul »

MitjaStachowiak wrote: Mon Apr 05, 2021 10:47 am So all the codes at phpBB/vendor/composer are just a minimal version, that manages the loading of installed packages but not the installation (which means creation of the autoload-files, etc.)?
the vendor folder for phpBB itself just contains the files required by phpBB. The dependencies each extension add's are in the extensions vendor folder.
If I run composer install outside of phpBB's CLI - how can it know, that there is a new package in my extension's sub-folder, for which it should create the load scripts?
Will it search for all vendor folders on my PC?
phpBB's CLI has nothing to do with vendor folders. Composer is something completely different, and phpBB only uses it as dependency manager. Extension can use it as dependency manager as well, but they are completely separate.
And finally - there are several vendor folders inside phpBB: The main one and the extension's ones. Is there a special configuration file, that holds this together? And how is this updated, when someone simply installs the release of my extension? For this purpose it should not be necessary to work with composer...
phpBB automaticly detects if your extension has a vendor folder, and will automaticly load the required files. There is no need to do any configuration.
You will need to include the vendor/ folder in your extensions zip when submitting your extension to the CDB, so the user doesn't need to run the composer install command.
MitjaStachowiak
Registered User
Posts: 38
Joined: Mon Dec 30, 2019 7:29 pm
Location: Reinheim, Germany
Contact:

Re: using composer packages in extension.

Post by MitjaStachowiak »

Okay, running /opt/lampp/bin/php /composerpath/composer.phar install in the directory of my extension seems to install openpgp-php and phpseclib. But also a ton of other libraries like symfony, psr and whatever. This cannot be correct...

This is my complete composer.json-file:

Code: Select all

{
  "name": "msta/emailontopic",
  "type": "phpbb-extension",
  "description": "Allows sending posts as mailcopies to specific users.",
  "homepage": "http://mitjastachowiak.de/components/php/phpbb_emailontopic",
  "version": "2.0.1",
  "time": "2016-05-05",
  "license": "read license.txt",
  "authors": [{
    "name": "Mitja Stachowiak",
    "email": "info@mitjastachowiak.de",
    "homepage": "http://www.mitjastachowiak.de/",
    "role": "Extension Author"
  }],
  "require": {
    "php": ">=5.3.3",
    "composer/installers": "~1.0",
    "phpseclib/phpseclib": "*",
    "singpolyma/openpgp-php": "*"
  },
  "require-dev": {
    "phpbb/epv": "dev-master"
  },
  "extra": {
    "display-name": "Email On Topic",
    "soft-require": {
      "phpbb/phpbb": ">=3.1.0-RC2,<3.2@dev"
    },
    "version-check": {
      "host": "mitjastachowiak.de",
      "directory": "/components/php/emailontopic",
      "filename": "version.json"
    }
  }
}
I've just added the lines for openpgp and seclib, the rest is taken from a "Hello Word"-Extension. Isn't this correct?
User avatar
david63
Registered User
Posts: 20646
Joined: Thu Dec 19, 2002 8:08 am

Re: using composer packages in extension.

Post by david63 »

You do not install composer in your extension, you install composer on your server. Then you run composer install mycomposerpackage for your extension which will install the relevant package in the vendor folder of your extension and create a composer.lock file in the root of your extension
David
Remember: You only know what you know and - you don't know what you don't know!

I now no longer support any of my extensions but they will start to become available here
Paul
Infrastructure Team Leader
Infrastructure Team Leader
Posts: 28451
Joined: Sat Dec 04, 2004 3:44 pm
Location: The netherlands.
Name: Paul Sohier
Contact:

Re: using composer packages in extension.

Post by Paul »

As you have phpbb/epv as development dependency, which depends on some symfony packages, that sounds fine. Not sure if the other packages have symfony dependencies as well, but it is not something to worry about.

If you want to submit it to the cdb for release you should run composer with --no-dev.
david63 wrote: Mon Apr 05, 2021 5:50 pm You do not install composer in your extension, you install composer on your server. Then you run composer install mycomposerpackage for your extension which will install the relevant package in the vendor folder of your extension and create a composer.lock file in the root of your extension
the command he ran was perfectly fine. It is also not needed to run it on a server during development.
MitjaStachowiak
Registered User
Posts: 38
Joined: Mon Dec 30, 2019 7:29 pm
Location: Reinheim, Germany
Contact:

Re: using composer packages in extension.

Post by MitjaStachowiak »

...composer.phar --no-dev install singpolyma/openpgp-php fails with the error
Invalid argument singpolyma/openpgp-php. Use "composer require singpolyma/openpgp-php" instead to add packages to your composer.json.
So I tried the require option composer.phar --no-dev require singpolyma/openpgp-php but this gives me

[Symfony\Component\Console\Exception\RuntimeException]
The "--no-dev" option does not exist.

Again I cannot find helpful information on this :roll:
Without --no-dev, it installs again all the unwanted dependencies. I just need openpgp-php which requires phpseclib which requires nothing else.

Well, I removed "phpbb/epv": "dev-master" from my composer.json and then it only installs the required libraries and one composer-folder, which maybe includes the autoloader.
Post Reply

Return to “Extension Writers Discussion”