PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, January 21, 2022

[FIXED] Composer: library which requires a library

 January 21, 2022     composer-php, php     No comments   

Issue

I am trying to create a PHP library which includes other libraries, and I bet I'm missing something fundamental.

Using the AWS PHP SDK as a guide, I'd like to create a library which, after installing with Composer, requires other libraries, yet the entire scope of classes (both from the current library, and required libraries) all become available simply by using require 'vendor/autoload.php;'.

What are the basic requirements to set this up? Is it a matter of configuring composer.json, namespacing in a particular way, or both?


Solution

What you're describing is exactly Composer's main purpose - the definition of a package of code that may require and implement other packages.

Using the AWS SDK as a guide, if you look at the composer.json file, which provides all of the Composer configuration information, you'll see two require blocks, one labeled require and one labeled require-dev:

"require": {
    "php": ">=5.5",
    "guzzlehttp/guzzle": "^5.3.1|^6.2.1",
    "guzzlehttp/psr7": "^1.4.1",
    "guzzlehttp/promises": "~1.0",
    "mtdowling/jmespath.php": "~2.2"
},
"require-dev": {
    "ext-openssl": "*",
    "ext-pcre": "*",
    "ext-spl": "*",
    "ext-json": "*",
    "ext-dom": "*",
    "ext-simplexml": "*",
    "phpunit/phpunit": "^4.8.35|^5.4.0",
    "behat/behat": "~3.0",
    "doctrine/cache": "~1.4",
    "aws/aws-php-sns-message-validator": "~1.0",
    "nette/neon": "^2.3",
    "andrewsville/php-token-reflection": "^1.4",
    "psr/cache": "^1.0"
},

This is how you define what other packages/libraries your library depends upon. The require section lists all other libraries that must be installed when your library is installed. The require-dev section lists libraries that may only be necessary when you are working in a development environment, and are not needed in your production environment.

When you specify other libraries that are required, Composer will install your library, and then go out and also require the libraries your library requires (and then the libraries those libraries require, and so on and so on).

Also included with the libraries to include, you'll notice that the version numbers are also included, to ensure compatibility.

The easiest way to add new dependencies I find is on the command line, with the composer require command, documented here: https://getcomposer.org/doc/03-cli.md#require. The command helps you search for the package you want if you don't know it exactly, and can resolve the latest version for you automatically (which you can override if you need/want to).

If you wish to require a development-only dependency, add the --dev flag when running the command.

Using this command, Composer will automatically update your composer.json file, pull down the dependency onto the local machine, and update your autoloader.

You should never need to do anything more than require_once vendor/autoload.php to ensure dependencies can be autoloaded - Composer will do all the legwork of setting up the autoloader so you don't have to, and keep everything up to date as new dependencies are added.

Here's the complete documentation on the composer.json schema: https://getcomposer.org/doc/04-schema.md. You will want to have a composer.json config file in the root of project, so you can configure composer for your project (and any others that require your library later). If you don't have one, you can use the composer init command to interactively create one. Documentation on that command is available here: https://getcomposer.org/doc/03-cli.md#init

And here's their basic usage guide, in case you haven't gone through it already: https://getcomposer.org/doc/01-basic-usage.md



Answered By - stratedge
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing