Multiple paths for configuration and packages

Added Loader::AddPath() which allows additional paths to be added to
the list in which the loader tries to find both configuration files
and components.
This commit is contained in:
Emmanuel BENOîT 2012-02-05 13:58:55 +01:00
parent 871d28cd16
commit 3b91a6fc8c

View file

@ -42,6 +42,7 @@ final class ConfigGetter
final class Package final class Package
{ {
private $name; private $name;
private $source;
private $files; private $files;
private $requires; private $requires;
@ -57,9 +58,10 @@ final class Package
private $loaded = false; private $loaded = false;
private $config; private $config;
public function __construct( $name , $description , $config ) public function __construct( $name , $description , $config , $source )
{ {
$this->name = $name; $this->name = $name;
$this->source = $source;
$fields = array( 'files' , 'requires' , 'daos' , 'views' , 'ctrls' , 'extras' , 'singletons' , 'pages' , 'hooks' ); $fields = array( 'files' , 'requires' , 'daos' , 'views' , 'ctrls' , 'extras' , 'singletons' , 'pages' , 'hooks' );
foreach ( $fields as $field ) { foreach ( $fields as $field ) {
@ -103,6 +105,11 @@ final class Package
return $this->name; return $this->name;
} }
public function source( )
{
return $this->source;
}
public function files( ) public function files( )
{ {
return $this->files; return $this->files;
@ -188,7 +195,7 @@ final class Loader
{ {
private static $loader = null; private static $loader = null;
private $baseDir; private static $paths = array();
private $config; private $config;
private $packages = array( ); private $packages = array( );
private $items = array( private $items = array(
@ -206,21 +213,38 @@ final class Loader
private function __construct( ) private function __construct( )
{ {
$this->baseDir = dirname( __FILE__ ); if ( empty( Loader::$paths ) ) {
array_push( Loader::$paths , dirname( __FILE__ ) );
}
$this->loadConfig( ); $this->loadConfig( );
$this->loadPackageDescriptions( ); $this->loadPackageDescriptions( );
} }
private function loadConfig( ) private function loadConfig( )
{ {
$config = array( ); $mergedConfig = array( );
@include( $this->baseDir . '/config.inc.php' ); foreach ( Loader::$paths as $directory ) {
$this->config = $config; if ( ! file_exists( $directory . '/config.inc.php' ) ) {
continue;
}
$config = array( );
@include( $directory . '/config.inc.php' );
$mergedConfig = array_merge_recursive( $mergedConfig , $config );
}
$this->config = $mergedConfig;
} }
private function loadPackageDescriptions( ) private function loadPackageDescriptions( )
{ {
if ( !( $dh = opendir( $this->baseDir ) ) ) { foreach ( Loader::$paths as $source ) {
$this->loadPackageDescriptionsFrom( $source );
}
}
private function loadPackageDescriptionsFrom( $source )
{
if ( !( $dh = opendir( $source ) ) ) {
throw new LoaderException( "unable to access directory" ); throw new LoaderException( "unable to access directory" );
} }
@ -229,9 +253,9 @@ final class Loader
continue; continue;
} }
$path = "{$this->baseDir}/$entry"; $path = "$source/$entry";
if ( is_dir( $path ) && is_file( "$path/package.inc.php" ) ) { if ( is_dir( $path ) && is_file( "$path/package.inc.php" ) ) {
$this->loadDescription( $entry ); $this->loadDescription( $entry , $source );
} }
} }
@ -239,10 +263,10 @@ final class Loader
} }
private function loadDescription( $name ) private function loadDescription( $name , $source )
{ {
$package = array( ); $package = array( );
require( $this->baseDir . '/' . $name . '/package.inc.php' ); require( $source . '/' . $name . '/package.inc.php' );
if ( empty( $package ) ) { if ( empty( $package ) ) {
throw new LoaderException( "package '$name': no information" ); throw new LoaderException( "package '$name': no information" );
} }
@ -251,7 +275,7 @@ final class Loader
$this->config[ $name ] = array( ); $this->config[ $name ] = array( );
} }
$package = new Package( $name , $package , $this->config[ $name ] ); $package = new Package( $name , $package , $this->config[ $name ] , $source );
$this->packages[ $name ] = $package; $this->packages[ $name ] = $package;
$this->config[ $name ] = null; $this->config[ $name ] = null;
@ -289,7 +313,7 @@ final class Loader
$this->loadPackage( $dependency ); $this->loadPackage( $dependency );
} }
$dir = $this->baseDir . '/' . $name; $dir = $package->source() . '/' . $name;
foreach ( $package->files( ) as $file ) { foreach ( $package->files( ) as $file ) {
require_once( "$dir/$file.inc.php" ); require_once( "$dir/$file.inc.php" );
} }
@ -393,6 +417,17 @@ final class Loader
} }
public static function AddPath( $path )
{
if ( empty( Loader::$paths ) ) {
array_push( Loader::$paths , dirname( __FILE__ ) );
}
if ( is_dir( $path ) ) {
array_push( Loader::$paths , $path );
}
}
public static function PackageConfig( $name ) public static function PackageConfig( $name )
{ {
$loader = Loader::get( ); $loader = Loader::get( );