Skip to content

Instantly share code, notes, and snippets.

@DavidBruchmann
Last active October 13, 2023 17:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DavidBruchmann/1735db3fa11bad8edfa757770cbf03af to your computer and use it in GitHub Desktop.
Save DavidBruchmann/1735db3fa11bad8edfa757770cbf03af to your computer and use it in GitHub Desktop.
Combining Composer, Git repositories and Pear

Combining composer, git repositories and pear

Problem

Pear is the the old PHP-packagemanager, but most 'packages' are nowadays managed with composer on base of git-repositories.
Adding pear-packages to a composer based installation is a little challenge that shall be explained below.

Solution

Prerequisites

For getting an overview it's advised to read the whole list, before installing anything, as some details are good to know and perhaps not always mentioned in the expected context.

  • git has to be installed, in Windows the git-bash is advisable.
    Link: https://git-scm.com/downloads. The PATH-variable should include the path to git.
  • pear has to be installed like explained on this page: https://pear2.php.net/manual/en/installation.php. I followed this page: https://pear.php.net/manual/en/installation.getting.php but you also can try to do it like described here: https://pear.php.net/manual/en/installationpyrus.introduction.php.
    Before starting the installation check with pear on the CLI if it's pehaps installed already.
  • The PATH variable of the system has to include the bin-folder inside the pear directory: ~/pear/bin. In Linux the path can be added in the file ~/.profile, i.e. by adding this line in the end:
    PATH="$PATH:~/pear/bin"
    
    The path is working if pear is accepted as command. It is required to start a new bash-session after changing the PATH in the file.
    If ddev is used the confiiguration-file has to be copied to the ddev-folder: .ddev/.homeaddition/.profile or .ddev/.homeaddition/.bash_profile and then the following line has to be added:
    PATH="$PATH:~/pear/bin"
    
    Details can be found here: https://ddev.readthedocs.io/en/stable/users/extend/in-container-configuration/.
  • After having installed pear the include-path inside the file php.ini should be changed, so that it includes the path ~/pear/shared or ~/pear/shared/pear for the packages.
    Note that the packes are not sorted in vendor-folders and that the logic is different than that of management by composer. Therefore paths inside files follow also another logic, namespaces are often not used as the code is sometimes quite old.
    Note that for ddev-installations the configuration should be done inside the folder .ddev where a php.ini file can be created in .ddev/php/my-php.ini. Further explanations can be found here: https://ddev.readthedocs.io/en/stable/users/extend/customization-extendibility/#providing-custom-php-configuration-phpini.
    I had success with this content in the file:
    [PHP]
    include_path = "~/pear/share/pear";
    
  • composer has to be installed like explained on this page: https://getcomposer.org/download/. The PATH-variable should include the path to composer.

Installing pear-packages

Pear packages have to be known in the environment, therefore the environment variables for pear have been added already before.
Composer can resolve pear-packages when they are installed properly. Below will be shown how to install pear packages first and reference (require) them afterwards in a composer.json file.

Installing pear-packages in the environment

  • pear- or pecl-packages might have versions that are not clearly visible for git- and composer-users. Nevertheless versioning is / was done and the versions can be seen on the download-page for each package. The most recent version can be found also on each project's overview-page.
    Usually pear packages are installed in the folder ~/pear/shared/ if not configured otherwise.
    Note that pear-packages might have dependencies too and those are usually installed if required.
  • The desired version of a pear-package has to be installed by usage of pear, example:
    pear install Mail_mimeDecode-1.5.6
    The packages include a file composer.json, so they can be handled by composer.

Reference (require) pear packages in a composer.json file

  • The pear packages can be included as dependency in other repositories with the name in the composer.json combined with the number of the installed version:
    "require": {
        "pear/mail_mimedecode": "1.5.6"
    }
    
  • Below is an example-snippet for typical composer-packages combined with some pear-packages in a project's or or extension's composer.json-file:
     "require": {
         "typo3/cms-core": "^10.4",
         "typo3/cms-rte-ckeditor": "^10.4",
         "typo3/cms-extbase": "^10.4",
         "typo3/cms-fluid": "^10.4",
         "typo3/cms-fluid-styled-content": "^10.4",
         "pear/mail_mbox": "0.6.3",
         "pear/mail_mimedecode": "1.5.6"
     },
    
    If composer can install this project or extension, everything worked fine so far.
    Else you'll get likely error-messages that the pear-packages can't be found or never exist in the expected version.

Using pear packages in your code

Assumed you could fix any errors and the composer installation succeeded, you can use the pear- / pecl-packages by relying on the include-paths:

require_once 'Mail/Mbox.php';
require_once "Mail/mimeDecode.php";

Note that composer might load the classes too as the composer.json might include something like this:

  "autoload": {
    "psr-0": {
      "Mail": "./"
    }
  },

Therefore you could try to omitt the require()-instructions.

Final notes

  • Finally it keeps to say that more and more pear packages migth be abandoned and outdated, so it's worth it to search for different solutions.
  • Nevertheless the Pear-packages offer perhaps some code that is otherwise hard to find and it's an option to use it also in combination with git and composer.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment