Skip to content

Commit

Permalink
// Use normalizeDirectory function in PrestaShopAutoLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
jnadaud committed Aug 11, 2014
1 parent 62c530f commit 8288a6f
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions classes/PrestaShopAutoload.php
Expand Up @@ -61,7 +61,7 @@ class PrestaShopAutoload
protected function __construct()
{
$this->root_dir = _PS_CORE_DIR_.'/';
$file = _PS_ROOT_DIR_.'/'.PrestaShopAutoload::INDEX_FILE;
$file = $this->normalizeDirectory(_PS_ROOT_DIR_).PrestaShopAutoload::INDEX_FILE;
if (@filemtime($file) && is_readable($file))
$this->index = include($file);
else
Expand Down Expand Up @@ -115,7 +115,7 @@ public function load($classname)
require_once($this->root_dir.$this->index[$classname.'Core']['path']);

$class_dir = (isset($this->index[$classname]['override'])
&& $this->index[$classname]['override'] === true) ? _PS_ROOT_DIR_.'/' : $this->root_dir;
&& $this->index[$classname]['override'] === true) ? $this->normalizeDirectory(_PS_ROOT_DIR_) : $this->root_dir;

if (isset($this->index[$classname]))
require_once($class_dir.$this->index[$classname]['path']);
Expand Down Expand Up @@ -147,7 +147,7 @@ public function generateIndex()
$content = '<?php return '.var_export($classes, true).'; ?>';

// Write classes index on disc to cache it
$filename = _PS_ROOT_DIR_.'/'.PrestaShopAutoload::INDEX_FILE;
$filename = $this->normalizeDirectory(_PS_ROOT_DIR_).PrestaShopAutoload::INDEX_FILE;
$filename_tmp = tempnam(dirname($filename), basename($filename.'.'));
if ($filename_tmp !== false && file_put_contents($filename_tmp, $content) !== false)
{
Expand All @@ -171,7 +171,7 @@ public function generateIndex()
protected function getClassesFromDir($path, $host_mode = false)
{
$classes = array();
$root_dir = $host_mode ? _PS_ROOT_DIR_.'/' : $this->root_dir;
$root_dir = $host_mode ? $this->normalizeDirectory(_PS_ROOT_DIR_) : $this->root_dir;

foreach (scandir($root_dir.$path) as $file)
{
Expand Down Expand Up @@ -210,4 +210,18 @@ public function getClassPath($classname)
{
return (isset($this->index[$classname]) && isset($this->index[$classname]['path'])) ? $this->index[$classname]['path'] : null;
}

private function normalizeDirectory($directory)
{
$last = $directory[strlen($directory) - 1];

if (in_array($last, array('/', '\\')))
{
$directory[strlen($directory) - 1] = DIRECTORY_SEPARATOR;
return $directory;
}

$directory .= DIRECTORY_SEPARATOR;
return $directory;
}
}

0 comments on commit 8288a6f

Please sign in to comment.