Skip to content

Commit

Permalink
Bug #4/#5, Fix use of glob() for Windows - SubClasses [iet:3699692]
Browse files Browse the repository at this point in the history
* Re-factor SubClasses - plugin auto-discovery [iet:3703744]
  • Loading branch information
nfreear committed Jun 26, 2015
1 parent 6905dc8 commit a3fcf72
Showing 1 changed file with 39 additions and 18 deletions.
57 changes: 39 additions & 18 deletions src/SubClasses/SubClasses.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ class SubClasses extends OffsetIterator
const PLUGIN_INTERFACE = '\\IET_OU\\SubClasses\\PluginInterface';
const REGISTER_FN = 'registerPlugin';

public static $verbose = false;
public static $verbose = true; //false;
protected $classes = array();

protected static $skip_me = array('\\Wikimedia\\Composer\\MergePlugin', '\\Nfreear\\Composer\\Suggest');


public function __construct($offset = self::PHP_CORE_OFFSET)
{
Expand Down Expand Up @@ -68,6 +70,11 @@ public function get_oembed_providers()
return $this->match('IET_OU\\Open_Media_Player\\Oembed_Provider');
}

public function get_player_themes()
{
return $this->match('IET_OU\\Open_Media_Player\\Media_Player_Theme');
}


protected function getPsr4Paths()
{
Expand All @@ -79,32 +86,46 @@ protected function getPsr4Paths()
$path = __DIR__ .'/../../vendor/composer/autoload_psr4.php';
$psr4 = require $path;
}
$flat_paths = call_user_func_array('array_merge', $psr4);
//$flat_paths = call_user_func_array('array_merge', $psr4);
return $psr4;
}


protected function discoverClasses()
{
$psr4 = $this->getPsr4Paths();
$psr4_paths = $this->getPsr4Paths();

foreach ($psr4 as $namespace => $paths) {
$glob = sprintf('{%s/*.php}', implode('/*.php,', $paths));
foreach ($psr4_paths as $namespace => $paths) {
//$glob = sprintf('{%s/*.php}', implode('/*.php,', $paths)); // Not on Windows!
//$glob = $paths[ 0 ] .'/*.php';
$this->debug('Glob: '. $glob);
$files = glob($glob, GLOB_BRACE | GLOB_MARK);
foreach ($files as $path) {
$name = basename($path, '.php');
$class = '\\' . $namespace . $name;
//if ('MergePlugin' == $name || 'Suggest' == $name) continue;
try {
$reflect = new \ReflectionClass($class);
if ($reflect->implementsInterface(self::PLUGIN_INTERFACE)
&& $reflect->isInstantiable()) {
$this->classes[] = $class; //ltrim($class, '\\');
foreach ($paths as $file_path) {
$glob = $file_path .'/*.php';
$this->debug('Glob: '. $glob);
$files = glob($glob, GLOB_MARK);
foreach ($files as $path) {
$name = basename($path, '.php');
$class = '\\' . $namespace . $name;

// A hack :(!
if (in_array($class, self::$skip_me)) {
$this->debug("Skipping class '$class'");
continue;
}

$this->debug($class, $path);

try {
if (class_exists($class) && is_subclass_of($class, self::PLUGIN_INTERFACE))
/*$reflect = new \ReflectionClass($class);
if ($reflect->implementsInterface(self::PLUGIN_INTERFACE)
&& $reflect->isInstantiable())*/ {
$this->classes[] = $class; //ltrim($class, '\\');
}
} catch(ErrorException $e) {
$this->debug('XX Warning! '. $e->getMessage());
} catch (\Exception $e) {
$this->debug('Warning! '. $e->getMessage());
}
} catch (\Exception $e) {
$this->debug('Warning! '. $e->getMessage());
}
}
}
Expand Down

0 comments on commit a3fcf72

Please sign in to comment.