Create an event / plugin for Composer

Asked

Viewed 96 times

1

I have developed a Webserver Portable and am also creating a Portable console to use Composer.

I have a problem. I need to create a plugin for Composer.

I need that when downloading any package with Composer, it edits the "scripts" of the composer.json of that package, so that it works on the Portable console.

When downloading Laravel, for example:

Original Composer.json:

{
    "name": "laravel/laravel",
    ...
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        ...
    },
    ...
}

Composer.json edited by the plugin:

{
    "name": "laravel/laravel",
    ...
    "scripts": {
        "post-root-package-install": [
            "F:/portable_php_path/php.exe -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        ...
    },
    ...
}
  • Note that a physical path was generated for php.exe, because in the Portable version, it could be in any path.

(My question is about creating the Composer plugin. I have no problem editing the.json composer with PHP)

I read the tutorial to create a plugin on the Composer site, but I got confused. (https://getcomposer.org/doc/articles/plugins.md)

If you have another way to do this, it’s also interesting. I accept other suggestions and ideas.

I thank anyone who can help.

1 answer

1

When running Composer via command line PHP is probably already in variable PATH of the operating system, then it is not necessary to pass the full PHP path:

"F:/portable_php_path/php.exe -r \"file_exists('.env') || copy('.env.example', '.env');\""

Just call it that:

 "php -r \"file_exists('.env') || copy('.env.example', '.env');\""

This is because Composer is written in PHP and depends on it, so the CLI interface will always be available when using Composer

To create PACKAGES for Composer see this reply:

Of course you can use other types of repositories, such as Bitbucket and even repositories of your own, but if something like this specifies that it will be easier to explain in the answer.

PSR-0, PSR-4 and autoload

I recommend that before learning how to create a package to Composer learn about PSR-4 and PSR-0 (this is in disuse, but there are still packages that use it):

  • I created a portable server (Portable), which can be run even from a USB stick. I am now developing a Portable console as well. Then nothing will exist in the environment variables. I really need to use a physical path to PHP, either by replacing it in Composer.json automatically when some (all) packages are installed, or otherwise

  • But to run Poser on the CLI itself it will be necessary to run set PATH=... in the CLI interface, which is equivalent to "existing" @Gilsones

  • I’ve already developed the Portable Console, and it works perfectly, with no environment variable. The problem even happens when a Composer.json has a "script" that calls php

  • And how Composer runs?

  • Thus: https://jsfiddle.net/gv74vLf4/

  • I didn’t ask about the script, I asked about how it executes the commands. So how do you call the Composer in the terminal?

  • No, it’s an HTML form, via post, for now... if everything goes well, I create something more comfortable.

  • So you do not want to create a "plugin" Composer, what you want is to create something entirely geared to a need your specific.

  • The need is to add a function to Composer, as soon as you download a package, check your Composer.json and replace it. I’m really confused if it is possible to do it through a plugin, or if by the Jsfiddle code I posted above, it would be possible to add events. I do not want to touch the code of Composer, because I intend to provide this server Portable

  • @Gilsones then Poser seems to be a little unnecessary, just for autoload makes the need, or maybe nothing is necessary, install everything you have to install on the pen drive and make available so.

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.