Copy files from package to project root directory using composer - php

I'm writing a package for Packagist and I'm facing a problem with Composer. I need to copy a file from my package to the project root after install, but nothing is happening after the package installation.
After reading Composer documentation, I found that I should put a script inside the event post-install-cmd, inside the script section into composer.json file.
So, I added this to my package composer.json file
"scripts": {
"post-install-cmd": [
"php -r \"copy('vendor/myvendor/mypackage/myfile', 'myfile');\""
]
}
To install the package I'm doing
$ composer require myvendor/mypackage --dev
After the package installation everything seems fine, but the file is not being copied and no error is shown.

See https://getcomposer.org/doc/articles/scripts.md for the documentation of scripts. The most relevant part is:
Note: Only scripts defined in the root package's composer.json are
executed. If a dependency of the root package specifies its own
scripts, Composer does not execute those additional scripts.
So, you cannot define any scripts in your module. There is a bug report about that, but the maintainer of composer is not a friend of executing the scripts of dependencies

Related

Running commands after Composer package installed globally

I have created a Composer package which is designed to be installed globally, it's basically a package to spin up Docker environments for projects that I use a lot for local development. I'd like to share my package but would like to make it more user friendly.
Currently when the package is installed (with composer global require me/my-package-name), the user needs to take some extra steps:
cd into the install location and run composer install (inside the root of my package in their vendor dir)
On MacOS, users also need to run cd docker && chmod +x phpenv
Add above docker folder to their PATH
I'm trying to make this less of a pain, so I attempted to add a post package install command - composer.json:
"scripts": {
"post-package-install": [
"DannyXCII\\Environment\\Install\\Installer::postPackageInstall"
]
},
public static function postPackageInstall(): void
{
$path = dirname(__DIR__, 3);
exec("cd $path && composer install && composer update && composer dump-autoload && cd docker && chmod +x phpenv");
}
After trying and failing to get this to work when installing this globally I realise now that this script won't get called when this package is installed, only when packages are required and composer install is ran inside this package, as:
Note: Only scripts defined in the root package's composer.json are executed. If a dependency of the root package specifies its own scripts, Composer does not execute those additional scripts.
So, I believe in this case, the solution would be to add this script to the root project - so I'd need to update this script and add to the composer.json file in C:\Users\Danny\AppData\Roaming\Composer(?), as would anyone else wanting to use this - so this doesn't solve my issues regarding usability.
I have been searching and attempting to resolve this for a while but found this quite a difficult topic to search on.
Essentially, my question is: how can I run commands such as composer install inside my package after it is globally required without the user having to do so manually?
Ok so I've now solved this - so, you know how you can run composer global require laravel/installer and immediately type laravel and you'll see some help text from the command? This is the same level of convenience I wanted to achieve with my package so I decided where better to look to see how it's done?
I was thinking about this incorrectly; I don't want a vendor directory inside my globally installed package and I shouldn't ever expect commands like composer install inside my package.
I was able to remove the two blocks from above. Inside my phpenv script, where I'm looking for the autoloader from within my package, I also added an additional check, so that this will use the autoloader at C:/Users/Danny/AppData/Roaming/Composer/vendor/autoload.php if it exists - this removes the need to run composer install (one step down):
if (file_exists(dirname(__DIR__, 3) . '/autoload.php')) {
require dirname(__DIR__, 3) . '/autoload.php';
} else {
require dirname(__DIR__, 1) . '/vendor/autoload.php';
}
Assuming you have your Composer bin directory added to your path (something like C:/Users/Danny/AppData/Roaming/Composer/bin), I was able to remove the need to add the path to this package specifically by making sure that the script to execute my commands was mapped to the above bin directory. I moved my phpenv script to a bin directory in my package and then added the following to my composer.json - this removes the need to add the path to the script directory to my environment variables path:
"bin": [
"bin/phpenv"
],

Composer giving error "Package drupal/smtp has no version defined" when using artifact type repository

I am trying to setup apigee/devportal-kickstart-project composer project in offline mode, i.e. access to the internet will not be available while installing. So I need to have all the required dependencies available on the machine itself and composer should read them locally to resolve the dependencies.
To achieve this, I first installed the project in online mode, i.e. with internet and captured the logs. I noted all the dependencies that were installed and downloaded the zip file for each dependency from github one by one. I placed all these zip files in a location /tmp/dependencies on my machine (while ensuring within each zip archive that the composer.json is present at the root level).
Now I modified the composer.json inside the project apigee/devportal-kickstart-project and replaced the existing repository definition to (everything else in the file remained as it is) :
"repositories": [
{
"type": "artifact",
"url": "/tmp/dependencies/"
}
]
I disabled packagist globally using the global config flag:
php composer.phar config -g repo.packagist false
Then while offline, I ran composer update from inside the apigee/devportal-kickstart-project folder, but I get the below error :
Loading composer repositories with package information
In ArtifactRepository.php line 142:
Failed loading package in /tmp/dependencies2/smtp-8.x-1.0.zip: Package drupal/smtp has no version defined.
In ArrayLoader.php line 117:
Package drupal/smtp has no version defined.
As I can understand, the composer.json files in each zip doesn't have a version tag defined explicitly. So what can I do here to have all these dependencies in zip format to get resolved other than manually adding the version tag in each zip's composer.json?
Is there any flag I can run composer update with using which composer simply loads the dependency without having to check the package?

PHP, Composer, PHP dependency manager

I am starting a new PHP project, and I wanted to pull some php components such as "nesbot/carbon" using composer. But when I create a composer.json file and try to run composer install command, it downloads other files from my previous projects that I don's want.
Even when I try to run "composer install" with out having a composer.json file in an empty folder, it downloads some previous dependencies from caches. I didn't get that from where it's reading composer.json. Am stuck in the middle of project.
How can I create a fresh project with composer?
To create a fresh project with composer, run
$ composer init
in the root directory of that project.
For reference, see https://getcomposer.org/doc/03-cli.md#init.

How to install PHP PhantomJS require packages

I'm trying to use PHP PhantomJS but it's require some package to run ..
I'm composer in php and when I'm trying to install dependencies on root PHP PhantomJS
composer install
says
Loading composer repositories with package information
Installing dependencies (including require-dev)
Nothing to install or update
Generating autoload files
I'm confused ... anybody can help how install package that's need to run PHP PhantomJS script?
If you downloaded it, you have everything you need without using Composer. Composer is for downloading this and other packages, but this is not the only way of getting packages. :)
Now the easiest way to use that package is to have a new clean empty directory and execute this line:
composer require jonnyw/php-phantomjs:2.*
Now magic happens, including the creation of a composer.json file, download of that package, creation of autoloading files, and then you are done.
With an otherwise empty directory you couldn't do very much, so in order to make use of that package (or others) inside your own code, go to the root directory of your code, and execute above line. The same things happen, and then you are pretty much done - apart from the fact that you need to include the autoloader file in your own code if you want to use that package.
Install PhantomJS via Composer
Use the package "jakoch/phantomjs-installer".
It installs the PhantomJS binary for Linux, Windows or Mac into the /bin folder of your project.
Simply add the following lines to your projects composer.json file:
{
"require": {
"jakoch/phantomjs-installer": "3.0.0"
},
"scripts": {
"post-install-cmd": [
"PhantomInstaller\\Installer::installPhantomJS"
],
"post-update-cmd": [
"PhantomInstaller\\Installer::installPhantomJS"
]
},
"config": {
"bin-dir": "bin"
}
}
The version number determines which version of PhantomJS is fetched.
Execute composer update or composer install

fuelphp Composer is not installed. Please run "php composer.phar update" in the root to install Composer

I am trying to install fuelphp.
And getting the error as
Composer is not installed. Please run "php composer.phar update" in the root to install Composer
In my xampp/php directory I run a command
php -r "eval('?>'.file_get_contents('https://getcomposer.org/installer'));"
But once i run php composer.phar install composer could not find a composer.json in e:\xampp\php
How can i resolve and run fuelphp successfully can anyone help.
As of 1.7.1, we no longer supply the composer.phar file in the zip. It only has a limited shelf life (30 days before it starts complaining).
Instead we suggest you install composer yourself, either locally (specific for this fuelphp installation) or globally so you only have to install it once for all your projects.
Composer is a tool for dependency management in PHP, like npm for Node.js, bundler for ruby, and others.
It reads a file called composer.json with the dependent libraries your project needs and, finally, installs (downloads) them for you.
Fuelphp can be installed using composer, but there are several packages.
So, you need to create the composer.json with all needed fuelphp packages. Open notepad, save a file with the name composer.json (be sure to save it with ".json" extension) and put this content:
{
"require": {
"fuelphp/upload": "2.0.1",
"fuelphp/event": "0.2.1",
(...)
}
}
Note you need to insert at "(...)" the others fuelphp packages and the needed versions. You can check them all at https://packagist.org/ (type fuelphp at search). Packagist is the main Composer repository.
More information about Composer at this link.
Let me know if you need more information about it.
If you are running FuelPHP 1.7, the download comes with all Composer files you need. You need to run php composer.phar install in the root directory of FuelPHP (the same directory that contains the public folder and the fuel folder.
As long as you have v1.7 (I'm not sure if earlier versions contain the files), that directory will contain composer.json and composer.phar.
In folder fuelphp-1.7 (latest), there are two files: composer.phar and composer.json you need to cd into that folder and run:
php composer.phar install
If you don't have php in your path, you should do something like:
e:\xampp\bin\php\php5.4.16\php.exe composer.phar install
Use the full path to your php.exe. Remember your current working directory should be fuelphp folder where composer.phar and composer.json are.

Categories