Skip to content
This repository has been archived by the owner on Sep 20, 2019. It is now read-only.

Commit

Permalink
Merge pull request #8 from wmde/revertautoloadcommit
Browse files Browse the repository at this point in the history
Revert "Improve autoloading code"
  • Loading branch information
addshore committed Nov 18, 2013
2 parents 6c6af93 + ffe7088 commit 0dc2539
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 21 deletions.
1 change: 0 additions & 1 deletion .travis.yml
Expand Up @@ -35,7 +35,6 @@ script:
- if [ "$TESTSUITE" == "MediaWiki" ] ; then phpunit --coverage-clover build/logs/clover.xml ; fi

after_script:
- composer require satooshi/php-coveralls dev-master
- php vendor/bin/coveralls -v

notifications:
Expand Down
8 changes: 4 additions & 4 deletions WikibaseDatabase.mw.php
Expand Up @@ -12,10 +12,6 @@
die( 'Not an entry point.' );
}

if ( is_readable( __DIR__ . '/vendor/autoload.php' ) ) {
require_once( __DIR__ . '/vendor/autoload.php' );
}

$GLOBALS['wgExtensionCredits']['wikibase'][] = array(
'path' => __DIR__,
'name' => 'Wikibase Database',
Expand All @@ -30,6 +26,10 @@

$GLOBALS['wgExtensionMessagesFiles']['WikibaseDatabase'] = __DIR__ . '/WikibaseDatabase.i18n.php';

if ( defined( 'MW_PHPUNIT_TEST' ) ) {
require_once __DIR__ . '/tests/testLoader.php';
}

/**
* Hook to add PHPUnit test cases.
* @see https://www.mediawiki.org/wiki/Manual:Hooks/UnitTestsList
Expand Down
52 changes: 44 additions & 8 deletions WikibaseDatabase.php
Expand Up @@ -3,18 +3,54 @@
/**
* Entry point of the Wikibase Database component.
*
* @codeCoverageIgnore
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/

if ( !defined( 'WIKIBASE_DATABASE_VERSION' ) ) {
define( 'WIKIBASE_DATABASE_VERSION', '0.2 alpha' );
if ( defined( 'WIKIBASE_DATABASE_VERSION' ) ) {
// Do not initialize more then once.
return;
}

define( 'WIKIBASE_DATABASE_VERSION', '0.1' );

spl_autoload_register( function ( $className ) {
$className = ltrim( $className, '\\' );
$fileName = '';
$namespace = '';

if ( $lastNsPos = strripos( $className, '\\') ) {
$namespace = substr( $className, 0, $lastNsPos );
$className = substr( $className, $lastNsPos + 1 );
$fileName = str_replace( '\\', '/', $namespace ) . '/';
}

$fileName .= str_replace( '_', '/', $className ) . '.php';

$namespaceSegments = explode( '\\', $namespace );

$inQueryEngineNamespace = count( $namespaceSegments ) > 1
&& $namespaceSegments[0] === 'Wikibase'
&& $namespaceSegments[1] === 'Database';

if ( defined( 'MEDIAWIKI' ) ) {
call_user_func( function() {
require_once __DIR__ . '/WikibaseDatabase.mw.php';
} );
if ( $inQueryEngineNamespace ) {
$inTestNamespace = count( $namespaceSegments ) > 2 && $namespaceSegments[2] === 'Tests';

if ( !$inTestNamespace ) {
$pathParts = explode( '/', $fileName );
array_shift( $pathParts );
array_shift( $pathParts );
$fileName = implode( '/', $pathParts );

require_once __DIR__ . '/src/' . $fileName;
}
}
} );

// @codeCoverageIgnoreStart
if ( defined( 'MEDIAWIKI' ) ) {
call_user_func( function() {
require_once __DIR__ . '/WikibaseDatabase.mw.php';
} );
}
// @codeCoverageIgnoreEnd
6 changes: 3 additions & 3 deletions composer.json
Expand Up @@ -28,10 +28,10 @@
"require": {
"php": ">=5.3.2"
},
"require-dev": {
"satooshi/php-coveralls": "dev-master"
},
"autoload": {
"classmap": [
"src"
],
"files" : [
"WikibaseDatabase.php"
]
Expand Down
8 changes: 3 additions & 5 deletions tests/bootstrap.php
Expand Up @@ -9,12 +9,10 @@
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/

if ( !is_readable( __DIR__ . '/../vendor/autoload.php' ) ) {
die( 'You need to install this package with Composer before you can run the tests' );
}

if ( !in_array( '--testsuite=WikibaseDatabaseStandalone', $GLOBALS['argv'] ) ) {
require_once( __DIR__ . '/evilMediaWikiBootstrap.php' );
}

require_once( __DIR__ . '/../vendor/autoload.php' );
require_once( __DIR__ . '/../WikibaseDatabase.php' );

require_once( __DIR__ . '/testLoader.php' );
41 changes: 41 additions & 0 deletions tests/testLoader.php
@@ -0,0 +1,41 @@
<?php

/**
* Test class autoloader for the Wikibase Database component.
*
* @since 0.1
*
* @licence GNU GPL v2+
* @author Jeroen De Dauw < jeroendedauw@gmail.com >
*/

spl_autoload_register( function ( $className ) {
$className = ltrim( $className, '\\' );
$fileName = '';
$namespace = '';

if ( $lastNsPos = strripos( $className, '\\') ) {
$namespace = substr( $className, 0, $lastNsPos );
$className = substr( $className, $lastNsPos + 1 );
$fileName = str_replace( '\\', '/', $namespace ) . '/';
}

$fileName .= str_replace( '_', '/', $className ) . '.php';

$namespaceSegments = explode( '\\', $namespace );

$inTestNamespace = count( $namespaceSegments ) > 2
&& $namespaceSegments[0] === 'Wikibase'
&& $namespaceSegments[1] === 'Database'
&& $namespaceSegments[2] === 'Tests';

if ( $inTestNamespace ) {
$pathParts = explode( '/', $fileName );
array_shift( $pathParts );
array_shift( $pathParts );
array_shift( $pathParts );
$fileName = implode( '/', $pathParts );

require_once __DIR__ . '/phpunit/' . $fileName;
}
} );

0 comments on commit 0dc2539

Please sign in to comment.