Delete custom autoloader
Change-Id: Ib63e4aa7507987e89ed9ed63f0130d4f8d3e5c90
This commit is contained in:
parent
061a3b784a
commit
b54de957d4
@ -52,13 +52,7 @@
|
|||||||
* The super-simple stream API:
|
* The super-simple stream API:
|
||||||
*
|
*
|
||||||
* <?php
|
* <?php
|
||||||
* // This is only required if you don't have a PSR-4
|
* require 'vendor/autoload.php';
|
||||||
* // autoloader to do the hard work for you.
|
|
||||||
* require 'OpenStack/Autoloader.php';
|
|
||||||
*
|
|
||||||
* // If you aren't using a PSR-4 autoloader,
|
|
||||||
* // you might want to use this:
|
|
||||||
* \OpenStack\Autoloader::useAutoloader();
|
|
||||||
*
|
*
|
||||||
* // Turn on stream wrappers.
|
* // Turn on stream wrappers.
|
||||||
* \OpenStack\Bootstrap::useStreamWrappers();
|
* \OpenStack\Bootstrap::useStreamWrappers();
|
||||||
@ -101,13 +95,7 @@
|
|||||||
* to log in and then dump a list of services that are available to you:
|
* to log in and then dump a list of services that are available to you:
|
||||||
*
|
*
|
||||||
* <?php
|
* <?php
|
||||||
* // This is only required if you don't have a PSR-4
|
* require 'vendor/autoload.php';
|
||||||
* // autoloader to do the hard work for you.
|
|
||||||
* require 'OpenStack/Autoloader.php';
|
|
||||||
*
|
|
||||||
* // If you aren't using a PSR-4 autoloader,
|
|
||||||
* // you might want to use this:
|
|
||||||
* \OpenStack\Autoloader::useAutoloader();
|
|
||||||
*
|
*
|
||||||
* use \OpenStack\Identity\v1\IdentityService;
|
* use \OpenStack\Identity\v1\IdentityService;
|
||||||
*
|
*
|
||||||
|
@ -1,13 +1,10 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/../src/OpenStack/Autoloader.php';
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
use \OpenStack\Autoloader;
|
|
||||||
use \OpenStack\Identity\v2\IdentityService;
|
use \OpenStack\Identity\v2\IdentityService;
|
||||||
use \OpenStack\ObjectStore\v1\ObjectStorage;
|
use \OpenStack\ObjectStore\v1\ObjectStorage;
|
||||||
use \OpenStack\ObjectStore\v1\ObjectStorage\Object;
|
use \OpenStack\ObjectStore\v1\ObjectStorage\Object;
|
||||||
|
|
||||||
Autoloader::useAutoloader();
|
|
||||||
|
|
||||||
// Load these from an ini file.
|
// Load these from an ini file.
|
||||||
$ini = parse_ini_file(getenv('HOME') . '/.OpenStack.ini');
|
$ini = parse_ini_file(getenv('HOME') . '/.OpenStack.ini');
|
||||||
$username = $ini['username'];
|
$username = $ini['username'];
|
||||||
|
@ -85,36 +85,22 @@ The first thing to do in your application is make sure the OpenStack
|
|||||||
library is bootstrapped. When we say "bootstrap", what we really mean is
|
library is bootstrapped. When we say "bootstrap", what we really mean is
|
||||||
letting the library initialize itself.
|
letting the library initialize itself.
|
||||||
|
|
||||||
Bootstrapping does not always involve any manual interaction on your
|
The only thing you will need to do is require Composer's PSR-compliant
|
||||||
part. If you are using an PSR-4 autoloader that knows of the OpenStack
|
autoloader, like so:
|
||||||
directory, that is enough for the system to initialize itself.
|
|
||||||
|
|
||||||
Sometimes, though, you will need to bootstrap OpenStack in your own code,
|
|
||||||
and this is done as follows:
|
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
require_once 'OpenStack/Autoloader.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
use \OpenStack\Autoloader;
|
|
||||||
use \OpenStack\Bootstrap;
|
use \OpenStack\Bootstrap;
|
||||||
use \OpenStack\Identity\v2\IdentityService;
|
use \OpenStack\Identity\v2\IdentityService;
|
||||||
use \OpenStack\ObjectStore\v1\ObjectStorage;
|
use \OpenStack\ObjectStore\v1\ObjectStorage;
|
||||||
use \OpenStack\ObjectStore\v1\Resource\Object;
|
use \OpenStack\ObjectStore\v1\Resource\Object;
|
||||||
|
|
||||||
\OpenStack\Autoloader::useAutoloader();
|
The first line should be self-explanatory: we require the main autoloader file
|
||||||
?>
|
which is generated by Composer.
|
||||||
|
|
||||||
The first line should be self-explanatory: We require the main
|
After that, we declare a list of namespaces that we will use. This way we can
|
||||||
`Bootstrap.php` file (which contains the `Bootstrap` class).
|
refer to classes by their short name, rather than by their fully qualified name.
|
||||||
|
|
||||||
After that, we declare a list of namespaced objects that we will use.
|
|
||||||
This way we can refer to them by their short name, rather than by their
|
|
||||||
fully qualified name.
|
|
||||||
|
|
||||||
The last line initializes the built-in OpenStack autoloader. What does
|
|
||||||
this mean? It means that this is the only `require` or `include`
|
|
||||||
statement you need in your code. The library does the rest of the
|
|
||||||
including for you, on demand, in a performance-sensitive way.
|
|
||||||
|
|
||||||
There are some other fancy things that OpenStack::Bootstrap can do for
|
There are some other fancy things that OpenStack::Bootstrap can do for
|
||||||
you. Most notably, you can pass configuration parameters into it. But
|
you. Most notably, you can pass configuration parameters into it. But
|
||||||
|
@ -1,9 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/../src/OpenStack/Autoloader.php';
|
require_once __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
use \OpenStack\Autoloader;
|
|
||||||
use \OpenStack\Bootstrap;
|
use \OpenStack\Bootstrap;
|
||||||
Autoloader::useAutoloader();
|
|
||||||
Bootstrap::useStreamWrappers();
|
Bootstrap::useStreamWrappers();
|
||||||
|
|
||||||
$ini = parse_ini_file(getenv('HOME') . '/.OpenStack.ini');
|
$ini = parse_ini_file(getenv('HOME') . '/.OpenStack.ini');
|
||||||
|
@ -51,24 +51,17 @@ 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:
|
use the autoloader in that file to load the rest of the library:
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
require_once '/OpenStack/Autoloader.php';
|
require 'vendor/autoload.php';
|
||||||
|
|
||||||
use \OpenStack\Autoloader;
|
|
||||||
use \OpenStack\Bootstrap;
|
use \OpenStack\Bootstrap;
|
||||||
Autoloader::useAutoloader();
|
|
||||||
Bootstrap::useStreamWrappers();
|
|
||||||
?>
|
|
||||||
|
|
||||||
The first thing the example above does is require the Autoloader.php
|
Bootstrap::useStreamWrappers();
|
||||||
|
|
||||||
|
The first thing the example above does is require Composer's autoloader
|
||||||
file, which contains code necessary to autoload anything else we will need.
|
file, which contains code necessary to autoload anything else we will need.
|
||||||
|
|
||||||
Next, we call two static methods:
|
Next, we call Bootstrap::useStreamWrappers(), which tells OpenStack to register
|
||||||
|
its stream wrapper classes.
|
||||||
- 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
|
|
||||||
stream wrapper classes.
|
|
||||||
|
|
||||||
In a nutshell, PHP allows libraries to map a particular URL pattern to a
|
In a nutshell, PHP allows libraries to map a particular URL pattern to a
|
||||||
stream wrapper. PHP-Client registers the `swift://` URL prefix. So any
|
stream wrapper. PHP-Client registers the `swift://` URL prefix. So any
|
||||||
@ -181,11 +174,10 @@ tokens in a database and re-using them).
|
|||||||
Here's how a stream context is used:
|
Here's how a stream context is used:
|
||||||
|
|
||||||
<?php
|
<?php
|
||||||
require_once __DIR__ . '/../src/OpenStack/Autoloader.php';
|
require __DIR__ . '/../vendor/autoload.php';
|
||||||
|
|
||||||
use \OpenStack\Autoloader;
|
|
||||||
use \OpenStack\Bootstrap;
|
use \OpenStack\Bootstrap;
|
||||||
Autoloader::useAutoloader();
|
|
||||||
Bootstrap::useStreamWrappers();
|
Bootstrap::useStreamWrappers();
|
||||||
|
|
||||||
$cxt = stream_context_create(array(
|
$cxt = stream_context_create(array(
|
||||||
|
@ -1,120 +0,0 @@
|
|||||||
<?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.
|
|
||||||
============================================================================ */
|
|
||||||
/**
|
|
||||||
* 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-4 compatible autoloader
|
|
||||||
* to use, this one can be used.
|
|
||||||
*
|
|
||||||
* The autoloader can be used like:
|
|
||||||
*
|
|
||||||
* Autoloader::useAutoloader();
|
|
||||||
*
|
|
||||||
* The structure of the OpenStack file hierarchy is PSR-4 compliant.
|
|
||||||
* This means that you can use any standard PSR-4 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-4.md
|
|
||||||
*/
|
|
||||||
Class Autoloader {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @var string 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-4 compliant, any
|
|
||||||
* full PSR-4 classloader should be capable of loading
|
|
||||||
* these classes witout issue. You may prefer to use
|
|
||||||
* a standard PSR-4 loader instead of this one.
|
|
||||||
*
|
|
||||||
* @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-4.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.
|
|
||||||
*
|
|
||||||
* // Enable the autoloader.
|
|
||||||
* Autoloader::useAutoloader();
|
|
||||||
*
|
|
||||||
* 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
@ -21,7 +21,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
$base = dirname(__DIR__);
|
$base = dirname(__DIR__);
|
||||||
require_once $base . '/vendor/autoloader.php';
|
require_once $base . '/vendor/autoload.php';
|
||||||
|
|
||||||
use \OpenStack\ObjectStore\v1\ObjectStorage;
|
use \OpenStack\ObjectStore\v1\ObjectStorage;
|
||||||
use \OpenStack\Identity\v2\IdentityService;
|
use \OpenStack\Identity\v2\IdentityService;
|
||||||
@ -33,7 +33,6 @@ $config = [
|
|||||||
'transport.ssl.verify' => 0,
|
'transport.ssl.verify' => 0,
|
||||||
];
|
];
|
||||||
|
|
||||||
\OpenStack\Autoloader::useAutoloader();
|
|
||||||
\OpenStack\Bootstrap::setConfiguration($config);
|
\OpenStack\Bootstrap::setConfiguration($config);
|
||||||
|
|
||||||
$help = "Authenticate against OpenStack Identity Service.
|
$help = "Authenticate against OpenStack Identity Service.
|
||||||
|
@ -1,48 +0,0 @@
|
|||||||
<?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.
|
|
||||||
============================================================================ */
|
|
||||||
/**
|
|
||||||
* Unit tests for the Autoloader.
|
|
||||||
*/
|
|
||||||
namespace OpenStack\Tests;
|
|
||||||
|
|
||||||
|
|
||||||
require_once 'src/OpenStack/Autoloader.php';
|
|
||||||
|
|
||||||
class AutoloaderTest extends 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\Common\Exception("TEST");
|
|
||||||
|
|
||||||
$this->assertInstanceOf('\OpenStack\Common\Exception', $test);
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user