PHP Objects, Patterns and Practice (3rd edition)

(Barry) #1

CHAPTER 15 ■ AN INTRODUCTION TO PEAR AND PYRUS


XML_Feed_Parser_Exception:Invalid input: this is not valid XML


MyPearException:feed acquisition failed
[cause] XML_Feed_Parser_Exception:Invalid input: this is not valid XML


an error occurred. See log for details


Our logger method is invoked for both the exceptions thrown by this sample (the first by
XML_Feed_Parser, the second by MyFeedThing). The XML_Feed_Parser_Exception object makes a second
appearance in the log output because we added it to the MyPearException object as a cause.


Creating Your Own PEAR Package


Packages from the PEAR repository are well documented and designed to be easy to use. How easy are
they to create, though, and how do you go about creating your own? In this section, we will look at the
anatomy of a PEAR package.


package.xml


The package.xml file is the heart of any PEAR package. It provides information about a package,
determines where and how its participants should be installed, and defines its dependencies. Whether it
operates on a URL, the local file system, or a tarred and gzipped archive, the PEAR installer needs the
package.xml file to acquire its instructions.
No matter how well designed and structured your package is, if you omit the build file, the install
will fail. Here’s what happens if you attempt to install an archive that does not contain package.xml:


$ pear install baddialekt.tgz
could not extract the package.xml file from "baddialekt.tgz"
Cannot initialize 'baddialekt.tgz', invalid or missing package file
Package "baddialekt.tgz" is not valid
install failed


The PEAR installer first unpacks our archive to the temporary directory and then looks for
package.xml. Here, it falls at the first hurdle. So if package.xml is so important, what does it consist of?


Package Elements


The package file must begin with an XML declaration. All elements are then enclosed by the root package
element:


<?xml version="1.0" encoding="UTF-8"?>
<package packagerversion="1.4.11" version="2.0"
xmlns="http://pear.php.net/dtd/package-2.0"
xmlns:tasks="http://pear.php.net/dtd/tasks-1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://pear.php.net/dtd/tasks-1.0
http://pear.php.net/dtd/tasks-1.0.xsd
http://pear.php.net/dtd/package-2.0
http://pear.php.net/dtd/package-2.0.xsd">

Free download pdf