Skip to content

Commit

Permalink
Fix: Autoload with composer
Browse files Browse the repository at this point in the history
  • Loading branch information
localheinz committed Nov 22, 2014
1 parent be92b61 commit 6992fe8
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 73 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@ php:
- 5.4
- 5.5

env:
- ZF2_PATH=./vendor/zendframework/zendframework/library

script:
- phpunit --configuration test/phpunit.xml
18 changes: 2 additions & 16 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,7 @@

namespace ZfcBase;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;

class Module implements AutoloaderProviderInterface
class Module
{
public function getAutoloaderConfig()
{
return array(
'Zend\Loader\ClassMapAutoloader' => array(
__DIR__ . '/autoload_classmap.php',
),
'Zend\Loader\StandardAutoloader' => array(
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/src/' . __NAMESPACE__,
),
),
);
}

}
11 changes: 0 additions & 11 deletions autoload_classmap.php

This file was deleted.

5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,10 @@
"classmap": [
"./Module.php"
]
},
"autoload-dev": {
"psr-0": {
"ZfcBaseTest\\": "test/"
}
}
}
1 change: 0 additions & 1 deletion test/TestConfig.php.dist
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php
define('ZF2_PATH', getenv('HOME') . '/workspace/zf2/library');

return array(
'modules' => array(
Expand Down
45 changes: 4 additions & 41 deletions test/Bootstrap.php → test/ZfcBaseTest/Bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
<?php

namespace ZfcBaseTest;

use Zend\Loader\AutoloaderFactory;
use Zend\Mvc\Service\ServiceManagerConfig;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\ArrayUtils;
use RuntimeException;

error_reporting(E_ALL | E_STRICT);
chdir(__DIR__);

class Bootstrap
{
Expand All @@ -17,10 +13,10 @@ class Bootstrap
public static function init()
{
// Load the user-defined test configuration file, if it exists; otherwise, load
if (is_readable(__DIR__ . '/TestConfig.php')) {
$testConfig = include __DIR__ . '/TestConfig.php';
if (is_readable('test/TestConfig.php')) {
$testConfig = include 'test/TestConfig.php';
} else {
$testConfig = include __DIR__ . '/TestConfig.php.dist';
$testConfig = include 'test/TestConfig.php.dist';
}

$zf2ModulePaths = array(dirname(dirname(__DIR__)));
Expand All @@ -34,8 +30,6 @@ public static function init()
$zf2ModulePaths = implode(PATH_SEPARATOR, $zf2ModulePaths) . PATH_SEPARATOR;
$zf2ModulePaths .= getenv('ZF2_MODULES_TEST_PATHS') ?: (defined('ZF2_MODULES_TEST_PATHS') ? ZF2_MODULES_TEST_PATHS : '');

static::initAutoloader();

// use ModuleManager to load this module and it's dependencies
$baseConfig = array(
'module_listener_options' => array(
Expand All @@ -56,35 +50,6 @@ public static function getServiceManager()
return static::$serviceManager;
}

protected static function initAutoloader()
{
$vendorPath = static::findParentPath('vendor');

if (is_readable($vendorPath . '/autoload.php')) {
$loader = include $vendorPath . '/autoload.php';
}

$zf2Path = getenv('ZF2_PATH') ?: (defined('ZF2_PATH') ? ZF2_PATH : (is_dir($vendorPath . '/ZF2/library') ? $vendorPath . '/ZF2/library' : false));

if (!$zf2Path) {
throw new RuntimeException('Unable to load ZF2. Run `php composer.phar install` or define a ZF2_PATH environment variable.');
}

if (isset($loader)) {
$loader->add('Zend', $zf2Path . '/Zend');
} else {
include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
AutoloaderFactory::factory(array(
'Zend\Loader\StandardAutoloader' => array(
'autoregister_zf' => true,
'namespaces' => array(
__NAMESPACE__ => __DIR__ . '/' . __NAMESPACE__,
),
),
));
}
}

protected static function findParentPath($path)
{
$dir = __DIR__;
Expand All @@ -97,5 +62,3 @@ protected static function findParentPath($path)
return $dir . '/' . $path;
}
}

Bootstrap::init();
11 changes: 11 additions & 0 deletions test/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

use ZfcBaseTest\Bootstrap;

error_reporting(E_ALL | E_STRICT);

chdir(dirname(__DIR__));

require_once 'vendor/autoload.php';

Bootstrap::init();
2 changes: 1 addition & 1 deletion test/phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<phpunit bootstrap="./Bootstrap.php" colors="true">
<phpunit bootstrap="./bootstrap.php" colors="true">
<testsuites>
<testsuite name="ZfcBase Module Tests">
<directory>./ZfcBaseTest</directory>
Expand Down

0 comments on commit 6992fe8

Please sign in to comment.