Merge "Separating the autoloader out. It only needs to be used when composer is not available."

This commit is contained in:
Jenkins 2014-03-14 22:55:18 +00:00 committed by Gerrit Code Review
commit 175d47ae4b
10 changed files with 201 additions and 144 deletions

View File

@ -54,11 +54,11 @@
* <?php
* // This is only required if you don't have a PSR-0
* // autoloader to do the hard work for you.
* require 'OpenStack/Bootstrap.php';
* require 'OpenStack/Autoloader.php';
*
* // If you aren't using a PSR-0 autoloader,
* // you might want to use this:
* \OpenStack\Bootstrap::useAutoloader();
* \OpenStack\Autoloader::useAutoloader();
*
* // Turn on stream wrappers.
* \OpenStack\Bootstrap::useStreamWrappers();
@ -104,11 +104,11 @@
* <?php
* // This is only required if you don't have a PSR-0
* // autoloader to do the hard work for you.
* require 'OpenStack/Bootstrap.php';
* require 'OpenStack/Autoloader.php';
*
* // If you aren't using a PSR-0 autoloader,
* // you might want to use this:
* \OpenStack\Bootstrap::useAutoloader();
* \OpenStack\Autoloader::useAutoloader();
*
* use \OpenStack\Services\IdentityService;
*

View File

@ -1,12 +1,13 @@
<?php
require_once __DIR__ . '/../src/OpenStack/Bootstrap.php';
require_once __DIR__ . '/../src/OpenStack/Autoloader.php';
use \OpenStack\Bootstrap;
use \OpenStack\Autoloader;
use \OpenStack\Services\IdentityService;
use \OpenStack\Storage\ObjectStorage;
use \OpenStack\Storage\ObjectStorage\Object;
Bootstrap::useAutoloader();
Autoloader::useAutoloader();
// Load these from an ini file.
$ini = parse_ini_file(getenv('HOME') . '/.OpenStack.ini');

View File

@ -93,14 +93,15 @@ Sometimes, though, you will need to bootstrap OpenStack in your own code,
and this is done as follows:
<?php
require_once 'OpenStack/Bootstrap.php';
require_once 'OpenStack/Autoloader.php';
use \OpenStack\Autoloader;
use \OpenStack\Bootstrap;
use \OpenStack\Services\IdentityService;
use \OpenStack\Storage\ObjectStorage;
use \OpenStack\Storage\ObjectStorage\Object;
\OpenStack\Bootstrap::useAutoloader();
\OpenStack\Autoloader::useAutoloader();
?>
The first line should be self-explanatory: We require the main

View File

@ -1,8 +1,9 @@
<?php
require_once __DIR__ . '/../src/OpenStack/Bootstrap.php';
require_once __DIR__ . '/../src/OpenStack/Autoloader.php';
use \OpenStack\Autoloader;
use \OpenStack\Bootstrap;
Bootstrap::useAutoloader();
Autoloader::useAutoloader();
Bootstrap::useStreamWrappers();
$ini = parse_ini_file(getenv('HOME') . '/.OpenStack.ini');

View File

@ -40,9 +40,9 @@ setup code.
### Loading Classes
The OpenStack PHP-Client library is structured following SPR-0 recommendations.
The OpenStack PHP-Client library is structured following PSR-0 recommendations.
Practically speaking, what this means is that applications that use an
SPR-0 autoloader may be able to automatically load the OpenStack PHP-Client.
PSR-0 autoloader may be able to automatically load the OpenStack PHP-Client.
However, we'll assume that that is not the case. We'll assume that the
library needs to be initialized manually.
@ -51,20 +51,20 @@ What we will do is first load the PHP-Client Bootstrap.php file, and then
use the autoloader in that file to load the rest of the library:
<?php
require_once '/OpenStack/Bootstrap.php';
require_once '/OpenStack/Autoloader.php';
use \OpenStack\Autoloader;
use \OpenStack\Bootstrap;
Bootstrap::useAutoloader();
Autoloader::useAutoloader();
Bootstrap::useStreamWrappers();
?>
The first thing the example above does is require the Bootstrap.php
file, which contains code necessary for initializing the OpenStack
PHP-Client.
The first thing the example above does is require the Autoloader.php
file, which contains code necessary to autoload anything else we will need.
Next, we call two static methods on the OpenStack::Bootstrap object:
Next, we call two static methods:
- Bootstrap::useAutoLoader(): This tells the PHP-Client to load any other
- Autoloader::useAutoLoader(): This tells the PHP-Client to load any other
classes on demand. Since we use this, we don't need any more `require`
or `include` statements.
- Bootstrap::useStreamWrappers(): This tells OpenStack to register its
@ -181,10 +181,11 @@ tokens in a database and re-using them).
Here's how a stream context is used:
<?php
require_once __DIR__ . '/../src/OpenStack/Bootstrap.php';
require_once __DIR__ . '/../src/OpenStack/Autoloader.php';
use \OpenStack\Autoloader;
use \OpenStack\Bootstrap;
Bootstrap::useAutoloader();
Autoloader::useAutoloader();
Bootstrap::useStreamWrappers();
$cxt = stream_context_create(array(

View File

@ -0,0 +1,124 @@
<?php
/* ============================================================================
(c) Copyright 2014 Hewlett-Packard Development Company, L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============================================================================ */
/**
* @file
* An Autoloader to use for the case Composer isn't available.
*/
namespace OpenStack;
/**
* Autoload the OpenStack library.
*
* The OpenStack library is natively designed to be available via Composer. When
* Composer is not available and there is not another PSR-0 compatible autoloader
* to use, this one can be used.
*
* The autoloader can be used like:
*
* @code
* Autoloader::useAutoloader();
* @endcode
*
* @attention
* The structure of the OpenStack file hierarchy is PSR-0 compliant.
* This means that you can use any standard PSR-0 classloader to
* load all of the classes here.
*
* That said, many projects rely upon packages to handle their own
* class loading. To provide this, this package contains a custom
* classloader that will load JUST the OpenStack classes. See
* the Autoloader::useAutoloader() static method.
*
* See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
*/
Class Autoloader {
/**
* The directory where OpenStack is located.
*/
public static $basedir = __DIR__;
/**
* Add the autoloader to PHP's autoloader list.
*
* This will add the internal special-purpose
* autoloader to the list of autoloaders that PHP will
* leverage to resolve class paths.
*
* Because OpenStack is PSR-0 compliant, any
* full PSR-0 classloader should be capable of loading
* these classes witout issue. You may prefer to use
* a standard PSR-0 loader instead of this one.
*
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
*/
public static function useAutoloader() {
spl_autoload_register(__NAMESPACE__ . '\Autoloader::autoload');
}
/**
* OpenStack autoloader.
*
* An implementation of a PHP autoload function. Use
* OpenStack::useAutoloader() if you want PHP to automatically
* load classes using this autoloader.
*
* @code
* // Enable the autoloader.
* Autoloader::useAutoloader();
* @endcode
*
* This is a special-purpose autoloader for loading
* only the OpenStack classes. It will not attempt to
* autoload anything outside of the OpenStack namespace.
*
* Because this is a special-purpose autoloader, it
* should be safe to use with other special-purpose
* autoloaders (and also projects that don't
* rely upon autoloaders).
*
* @param string $klass
* The fully qualified name of the class to be autoloaded.
*/
public static function autoload($klass) {
$components = explode('\\', $klass);
if (empty($components[0])) {
array_shift($components);
}
// This class loader ONLY loads
// our classes. A general purpose
// classloader should be used for
// more sophisticated needs.
if ($components[0] != 'OpenStack') {
return;
}
// We need the path up to, but not including, the root OpenStack dir:
$loc = DIRECTORY_SEPARATOR . 'OpenStack';
$local_path = substr(self::$basedir, 0, strrpos(self::$basedir, $loc));
array_unshift($components, $local_path);
$path = implode(DIRECTORY_SEPARATOR, $components) . '.php';
if (file_exists($path)) {
require $path;
return;
}
}
}

View File

@ -18,8 +18,7 @@
* @file
* OpenStacl PHP-Client configuration.
*
* This file contains the PHP-Client autoloader. It also automatically
* register the OpenStack stream wrappers.
* It also automatically register the OpenStack stream wrappers.
*/
namespace OpenStack;
@ -33,8 +32,7 @@ use OpenStack\Exception;
* There is no requirement that this class be used. OpenStack is
* built to be flexible, and any individual component can be
* used directly, with one caveat: No explicit @c require or
* @c include calls are made. See the "autoloaders" discussion
* below.
* @c include calls are made.
*
* This class provides the following services:
*
@ -45,9 +43,6 @@ use OpenStack\Exception;
* - <em>Stream Wrappers:</em> This class can initialize a set of stream
* wrappers which will make certain OpenStack services available
* through the core PHP stream support.
* - <em>Autoloader:</em> It provides a special-purpose autoloader that can
* load the OpenStack classes, but which will not interfere with
* other autoloading facilities.
*
* <b>Configuration</b>
*
@ -78,26 +73,6 @@ use OpenStack\Exception;
* ?>
* @endcode
*
* <b>AUTOLOADING</b>
*
* OpenStack comes with a built-in autoloader that can be called like this:
*
* @code
* Bootstrap::useAutoloader();
* @endcode
*
* @attention
* The structure of the OpenStack file hierarchy is PSR-0 compliant.
* This means that you can use any standard PSR-0 classloader to
* load all of the classes here.
*
* That said, many projects rely upon packages to handle their own
* class loading. To provide this, this package contains a custom
* classloader that will load JUST the OpenStack classes. See
* the Bootstrap::useAutoloader() static method.
*
* See https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
*
* <b>STREAM WRAPPERS</b>
*
* Stream wrappers allow you to use the built-in file manipulation
@ -111,11 +86,6 @@ use OpenStack\Exception;
*/
class Bootstrap {
/**
* The directory where OpenStack is located.
*/
public static $basedir = __DIR__;
public static $config = array(
// The transport implementation. By default, we use the PHP stream
// wrapper's HTTP mechanism to process transactions.
@ -132,24 +102,6 @@ class Bootstrap {
*/
public static $identity = NULL;
/**
* Add the autoloader to PHP's autoloader list.
*
* This will add the internal special-purpose
* autoloader to the list of autoloaders that PHP will
* leverage to resolve class paths.
*
* Because OpenStack is PSR-0 compliant, any
* full PSR-0 classloader should be capable of loading
* these classes witout issue. You may prefer to use
* a standard PSR-0 loader instead of this one.
*
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
*/
public static function useAutoloader() {
spl_autoload_register(__NAMESPACE__ . '\Bootstrap::autoload');
}
/**
* Register stream wrappers for OpenStack.
*
@ -232,57 +184,6 @@ class Bootstrap {
self::$config = $array + self::$config;
}
/**
* OpenStack autoloader.
*
* An implementation of a PHP autoload function. Use
* OpenStack::useAutoloader() if you want PHP to automatically
* load classes using this autoloader.
*
* @code
* // Enable the autoloader.
* Bootstrap::useAutoloader();
* @endcode
*
* This is a special-purpose autoloader for loading
* only the OpenStack classes. It will not attempt to
* autoload anything outside of the OpenStack namespace.
*
* Because this is a special-purpose autoloader, it
* should be safe to use with other special-purpose
* autoloaders (and also projects that don't
* rely upon autoloaders).
*
* @param string $klass
* The fully qualified name of the class to be autoloaded.
*/
public static function autoload($klass) {
$components = explode('\\', $klass);
if (empty($components[0])) {
array_shift($components);
}
// This class loader ONLY loads
// our classes. A general purpose
// classloader should be used for
// more sophisticated needs.
if ($components[0] != 'OpenStack') {
return;
}
// We need the path up to, but not including, the root OpenStack dir:
$loc = DIRECTORY_SEPARATOR . 'OpenStack';
$local_path = substr(self::$basedir, 0, strrpos(self::$basedir, $loc));
array_unshift($components, $local_path);
$path = implode(DIRECTORY_SEPARATOR, $components) . '.php';
if (file_exists($path)) {
require $path;
return;
}
}
/**
* Get a configuration option.
*

View File

@ -30,6 +30,7 @@
namespace OpenStack\Tests;
require_once 'PHPUnit/Autoload.php';
require_once 'src/OpenStack/Autoloader.php';
require_once 'src/OpenStack/Bootstrap.php';
/**
@ -66,7 +67,7 @@ class TestCase extends \PHPUnit_Framework_TestCase {
}
\OpenStack\Bootstrap::useAutoloader();
\OpenStack\Autoloader::useAutoloader();
\OpenStack\Bootstrap::setConfiguration(self::$settings);
//parent::__construct($score, $locale, $adapter);

View File

@ -0,0 +1,48 @@
<?php
/* ============================================================================
(c) Copyright 2014 Hewlett-Packard Development Company, L.P.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
============================================================================ */
/**
* @file
*
* Unit tests for the Autoloader.
*/
namespace OpenStack\Tests;
require_once 'src/OpenStack/Autoloader.php';
require_once 'test/TestCase.php';
class AutoloaderTest extends \OpenStack\Tests\TestCase {
/**
* Test the BaseDir.
*/
public function testBasedir() {
$basedir = \OpenStack\Autoloader::$basedir;
$this->assertRegExp('/OpenStack/', $basedir);
}
/**
* Test the autoloader.
*/
public function testAutoloader() {
\OpenStack\Autoloader::useAutoloader();
// If we can construct a class, we are okay.
$test = new \OpenStack\Exception("TEST");
$this->assertInstanceOf('\OpenStack\Exception', $test);
}
}

View File

@ -21,7 +21,6 @@
*/
namespace OpenStack\Tests;
require_once 'src/OpenStack/Bootstrap.php';
require_once 'test/TestCase.php';
class BootstrapTest extends \OpenStack\Tests\TestCase {
@ -32,24 +31,4 @@ class BootstrapTest extends \OpenStack\Tests\TestCase {
public function testSettings() {
$this->assertTrue(!empty(self::$settings));
}
/**
* Test the BaseDir.
*/
public function testBasedir() {
$basedir = \OpenStack\Bootstrap::$basedir;
$this->assertRegExp('/OpenStack/', $basedir);
}
/**
* Test the autoloader.
*/
public function testAutoloader() {
\OpenStack\Bootstrap::useAutoloader();
// If we can construct a class, we are okay.
$test = new \OpenStack\Exception("TEST");
$this->assertInstanceOf('\OpenStack\Exception', $test);
}
}