Browse files

Removed Zend Autoloader in favor of simpler autoloader

  • Loading branch information...
1 parent 39f7ce7 commit 610b0b0ead41ac634e7f192de2f53bdf54b35234 Ralph Schindler committed Jun 15, 2011
Showing with 24 additions and 21 deletions.
  1. +1 −2 examples/di/02/_main_.php
  2. +2 −4 examples/di/03/_main_.php
  3. +1 −2 examples/di/04/_main_.php
  4. +1 −2 examples/di/05/_main_.php
  5. +1 −3 examples/di/06/_main_.php
  6. +18 −8 run.php
View
3 examples/di/02/_main_.php
@@ -1,8 +1,7 @@
<?php
function _main_() {
- global $autoloader;
- $autoloader->registerNamespace('My', __DIR__ . '/My/');
+ simple_autoloader_register('My', __DIR__);
if (!file_exists(__DIR__ . '/di-definition.php')) {
echo 'COMPILING DEFINITION (run again to delete di-definition.php)' . PHP_EOL;
View
6 examples/di/03/_main_.php
@@ -1,9 +1,7 @@
<?php
function _main_() {
- global $autoloader;
-
- $autoloader->registerNamespace('My', __DIR__ . '/My/');
+ simple_autoloader_register('My', __DIR__, true);
if (!class_exists('My\DiDefinition', true)) {
echo 'COMPILING DEFINITION (and writing to disk at My\DiDefinition.php)' . PHP_EOL;
@@ -21,7 +19,7 @@ function _main_() {
));
file_put_contents(__DIR__ . '/My/DiDefinition.php', $codeGenerator->generate());
- unset($compiler, $definition, $codeGenerator, $class, $method);
+ unset($compiler, $definition, $codeGenerator, $class);
} else {
echo 'USING DEFINITION' . PHP_EOL;
}
View
3 examples/di/04/_main_.php
@@ -2,8 +2,7 @@
function _main_() {
- global $autoloader;
- $autoloader->registerNamespace('My', __DIR__ . '/My/');
+ simple_autoloader_register('My', __DIR__);
$compiler = new Zend\Di\Definition\Compiler();
$compiler->addCodeScannerDirectory(new Zend\Code\Scanner\DirectoryScanner(__DIR__ . '/My/'));
View
3 examples/di/05/_main_.php
@@ -1,8 +1,7 @@
<?php
function _main_() {
- global $autoloader;
- $autoloader->registerNamespace('My', __DIR__ . '/My/');
+ simple_autoloader_register('My', __DIR__);
$di = new Zend\Di\DependencyInjector();
$im = $di->getInstanceManager();
View
4 examples/di/06/_main_.php
@@ -1,9 +1,7 @@
<?php
function _main_() {
- global $autoloader;
-
- $autoloader->registerNamespace('My', __DIR__ . '/My/');
+ simple_autoloader_register('My', __DIR__);
$configValues = new Zend\Config\Ini(__DIR__ . '/di-config.ini', 'production');
$diConfig = new Zend\Di\Configuration($configValues->di);
View
26 run.php
@@ -1,20 +1,14 @@
<?php
// Defint the ZF2 Path
-define('ZF_PATH', __DIR__ . '/../ZFGit/library/');
+define('ZF_PATH', realpath(__DIR__ . '/../ZFGit/library/'));
// error_reporting & display_errors
error_reporting(32767);
ini_set('display_errors', 1);
// bootstrap
-$autoloaderFile = ZF_PATH . 'Zend/Loader/StandardAutoloader.php';
-if (stream_resolve_include_path($autoloaderFile) === false && file_exists($autoloaderFile) === false) {
- die('You must first set the ZF_PATH constant to your ZF2 library');
-}
-require_once $autoloaderFile;
-$autoloader = new Zend\Loader\StandardAutoloader;
-$autoloader->register();
+simple_autoloader_register('Zend', ZF_PATH);
$exampleToRun = $_SERVER['argv'][1];
@@ -23,4 +17,20 @@
}
include __DIR__ . DIRECTORY_SEPARATOR . ltrim($exampleToRun, '\\/');
+
_main_();
+
+
+
+/**
+ * Functions
+ */
+
+function simple_autoloader_register($namespace, $directory, $checkFile = false) {
+ spl_autoload_register(function ($class) use ($namespace, $directory, $checkFile) {
+ if (strpos($class, $namespace . '\\') !== 0) return;
+ $file = rtrim($directory, '\\/') . DIRECTORY_SEPARATOR . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
+ if ($checkFile && !file_exists($file)) return;
+ return include $file;
+ });
+}

0 comments on commit 610b0b0

Please sign in to comment.