Skip to content
This repository has been archived by the owner on Nov 2, 2018. It is now read-only.

Commit

Permalink
Fixed broken directory scanning for plugin files
Browse files Browse the repository at this point in the history
Cleaned up ``Zepto\FileLoader\PluginLoader`` and test class
  • Loading branch information
hassankhan committed Nov 18, 2013
1 parent 6e40692 commit b8c247a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 50 deletions.
37 changes: 17 additions & 20 deletions library/Zepto/FileLoader/PluginLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@

class PluginLoader extends \Zepto\FileLoader {

// public function __construct($plugin_dir)
// {
// parent::__construct();
// $this->plugin_dir = $plugin_dir;
// }

/**
* Loads in a single file or all files in a directory if $file_path is a folder
*
Expand All @@ -37,9 +43,6 @@ public function load($file_path, $file_extension)
// Strip extraneous path details
$file = str_replace(ROOT_DIR, '', $file_path);

// Get plugin name and add to local cache variable
$matches = array();

if (preg_match_all('/^plugins\/([A-Z]+\w+)Plugin.php$/', $file, $matches) === false) {
throw new Exception('You didn\'t name the fucking plugin correctly. Should be MyPluginNamePlugin.php');
}
Expand All @@ -54,28 +57,22 @@ public function load($file_path, $file_extension)

// For a directory
if (is_dir($file_path)) {
$iterator = new \RecursiveDirectoryIterator($file_path);
foreach(new \RecursiveIteratorIterator($iterator) as $file) {

// Include plugin file
include_once($file);

// Strip extraneous path details
$file = str_replace(ROOT_DIR, '', $file);
if ($handle = opendir(ROOT_DIR . 'plugins')) {
while (false !== ($entry = readdir($handle))) {

// Get plugin name and add to local cache variable
$matches = array();

if (preg_match_all('/^plugins\/([A-Z]+\w+)Plugin.php$/', $file, $matches) === false) {
throw new Exception('You didn\'t name the fucking plugin correctly. Should be MyPluginNamePlugin.php');
}
if (preg_match_all('/^([A-Z]+\w+)Plugin.php$/', $entry, $matches)) {

$plugin_name = $matches[1][0] . 'Plugin';
include_once(ROOT_DIR . 'plugins/' . $entry);

if (class_exists($plugin_name)) {
$plugin = new $plugin_name;
$loaded_plugins[$plugin_name] = $plugin;
$plugin_name = $matches[1][0] . 'Plugin';
if (class_exists($plugin_name)) {
$plugin = new $plugin_name;
$loaded_plugins[$plugin_name] = $plugin;
}
}
}
closedir($handle);
}
}

Expand Down
59 changes: 29 additions & 30 deletions tests/Zepto/PluginLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,23 @@ class PluginLoaderTest extends \PHPUnit_Framework_TestCase
*/
protected function setUp()
{
$this->object = new FileLoader\PluginLoader();
$this->object = new FileLoader\PluginLoader();

include_once(ROOT_DIR . 'plugins/ExamplePlugin.php');
include_once(ROOT_DIR . 'plugins/OtherExamplePlugin.php');

$plugin_1_name = 'ExamplePlugin';
$plugin_2_name = 'OtherExamplePlugin';

$plugin_1 = new $plugin_1_name;
$plugin_2 = new $plugin_2_name;

$this->plugins = array(
$plugin_1_name => $plugin_1,
$plugin_2_name => $plugin_2
);


}

/**
Expand Down Expand Up @@ -45,15 +61,13 @@ public function testLoad()
public function testLoadSingleFile()
{
$path = ROOT_DIR . 'plugins/ExamplePlugin.php';
$plugin_name = 'ExamplePlugin';

$actual = $this->object->load($path, array('php'));
$plugin = new $plugin_name;

$expected = array(
$plugin_name => $plugin
'ExamplePlugin' => $this->plugins['ExamplePlugin']
);

$this->assertTrue(class_exists($plugin_name));
$this->assertEquals($expected, $actual);
}

Expand All @@ -62,31 +76,16 @@ public function testLoadSingleFile()
*/
public function testLoadMultipleFiles()
{
// $files['sub/index.md'] = array(
// 'meta' => array(
// 'title' => 'Sub Page Index'
// ),
// 'content' => '<h2>This is a Sub Page Index</h2>' . PHP_EOL
// . '<p>This is index.md in the "sub" folder.</p>' . PHP_EOL
// . '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>' . PHP_EOL
// . '<p>Donec ultricies tristique nulla et mattis.</p>' . PHP_EOL
// . '<p>Phasellus id massa eget nisl congue blandit sit amet id ligula.</p>'
// );

// $files['sub/page.md'] = array(
// 'meta' => array(
// 'title' => 'Sub Page'
// ),
// 'content' => '<h2>This is a Sub Page</h2>' . PHP_EOL
// . '<p>This is page.md in the "sub" folder.</p>' . PHP_EOL
// . '<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>' . PHP_EOL
// . '<p>Donec ultricies tristique nulla et mattis.</p>' . PHP_EOL
// . '<p>Phasellus id massa eget nisl congue blandit sit amet id ligula.</p>'
// );

// $result = $this->object->load(ROOT_DIR . 'content/sub', array('md'));
// $this->assertEquals($files, $result);
$this->markTestIncomplete('Not yet implemented');
$path = ROOT_DIR . 'plugins';

$actual = $this->object->load($path, array('php'));

$expected = array(
'ExamplePlugin' => $this->plugins['ExamplePlugin'],
'OtherExamplePlugin' => $this->plugins['OtherExamplePlugin']
);

$this->assertEquals($expected, $actual);
}

}

0 comments on commit b8c247a

Please sign in to comment.