Locate the installation path for any installed Composer package

Overview

mindplay/composer-locator

This Composer plugin provides a means of locating the installation path for a given Composer package name.

PHP Version Build Status

Use this to locate vendor package roots, e.g. when working with template files or other assets in a package.

It works regardless of installers affecting the individual package installation paths, and also works whether the package in question is currently the root package/project or a dependency.

You can think of this as a minimalist alternative to puli - rather than abstracting repositories and resources through a complex virtual file system, I prefer to just use the physical file system and standard PHP APIs.

Works for me. I like simple things. YMMV.

Usage

Add to your composer.json file:

{
    "require": {
        "mindplay/composer-locator": "^2"
    }
}

Running composer install or composer update will bootstrap your project with a generated class containing a registry of Composer package installation paths.

To obtain the installation path for given package:

$path = ComposerLocator::getPath("vendor/package"); // => "/path/to/vendor/package" 

If the specified package name is not found, the function throws a RuntimeException.

To check whether a given package is installed:

$is_installed = ComposerLocator::isInstalled("vendor/package"); // => (bool) true|false 

The root project package doesn't necessarily have a package name - in that case, or in other cases where you need the project root path, you can obtain it directly:

$path = ComposerLocator::getRootPath(); // => "/path/to/project/root" 

You can also get a list of all installed packages via ComposerLocator::getPackages(), or obtain the full map of vendor/package names to absolute root paths via ComposerLocator::getPaths().

Why?

Needing to know the root path of a package installation folder is quite a common requirement, such as when you need to specify paths to template files or other assets.

The problem is that Composer itself offers no simple and reliable way to do that.

You can use reflection to get the path to a known class or interface from the package, and then dirname() up from your src folder to the package installation root, but that approach is pretty clumsy and creates random dependencies on arbitrary class/interface-names, just for the sake of locating a package root.

Even if you know the path of the vendor root folder, and the {vendor}/{package} folder name convention, there is no guarantee that's always where packages are installed - something like composer-installers or other custom installers could affect the installation paths.

Also, under test, when a package is the root/project package, of course the assumption about the vendor folder is always going to be wrong.

Comments
  • Use `static` instead of `self` to allow extension in tests

    Use `static` instead of `self` to allow extension in tests

    I'm trying to use this package in another package but the static class/methods make it difficult to test it (i.e. simulate a package being installed in Composer to test what happens then).

    A workaround I see is using static instead of self, that would allow to extend the class like this:

    class FakeComposerLocator extends ComposerLocator
    {
        public static $paths = [];
    }
    

    Then in my tests I can make my system use FakeComposerLocator instead of ComposerLocator and I can register fake packages like this:

    FakeComposerLocator::$paths = ['foo/bar'];
    

    Not the cleanest solution, but better than nothing?

    opened by mnapoli 6
  • Support for composer 2.x

    Support for composer 2.x

    Like title says, this adds composer 2.x support to the package.

    Note: You can use #7 to merge first and see if everything is ok with this pull request.

    opened by MekDrop 3
  • Use GitHub actions for tests instead of Travis

    Use GitHub actions for tests instead of Travis

    I don't know how to upgrade travis to use different composer versions in tests, so I have changed tests to use GitHub Actions.

    Also, now tests on all PHP versions (including 8.0 that will be released in december)

    I hope this will be usefull.

    opened by MekDrop 3
  • Stream wrapper?

    Stream wrapper?

    Hi! I wonder if it would make sense to create a stream wrapper to access files through URIs like composer://vendor/package/file.json? Or is it out of the scope of this project? How do you use this project yourself?

    opened by mnapoli 2
  • Remove exception on uninstall

    Remove exception on uninstall

    Uninstalling the plugin causes the exception to be thrown. Not sure if there's a way to tell if the plugin is being uninstalled, and so only skip in that case.

    opened by thewilkybarkid 1
  • Add support for stream-wrapper

    Add support for stream-wrapper

    See documentation for the stream-wrapper feature in the README diff below.

    Upgraded Symfony dependencies require PHP 7.2: support for older versions (PHP 5.4-7.1) has been removed.

    I've proposed this become a built-in Composer feature.

    For discussion:

    • Is composer:// an appropriate name for the stream-wrapper? Something like packages:// might be more neutral.

    • Should we alter the local file-stats and prevent opening of files in write-mode? Opening files in the vendor folder in write-mode probably doesn't make a lot of sense, but isn't really a security issue either: you could already locate those files by other means. This would be more of a helpful sanity check to avoid accidental writes.

    Try it out:

    You can try it out by using a temporary version constraint in your composer.json , such as: dev-stream-wrapper as 1.2.0.

    opened by mindplay-dk 5
Releases(2.1.4)
Owner
Rasmus Schultz
Rasmus Schultz
Yii Aliases - store path aliases, i.e. short name representing a long path

Yii Aliases The package aim is to store path aliases, i.e. short name representing a long path (a file path, a URL, etc.). Path alias value may have a

Yii Software 20 Nov 8, 2022
It is a web application that allows you to analyze electricity consumption and savings resulting from the installation of a photovoltaic installation.

It is a web application that allows you to analyze electricity consumption and savings resulting from the installation of a photovoltaic installation.

Studenckie Koło Naukowe Informatyków 10 Feb 9, 2022
Composer Repository Manager for selling Magento 2 extension and offering composer installation for ordered packages.

Magento 2 Composer Repository Credits We got inspired by https://github.com/Genmato. Composer Repository for Magento 2 This extension works as a Magen

EAdesign 18 Dec 16, 2021
Installed composer packages info

PackageInfo This package was highly inspired from ocramius/package-versions I needed some methods to read data from the composer.lock file fast...this

Martin Keckeis 7 Jun 2, 2022
Composer addon to efficiently get installed packages' version numbers

Package Versions composer/package-versions-deprecated is a fully-compatible fork of ocramius/package-versions which provides compatibility with Compos

Composer 1.4k Dec 27, 2022
Checks prefer-lowest installation for actually defined min versions in composer.json

Composer Prefer Lowest Validator This validator will strictly compare the specified minimum versions of your composer.json with the ones actually used

Mark Scherer 17 Aug 7, 2022
This composer installer plugin allows for easy installation of PHP_CodeSniffer coding standards

PHP_CodeSniffer Standards Composer Installer Plugin This composer installer plugin allows for easy installation of PHP_CodeSniffer coding standards (r

PHPCSStandards 393 Feb 25, 2022
Jetstrap is a lightweight laravel 8 package that focuses on the VIEW side of Jetstream / Breeze package installed in your Laravel application

A Laravel 8 package to easily switch TailwindCSS resources generated by Laravel Jetstream and Breeze to Bootstrap 4.

null 686 Dec 28, 2022
Shows the path of each blade file loaded in a template

Laravel Tracer Tracer shows the paths of all the Blade files that are loaded into your templates. This could be very convenient for a number of reason

Appstract 97 Oct 1, 2022
Manage meta data based on URL path within your app.

Laravel SEO Manager This package provides simple functionality to manage SEO tags based on URL path within your Laravel application. You can put the U

Michael Rubel 20 Oct 20, 2022
Shortest Path - have a function ShortestPath (strArr) take strArr which will be an array of strings which models a non-looping Graph.

Have the function ShortestPath(strArr) take strArr which will be an array of strings which models a non-looping Graph

null 1 Feb 5, 2022
Magento 2 extension. Solution for set Url of Category without Parent path.

Magento 2 extension. Solution for set Url of Category without Parent path.

Alex S 1 Jul 28, 2022
A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.

A Magento 2 module that allows for creating discrete PDP (Product Detail Pages) page layouts for customers landing on the site from a PPC (Pay Per Click) link/ad by allowing routing to the same pages using a `/ppc/` prefix in the URL path.

null 16 Nov 11, 2022
salah eddine bendyab 18 Aug 17, 2021
⚙️ A Laravel package to decompose your installed packages, their dependencies, your app & server environment

Introduction Laravel Decomposer decomposes and lists all the installed packages and their dependencies along with the Laravel & the Server environment

LUBUS 513 Dec 30, 2022
This component provides a collection of functions/classes using the symfony/intl package when the Intl extension is not installed.

Symfony Polyfill / Intl: ICU This package provides fallback implementations when the Intl extension is not installed. It is limited to the "en" locale

Symfony 2.4k Jan 6, 2023
Allows installing Drupal extensions event if not compatible with installed drupal/core package

mglaman/composer-drupal-lenient Lenient with it, Drupal 10 with it. Why? The Drupal community introduced a lenient Composer facade that modified the d

Matt Glaman 14 Dec 18, 2022
Secure package for WP CLI, built to provide an easier way of securing your WordPress installation

wp-cli/secure-command Official website: Hackthewp.com Manages common security aspects of WordPress. Supports nginx and Apache. Basic Usage This packag

Igor Hrcek 68 Dec 27, 2022
A CLI tool to check whether a specific composer package uses imported symbols that aren't part of its direct composer dependencies

A CLI tool to analyze composer dependencies and verify that no unknown symbols are used in the sources of a package. This will prevent you from using "soft" dependencies that are not defined within your composer.json require section.

Matthias Glaub 722 Dec 30, 2022