Did you know that you can obtain PEAR packages via Composer? And you don't need to install or set up PEAR or configure global settings!
A work colleague was talking about difficulties setting up CPAN for Perl on a server and that got me thinking about dependency managers and repositories and the options available in the PHP world. The modern way to manage dependencies and find libraries/packages is using Composer and Packagist but the old way was through using PEAR which was a global dependency manager with a repository of useful packages. I started to wonder if it was possible to load an old-style PEAR package through the new Composer dependency manager and after some fiddling about I got it working successfully.
Not long ago the PEAR guys decided to create a new version of PEAR so there are now two PEAR repositories available: classic PEAR (http://pear.php.net) and PEAR2 (http://pear2.php.net) which is also known as Pyrus. Most of the packages are still stuck in the old PEAR repository. So let's pick a test package from each of them to install. For PEAR2 I'm going to try the package suggested in the Composer documentation, PEAR2_HTTP_Request, and for old PEAR I'm going to try Numbers_Roman.
Open up the composer.json file and edit it so it looks something like this:
{
"repositories": [
{
"type": "pear",
"url": "http://pear.php.net"
},
{
"type": "pear",
"url": "http://pear2.php.net"
}
],
"require": {
"pear-pear2/PEAR2_HTTP_Request": "*",
"pear-pear/Numbers_Roman": "*"
}
}
The two different PEAR repositories are defined in the repositories section of the composer.json file. Note the first two words (pear-pear and pear-pear2) used in the paths of the require values. This allows Composer to know which repository to look in. Details about choosing alternative channels are in the Composer documentation.
Then run php composer.phar update to install the new packages.
Now we can create a new PHP file to test the packages just installed. Here's my test code. Note that PEAR2 packages are namespaced, hence the use command:
<?php
require 'vendor/autoload.php';
use pear2\HTTP\Request,
pear2\HTTP\Request\Adapter\Curl;
$url = 'http://www.yahoo.com/';
$request = new Request($url, new Curl());
$response = $request->sendRequest();
$nr = new Numbers_Roman();
//http response code 200 or 'CC' in roman numerals means success
if ('CC' == $nr->toNumeral($response->code)) {
echo "Successfully accessed $url";
} else {
echo "An error (HTTP{$response->code}) occured while trying to access $url";
}
?>
And that's it. Composer is just awesome!
Showing posts with label PEAR. Show all posts
Showing posts with label PEAR. Show all posts
Tuesday, 14 January 2014
Friday, 13 January 2012
How to setup PEAR on WAMP2.1 and Windows 7
This has been bugging me for ages now and I gave up trying to get it to work on my laptop but now I tried again at work and finally got it working. Yay!
Visit this page in your browser: http://pear.php.net/go-pear.phar
It will start a download. Move the go-pear.phar file to your C:\wamp\bin\php\php5.3.5\ directory
Now you need to put the pear executable into your system path. Right click on (My) Computer which can be accessed from the Desktop or Start button and select Properties. Click on Advanced System Settings and select the Advanced tab. Click the Environmental Variables button. Scroll down to find the Path variable and double click the value. Add a semicolon ";" at the end if there isn't one and then add this path: C:\wamp\bin\php\php5.3.5\
Save your changes.
Now open up the command line (Press the Start button and type in cmd) and go to C:\wamp\bin\php\php5.3.5\
Type in php go-pear.phar
Read the questions it asks you and type the stuff it tells you to continue. At the moment we're not too bothered with the variables it sets. When it's done it will write to the php.ini file in the current directory. This is NOT the php.ini file that WAMP uses. To see where the php.ini file is that WAMP uses you can create a phpinfo.php file in your www folder with <?php phpinfo(); ?> inside it and then run it from the browser, but I'll just tell you where it is instead - C:\wamp\bin\apache\Apache2.2.17\bin.
Open up the php.ini file that is in the current php5.3.5 directory with a text editor and scroll to the bottom to find these lines:
Copy and paste those to the bottom of the php.ini file in the Apache-based directory. Save your changes and restart WAMP if you have it running.
Back in the command line type in pear config-create \wamp\bin\php\php5.3.5 pear.ini
Note the space between the path and the file name.
Now you may wish to set the paths of some of the configuration files if these are not already correct. To view the pear config settings type in pear config-show
This will give you an idea of which paths to amend. It will display a list in three columns, the first is the description/name of the setting, the second is its key and the third column displays the path.
In order to set the path correctly you need to supply the key as the first argument and the folder path as the second argument like so: pear config-set www_dir C:\wamp\bin\php\php5.3.5\www
Set the correct path for the keys www, data, cfg, docs and tests.
So now you are ready to run PEAR with your php.
First, have a go at installing a PEAR package like PHPUnit which is used in unit testing. The commands are:
pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit
Hopefully that should work fine. But if it doesn't like when I got back home to try installing it on my laptop. Try these commands and give it another go.
pear clear-cache
pear update-channels
With pear working, pear packages installed and your PHP include path including the pear directory you can write some good code. To include a pear-installed module type in include 'PHPExcel/PHPExcel.php'; for example.
I hope this stuff works for you too. I can't provide any support if it doesn't work for you because I don't know much about this stuff and I'm surprised I got it to work.
Visit this page in your browser: http://pear.php.net/go-pear.phar
It will start a download. Move the go-pear.phar file to your C:\wamp\bin\php\php5.3.5\ directory
Now you need to put the pear executable into your system path. Right click on (My) Computer which can be accessed from the Desktop or Start button and select Properties. Click on Advanced System Settings and select the Advanced tab. Click the Environmental Variables button. Scroll down to find the Path variable and double click the value. Add a semicolon ";" at the end if there isn't one and then add this path: C:\wamp\bin\php\php5.3.5\
Save your changes.
Now open up the command line (Press the Start button and type in cmd) and go to C:\wamp\bin\php\php5.3.5\
Type in php go-pear.phar
Read the questions it asks you and type the stuff it tells you to continue. At the moment we're not too bothered with the variables it sets. When it's done it will write to the php.ini file in the current directory. This is NOT the php.ini file that WAMP uses. To see where the php.ini file is that WAMP uses you can create a phpinfo.php file in your www folder with <?php phpinfo(); ?> inside it and then run it from the browser, but I'll just tell you where it is instead - C:\wamp\bin\apache\Apache2.2.17\bin.
Open up the php.ini file that is in the current php5.3.5 directory with a text editor and scroll to the bottom to find these lines:
;***** Added by go-pear
include_path=".;C:\wamp\bin\php\php5.3.5\pear"
;*****
include_path=".;C:\wamp\bin\php\php5.3.5\pear"
;*****
Copy and paste those to the bottom of the php.ini file in the Apache-based directory. Save your changes and restart WAMP if you have it running.
Back in the command line type in pear config-create \wamp\bin\php\php5.3.5 pear.ini
Note the space between the path and the file name.
Now you may wish to set the paths of some of the configuration files if these are not already correct. To view the pear config settings type in pear config-show
This will give you an idea of which paths to amend. It will display a list in three columns, the first is the description/name of the setting, the second is its key and the third column displays the path.
In order to set the path correctly you need to supply the key as the first argument and the folder path as the second argument like so: pear config-set www_dir C:\wamp\bin\php\php5.3.5\www
Set the correct path for the keys www, data, cfg, docs and tests.
So now you are ready to run PEAR with your php.
First, have a go at installing a PEAR package like PHPUnit which is used in unit testing. The commands are:
pear config-set auto_discover 1
pear install pear.phpunit.de/PHPUnit
Hopefully that should work fine. But if it doesn't like when I got back home to try installing it on my laptop. Try these commands and give it another go.
pear clear-cache
pear update-channels
With pear working, pear packages installed and your PHP include path including the pear directory you can write some good code. To include a pear-installed module type in include 'PHPExcel/PHPExcel.php'; for example.
I hope this stuff works for you too. I can't provide any support if it doesn't work for you because I don't know much about this stuff and I'm surprised I got it to work.
Labels:
PEAR,
PHP WAMP PEAR INSTALL,
WAMP,
WAMP2.1,
Windows 7
Subscribe to:
Posts (Atom)