The open source Composer tool, which manages PHP project dependencies, has become standard equipment for modern PHP. New to this tool? I recommend this introduction to Composer.
Composer automatically installs or updates required components, known as dependencies, and any others required by those initial components. The required components are defined by the developer in an easy-to-read JSON-formatted text file.
Say my application uses a PHP framework such as Zend Framework 3 or Laravel. My application depends on that PHP framework, which, in turn, might depend on something else being installed. Once you define the top-level component, or dependency (in this case, ZF3 or Laravel), and run the “composer install” command, Composer will download any needed components to the vendor folder.
Composer also allows you to choose bits and pieces (components) from various frameworks or libraries. Furthermore, it creates an autoloader, so that your application doesn’t have to know the exact path of the component. You can point your application to the component’s autoloader, and you are set to go. It really saves you time and headaches once you get used to using it.
How? (IBM i–specific)
Use either the PASE shell (QP2TERM) by running CALL QP2TERM from the command line, or, better, an SSH terminal shell.
Steps In Brief:
1. Add PHP to classpath
2. Run Composer installer
3. Copy it to php bin library
4. Use it!
Hot Tip: If you get out-of-memory errors when running Composer, try these steps:
1. Increase PASE memory:
before executing your PHP composer commands.
2. In your php command string, add a string resembling
right after the php, to make something like:
php -d memory_limit-512 [rest of command goes here]
Step 1 – Add PHP to classpath
You can run the following commands on the QP2TERM or SSH command line (you’d need to do this each time you start a QP2TERM or SSH session):
$ PATH=$PATH:/usr/local/zendphp7/bin $ LIBPATH=$LIBPATH:/usr/local/zendphp7/bin $ export PATH $ export LIBPATH
Or you can create or modify a file called .profile with those exact commands in /home/YOURUSERPROFILEHERE/
See this article:
I find if I use EDTF to create the file, it creates the file with CCSID 37 (EBCDIC). If, however, I create it in a PC-based text editor and move it to the IFS, the file will be CCSID 819 or 1208, which PHP can understand.
Step 2 – Install
Official installation instructions: https://getcomposer.org/download/
$ php -r "readfile('https://getcomposer.org/installer');" > composer-setup.php
(on one system, it wouldn’t allow me to run https, so I changed the URL to http.)
$ php composer-setup.php $ php -r "unlink('composer-setup.php');"
Step 3 – Copy the archive file to the PHP binary directory
$ mv composer.phar /usr/local/zendphp7/bin
Step 4 – Use it!
Create a composer.json configuration file in your project folder. I could take a closer look at composer.json in a future post if there is interest from our readers.
Now, run composer install!
$ composer.phar install
Watch your folders fill with dependencies 🙂
require __DIR__ . '/vendor/autoload.php';
at the top of your application code. Composer can now create component object instances as you need them.
Part of my job at Seiden Group, besides developing applications for clients, is to help the IBM i platform appeal to fresh talent. On any given day, I might teach open source to IBM i professionals, or I might empower Linux-oriented developers to thrive on IBM i. If you would like to jump-start your application development with tools such as Composer or Git, or speed development of a great application, let us know.