Skip to content

Commit

Permalink
Merge pull request #262 from khoaofgod/final
Browse files Browse the repository at this point in the history
Fix Autoload Confict
  • Loading branch information
khoaofgod committed Mar 26, 2016
2 parents 60f0a98 + 81a52a1 commit 39a25bc
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 31 deletions.
22 changes: 22 additions & 0 deletions examples/autoload.Problem.And.Legacy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

/*
* As reported, HOA Project cause some conflict problem with Autoload of Composer.
* This is not phpFastCache problem, we used same autoload with Google Re-Capcha.
* So, Until HOA, or the your project fixed the autoload.
* All you need is put this line on your config.
*/

// In your Setting / Config.php or Index.php
define("PHPFASTCACHE_LEGACY",true);

// If you use composer, then it auto included our "src/phpFastCache/phpFastCache.php" on vendor folder already, you don't need to do anything else

require_once __DIR__.'/../src/autoload.php';

// run Files Example
require_once __DIR__.'/files.php';

/*
* It also bring back the __c() legacy function
*/
43 changes: 43 additions & 0 deletions src/phpFastCache/Util/Legacy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/*
* If Any problem with Autoload on other project
* Try to put this line on your config project
* define("PHPFASTCACHE_LEGACY",true);
* and just keep include phpFastCache/phpFastCache.php or Composer Autoloader
*/

use phpFastCache\CacheManager;

require_once __DIR__.'/../Core/DriverInterface.php';
require_once __DIR__.'/../Core/DriverAbstract.php';
require_once __DIR__.'/../Core/phpFastCache.php';
require_once __DIR__.'/../Core/phpFastCacheExtensions.php';
require_once __DIR__.'/../Exceptions/phpFastCacheCoreException.php';
require_once __DIR__.'/../Exceptions/phpFastCacheDriverException.php';

require_once __DIR__.'/../Drivers/files.php';
require_once __DIR__.'/../Drivers/memcache.php';
require_once __DIR__.'/../Drivers/memcached.php';
require_once __DIR__.'/../Drivers/mongodb.php';
require_once __DIR__.'/../Drivers/predis.php';
require_once __DIR__.'/../Drivers/redis.php';
require_once __DIR__.'/../Drivers/sqlite.php';

require_once __DIR__.'/../CacheManager.php';
require_once __DIR__.'/../phpFastCache.php';


/**
* __c() Short alias
* @param string $storage
* @param array $config
* @return mixed
*/
if (!function_exists("__c")) {
function __c($storage = 'auto', $config = array())
{
return CacheManager::getInstance($storage, $config);
}
}


56 changes: 25 additions & 31 deletions src/phpFastCache/phpFastCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,32 @@

use phpFastCache\CacheManager;
define('PHP_EXT', substr(strrchr(__FILE__, '.'), 1));
/**
* Register Autoload
*/
spl_autoload_register(function ($entity) {
// Explode is faster than substr & strstr also more control
$module = explode('\\',$entity,2);
if ($module[0] !== 'phpFastCache') {
/**
* Not a part of phpFastCache file
* then we return here.
*/
return;
}

$entity = str_replace('\\', '/', $module[1]);
$path = __DIR__ . '/' . $entity . '.' . PHP_EXT;
if (is_readable($path)) {
require_once $path;
}
});
if(!defined("PHPFASTCACHE_LEGACY")) {
/**
* Register Autoload
*/
spl_autoload_register(function ($entity) {
// Explode is faster than substr & strstr also more control
$module = explode('\\',$entity,2);
if ($module[0] !== 'phpFastCache') {
/**
* Not a part of phpFastCache file
* then we return here.
*/
return;
}

$entity = str_replace('\\', '/', $module[1]);
$path = __DIR__ . '/' . $entity . '.' . PHP_EXT;
if (is_readable($path)) {
require_once $path;
}
});

} else {
require_once __DIR__.'/Util/Legacy.php';
}

/**
* phpFastCache() Full alias
Expand All @@ -47,15 +53,3 @@ function phpFastCache($storage = 'auto', $config = array())
return CacheManager::getInstance($storage, $config);
}
}
/**
* __c() Short alias
* @param string $storage
* @param array $config
* @return mixed
*/
if (!function_exists("__c")) {
function __c($storage = 'auto', $config = array())
{
return CacheManager::getInstance($storage, $config);
}
}

0 comments on commit 39a25bc

Please sign in to comment.