Skip to content
Snippets Groups Projects
Commit 2bf90ac5 authored by Tim van Dijen's avatar Tim van Dijen
Browse files

Rewrite Module using symfony/filesystem

parent 261ebc96
No related branches found
No related tags found
No related merge requests found
...@@ -9,6 +9,9 @@ use SimpleSAML\Assert\Assert; ...@@ -9,6 +9,9 @@ use SimpleSAML\Assert\Assert;
use SimpleSAML\Kernel; use SimpleSAML\Kernel;
use SimpleSAML\Utils; use SimpleSAML\Utils;
use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException; use Symfony\Component\Config\Exception\FileLocatorFileNotFoundException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpFoundation\BinaryFileResponse; use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
...@@ -16,6 +19,31 @@ use Symfony\Component\HttpFoundation\Response; ...@@ -16,6 +19,31 @@ use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\ResponseHeaderBag; use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use function array_filter;
use function array_key_exists;
use function class_exists;
use function count;
use function dirname;
use function explode;
use function function_exists;
use function in_array;
use function is_bool;
use function is_callable;
use function is_dir;
use function is_file;
use function is_null;
use function is_subclass_of;
use function mb_strtolower;
use function mime_content_type;
use function preg_match;
use function rtrim;
use function str_replace;
use function strlen;
use function strpos;
use function strtolower;
use function strval;
use function substr;
/** /**
* Helper class for accessing information about modules. * Helper class for accessing information about modules.
* *
...@@ -233,11 +261,12 @@ class Module ...@@ -233,11 +261,12 @@ class Module
} }
$path = $moduleDir . $url; $path = $moduleDir . $url;
$fileSystem = new Filesystem();
if ($path[strlen($path) - 1] === '/') { if ($path[strlen($path) - 1] === '/') {
// path ends with a slash - directory reference. Attempt to find index file in directory // path ends with a slash - directory reference. Attempt to find index file in directory
foreach (self::$indexFiles as $if) { foreach (self::$indexFiles as $if) {
if (file_exists($path . $if)) { if ($fileSystem->exists($path . $if)) {
$path .= $if; $path .= $if;
break; break;
} }
...@@ -251,7 +280,7 @@ class Module ...@@ -251,7 +280,7 @@ class Module
throw new Error\NotFound('Directory listing not available.'); throw new Error\NotFound('Directory listing not available.');
} }
if (!file_exists($path)) { if (!$fileSystem->exists($path)) {
// file not found // file not found
Logger::info('Could not find file \'' . $path . '\'.'); Logger::info('Could not find file \'' . $path . '\'.');
throw new Error\NotFound('The URL wasn\'t found in the module.'); throw new Error\NotFound('The URL wasn\'t found in the module.');
...@@ -373,21 +402,11 @@ class Module ...@@ -373,21 +402,11 @@ class Module
$path = self::getModuleDir('.'); $path = self::getModuleDir('.');
$dh = scandir($path); $finder = new Finder();
if ($dh === false) { $finder->directories()->in($path)->depth(0);
throw new Exception('Unable to open module directory "' . $path . '".');
}
foreach ($dh as $f) {
if ($f[0] === '.') {
continue;
}
if (!is_dir($path . '/' . $f)) {
continue;
}
self::$modules[] = $f; foreach ($finder as $module) {
self::$modules[] = $module->getFileName();
} }
return self::$modules; return self::$modules;
...@@ -485,25 +504,21 @@ class Module ...@@ -485,25 +504,21 @@ class Module
return self::$modules[$module]['hooks']; return self::$modules[$module]['hooks'];
} }
$hook_dir = self::getModuleDir($module) . '/hooks';
if (!is_dir($hook_dir)) {
return [];
}
$hooks = []; $hooks = [];
$files = scandir($hook_dir); $hook_dir = dirname(dirname(dirname(__FILE__))) . '/modules/' . $module . '/hooks';
foreach ($files as $file) { if ((new Filesystem())->exists($hook_dir)) {
if ($file[0] === '.') { $finder = new Finder();
continue; $finder->files()->in($hook_dir)->depth(0);
}
foreach ($finder as $file) {
if (!preg_match('/^hook_(\w+)\.php$/', $file, $matches)) { if (preg_match('/^hook_(\w+)\.php$/', $file->getFileName(), $matches)) {
continue; $hook_name = $matches[1];
$hook_func = $module . '_hook_' . $hook_name;
$hooks[$hook_name] = ['file' => Path::canonicalize(strval($file)), 'func' => $hook_func];
}
} }
$hook_name = $matches[1];
$hook_func = $module . '_hook_' . $hook_name;
$hooks[$hook_name] = ['file' => $hook_dir . '/' . $file, 'func' => $hook_func];
} }
return $hooks; return $hooks;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment