The Library class' purpose is to simplify the
inclusion of dependencies between classes. Under the covers
Library uses an autoloading mechanism and some
other techniques to ensure high-performance in applications that use
Recess Core's functionalities. These caching and script compilation
techniques will be discussed in-depth later.
Library's most useful static methods are
addClassPath and import.
Library maintains a list of directories it attempts
to find an imported class in. The exact location of imported class files
can be cached by Library to avoid hitting disk
multiple times to locate single files. The order in which paths are added
to Library using
addClassPath is important. When attempting to
import a class paths are checked from the most-recently added to the
least-recently added using addClassPath. The
Recess Framework uses this mechanism to allow classes defined in
plugins/ to take precedence over
recess/, and classes in apps/ to
take precedence over plugins/.
Library's import style is
inspired by Java/C# imports and is a stylistic difference from libraries
like the Zend Framework. Suppose the following directory structure:
/public_html
/foo
ClassA.class.php
/bar
ClassB.class.phpThese classes can be imported using
Library with the following snippet of PHP:
Example 6.1. Using Library's addClassPath and
import methods
Library::addClassPath('/home/example/public_html'); Library::import('foo.ClassA'); $aClass = new AClass(); Library::import('foo.bar.ClassB'); $bClass = new BClass();
If you ever need to use use Library and
Recess classes in PHP files outside of the Recess Framework it requires
setting up some environment variables and using
include on Library directly. The following
snippet can be placed into a php script and included by plain-old PHP
files.
Recess' convention is one class definition per file. PHP files
that contain class definitions must use the same name for the file as
the class' name, followed by the extension ".class.php". So, for a
class defined with class Foo { ... } the
filename would be Foo.class.php.
Example 6.2. core-bootstrap.php - Bootstrapping into Recess Core with
Library
<?php // core-bootstrap.php // Bootstrap into Recess Core date_default_timezone_set('America/New_York'); $_ENV['dir.bootstrap'] = str_replace('\\','/',realpath(dirname(__FILE__))) . '/'; $_ENV['url.base'] = str_replace(basename(__FILE__), '', $_SERVER['PHP_SELF']); $_ENV['dir.recess'] = $_ENV['dir.bootstrap'] . 'recess/'; $_ENV['dir.temp'] = $_ENV['dir.bootstrap'] . 'data/temp/'; // This directory must be writeable by PHP require($_ENV['dir.recess'] . 'recess/lang/Library.class.php'); Library::addClassPath($_ENV['dir.recess']); Library::addClassPath($_ENV['dir.bootstrap'] . 'plugins/'); // Add additonal paths to Library's classpath Library::addClassPath($_ENV['dir.bootstrap'] . 'apps/'); ?>
These directory paths may need to be tweaked depending on where you've extracted the Recess distribution. Explanations for each of these environment variables follows:
$_ENV['dir.bootstrap'] - The absolute directory
that contains the bootstrap file. This is a helper variable that
simplifies defining the other environment directories by reference.
$_ENV['dir.recess'] - The base directory for
Recess' source files. This directory should contain two subdirectories:
recess/ and test/.
$_ENV['dir.temp'] - A directory that is writable
by PHP scripts. This directory is used by Recess to store temporary data
such as cached data structures or compiled scripts.
$_ENV['url.base'] - This is the base URL path
that relative URLs should be constructed from. Outside of the Recess
Framework this is less meaningful. Recess uses it for mapping routes, and
generating URLs in view helpers.