Refactoring Composer executable file for portability - composer-php

Installing Composer locally puts a file named composer in your directory. I have taken to copying this file into each of my projects that uses Composer, so that I can run php composer in each folder.
Since this results in a lot of needlessly-repeated code, I want to refactor the composer file into something minimal. The most logical solution would be to refer to a class that resides in the vendor folder. Why is there so much code in the composer file anyway? I'd prefer not to put the code into my own libraries if I don't have to.
For comparison, here's a sample of code in a typical phpunit file:
require_once 'my_bootstrap.php';
spl_autoload_register(array(MyBootstrap::class,'autoload'));
// Do some other stuff here.
\PHPUnit_TextUI_Command::main();
I want to reduce the composer file in each project to something like this.
I do not want to install Composer globally. I want each project to be an environment unto itself and not have to instruct another developer to install anything other than PHP.

Related

how to compile C-code when installing laravel package (composer)

We have a C-code utility that I need to execute from php code that I'm trying to convert into a laravel package. (The PHP code basically just an interface-to/wrapper-for the program so it can be utilized from inside php)
One of the issues with the c-code is that I need to compile it per-environment.
I'm wondering if it's possible either somewhere in composer or somewhere in the provider code to have the C-code utility compiled either when I require the laravel package (composer) or when I run artisan to publish the vendor pieces. (and wondering how to code that)
e.g. in my package I have a folder called 'imagescanner' which includes a makefile to create a binary called 'imagescanner'. (To make things trickier, I technically have two versions of it depending on whether it is a website or a production-facility environment, but I can probably figure out how to trigger the version once I have a solution for triggering a install-time compile)
What I would like to be able to do is to ultimately run artisan to publish the two versions of the utility 'wimagescanner' and 'pimagescanner' into the main laravel project root in a 'bin/' directory.
File structure of the package (when installed by composer):
laravel/
bin/
vendor/
mycompany/
laravel-imagescanner/
imagescanner/
production/
imagescanner.h
imagescanner.c
Makefile
website/
imagescanner.h
imagescanner.c
Makefile
src/
ImageScanner.php
ImageScannerServiceProvider.php
I basically need 'make' to be run in each directory at some point (either when required/installed with composer or when the artisan publish is called) then have the respective 'imagescanner' binaries from 'production' and 'websites' to be copied to the main laravel bin/pimagescanner and bin/wimagescanner respectively.

Calling class from vendor

EDIT Autoload
require __DIR__ . '/../../../vendor/autoload.php';
I am calling it this way:
$generatorSVG = new \Picqer\Barcode\BarcodeGeneratorSVG();
The error:
Message: Class 'Picqer\Barcode\BarcodeGeneratorSVG' not found
The class exist. I am not installing it via composer but copying it in the vendor folder, because I dont have composer and the server and dont have permissions to install it.
Using PHPStorm and by ctrl+click on the class leads me to it. I am wrong with the proper calling it in CI ?
Manually copying files in the vendor directory is not enough, because Composer generates a bunch of lookup files (vendor/composer/autoload_*.php) to be able to figure out where the files are located that you want to load via Composer's autoloader.
Either, you should composer require the package you want to install or require all your source files manually in your bootstrap.php or index.php.
If this is a custom package you built, be aware that Composer can also pull it directly from a Git instance, local directory, or zip file. There's no need to create a public package on Packagist or self-host it with Satis. Check the Composer docs on how to configure repositories to find out more.

Installing Composer and Packagers - first time

I have never used Composer, but I want to use PHPSpreadsheet package, and it is recommended that Composer is used.
I am on a MAC using XAMPP and Netbeans.
I have installed Composer, and I have run the following command to get and install the PHPSpreadsheet package.
php ../../Composer/composer.phar require phpoffice/phpspreadsheet
I am running this in my project folder, (hence the ../../ to where Composer.phar is located.
This downloads the files into a vendor folder in my project folder.
What should I do then? Do I need to keep it in the Vendor folder, or can I move into a folder of my choice?
Netbeans has Composer options in the menus, but as far as I can see, this is for creating dependencies rather than installing packages.
I know I am totally missing the point of Composer somewhere, but have spent hours just trying to get this work.
Many thanks
You should really start with the docs -> https://getcomposer.org/doc/01-basic-usage.md
You have to keep the vendor directory - this is where all dependencies are kept. If you require more packages - then they will be installed in that directory.
After requring the package you have to load it so the PHP will know all the classes. Composer comes with great autolader. It is located by default in vendor/autoload.php. So what you have to do now is to require this file in your project. After that all classes from composer packages will be loaded automaticaly each time you use them in the code :)
I hope this will help you with this great tool. Cheers.

How to use composer included items?

In my composer.json file I added "kigkonsult/icalcreator": "v2.23.7" to the required list, and after doing the install I see that directory in existence under the vendor dir. I can't figure out how to "use" it though in my PHP files in this project.
If I weren't using composer I'd just require the icalcreator.php file in that area. What's the right syntax when I include it via composer?
Once the package is installed you need to add the composer autoloader somewhere in your project.
require_once('/path/to/vendor/autoload.php');
Normally Composer uses PSR-4 autoloading, but it looks like this package doesn't. It just adds it's own autoloader to composer so just including the Composer loader should be sufficient.
Once the autoloader is added you can just use the package however you need. Composer will automatically register the packages custom autoloader and PHP will use that to load the appropriate file.

Where should I install Composer?

Just wondering where I should install Composer. It kind of wrecked my environment last time. I'm running XAMPP and i'm looking to use it within some Framework sites. So to me the XAMPP folder itself seems appropriate. Would that be correct? The main thing for me is that it doesn't alter any environment paths or the such.
Any advice would be great.
The main aspect is that you'd probably want to run Composer easily in the command shell. This implies that Composer has to be in any directory mentioned in the path variable.
Have a look at your current path, pick a convenient directory, and put the composer.phar file there.
If you don't like that, you could also create a batch file that does run PHP with that phar file in a different location, and passes all the other command line arguments to it.

Resources