Skip to content
Composer Packages is a Composer plugin for getting information about installed packages in your project.
PHP HTML
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github
build
src
templates
tests
.editorconfig
.gitattributes
.gitignore
.scrutinizer.yml
.travis.yml
LICENSE
README.md
composer.json
grumphp.yml.dist
infection.json.dist
phpunit.xml.dist

README.md

Latest Stable Version GitHub stars Total Downloads Build Status Scrutinizer code quality Code Coverage Mutation testing badge License Say Thanks! Donate!

Composer Packages

Description

Composer Packages is a Composer plugin for getting information about installed packages in your project.

It could be very useful for anyone who wants to build a package discovery system, crawling the filesystem is then not needed.

Documentation

This package provides:

  • An easy way to get information about installed packages,
  • An easy way to retrieve packages that has a particular types,
  • An easy way to find the installation directory of a package.

How does it work ?

When doing a composer update or composer install, the plugin will generate classes that are going to be automatically loaded by the Composer autoload system.

Those classes contains statical information about packages that are installed in your project. Among those static data, it also contains some useful methods. The number of methods in those classes can very depending on the number of packages that are in your project.

This package idea has been inspired by the package ocramius/package-versions from the amazing Marco Pivetta.

Requirements

  • PHP >= 7.1.3

Installation

composer require drupol/composer-packages --dev

Usage

To get packages of a particular type

<?php

declare(strict_types=1);

include './vendor/autoload.php';

use ComposerPackages\Types;

// Use your IDE auto completion to list all the available methods based on your installed packages.
$packages = Types::library();

foreach ($packages as $package) {
    $package->getName(); // $package is an instance of Composer\Package\PackageInterface
}

// You can also get an array:
$packagesArray = iterator_to_array($packages);

To get a package

<?php

declare(strict_types=1);

include './vendor/autoload.php';

use ComposerPackages\Packages;
use Composer\Package\PackageInterface;

// Use your IDE auto completion to list all the available methods based on your installed packages.
$package = Packages::symfonyProcess();

// Package is an instance of Composer\Package\PackageInterface then:
$package->getName(); // To get the name.

// Find all the packages where the name starts with the letter "c".
$finder = static function (PackageInterface $package) : bool {
    return 'c' === str_split($package->getName())[0];
};

foreach (Packages::find($finder) as $package) {
    // Do something here.
}

To get an installation directory

<?php

declare(strict_types=1);

include './vendor/autoload.php';

use ComposerPackages\Directories;

// Use your IDE auto completion to list all the available methods based on your installed packages.
$directory = Directories::symfonyProcess();

Note: If composer is not already installed, you might get an error like below when using this package:

In Types.php line […]:

  Attempted to load class "ArrayLoader" from namespace "Composer\Package\Loader".
  Did you forget a "use" statement for e.g. "…\ArrayLoader", "…\ArrayLoader" or "…\ArrayLoader"?

If you do, you can explicitly require composer in your project, to ensure it's available:

composer require composer/composer

To get a package version

<?php

declare(strict_types=1);

include './vendor/autoload.php';

use ComposerPackages\Versions;

// Use your IDE auto completion to list all the available methods based on your installed packages.
$version = Versions::symfonyProcess();

Code quality and tests

Every time changes are introduced into the library, Travis CI run the tests and the benchmarks.

The library has tests written with PHPUnit.

Before each commit some inspections are executed with GrumPHP, run ./vendor/bin/grumphp run to trigger them manually.

PHPInfection is used to ensure that your code is properly tested, run composer infection to test your code.

Contributing

Feel free to contribute to this library by sending Github pull requests. I'm quite reactive :-)

You can’t perform that action at this time.