1
0
mirror of https://github.com/phpbb/phpbb.git synced 2025-05-06 15:45:34 +02:00

[ticket/10735] Changing locator paths structure

Changing locator paths to 2 dimensional array

PHPBB3-10735
This commit is contained in:
Vjacheslav Trushkin 2012-03-31 21:20:18 +03:00
parent 7e2f16aafa
commit b3f46b9565
6 changed files with 62 additions and 85 deletions

View File

@ -270,11 +270,12 @@ class phpbb_extension_finder
* Finds all directories matching the configured options
*
* @param bool $cache Whether the result should be cached
* @param bool $extension_keys Whether the result should have extension name as array key
* @return array An array of paths to found directories
*/
public function get_directories($cache = true)
public function get_directories($cache = true, $extension_keys = false)
{
return $this->find_with_root_path($cache, true);
return $this->find_with_root_path($cache, true, $extension_keys);
}
/**
@ -294,16 +295,25 @@ class phpbb_extension_finder
* @param bool $cache Whether the result should be cached
* @param bool $is_dir Directories will be returned when true, only files
* otherwise
* @param bool $extension_keys If true, result will be associative array
* with extension name as key
* @return array An array of paths to found items
*/
protected function find_with_root_path($cache = true, $is_dir = false)
protected function find_with_root_path($cache = true, $is_dir = false, $extension_keys = false)
{
$items = $this->find($cache, $is_dir);
$result = array();
foreach ($items as $item => $ext_name)
{
$result[] = $this->phpbb_root_path . $item;
if ($extension_keys)
{
$result[$ext_name] = $this->phpbb_root_path . $item;
}
else
{
$result[] = $this->phpbb_root_path . $item;
}
}
return $result;

View File

@ -82,22 +82,26 @@ class phpbb_style_extension_path_provider extends phpbb_extension_provider imple
$directories = array();
$finder = $this->extension_manager->get_finder();
foreach ($this->base_path_provider as $path)
foreach ($this->base_path_provider as $key => $paths)
{
if ($path && !phpbb_is_absolute($path))
if ($key == 'style')
{
$directories = array_merge($directories, $finder
->directory('/' . $this->ext_dir_prefix . $path)
->get_directories()
);
foreach ($paths as $path)
{
$directories['style'][] = $path;
if ($path && !phpbb_is_absolute($path))
{
$result = $finder->directory('/' . $this->ext_dir_prefix . $path)
->get_directories(true, true);
foreach ($result as $ext => $ext_path)
{
$directories[$ext][] = $ext_path;
}
}
}
}
}
foreach ($this->base_path_provider as $path)
{
$directories[] = $path;
}
return $directories;
}
@ -112,14 +116,4 @@ class phpbb_style_extension_path_provider extends phpbb_extension_provider imple
$this->base_path_provider->set_styles($styles);
$this->items = null;
}
/**
* Retrieves the path to the main style passed into set_styles()
*
* @return string Main style path
*/
public function get_main_style_path()
{
return $this->base_path_provider->get_main_style_path();
}
}

View File

@ -24,7 +24,6 @@ if (!defined('IN_PHPBB'))
*/
class phpbb_style_path_provider implements IteratorAggregate, phpbb_style_path_provider_interface
{
protected $main_style_name = '';
protected $paths = array();
/**
@ -41,25 +40,14 @@ class phpbb_style_path_provider implements IteratorAggregate, phpbb_style_path_p
* Overwrites the current style paths
*
* The first element of the passed styles map, is considered the main
* style and can be retrieved through get_main_style_path().
* style.
*
* @param array $styles An array of style paths. The first element is the main style.
* @return null
*/
public function set_styles(array $styles)
{
$this->paths = $styles;
$this->main_style_path = $this->paths[0];
}
/**
* Retrieves the path to the main style passed into set_styles()
*
* @return string Main style path
*/
public function get_main_style_path()
{
return $this->main_style_path;
$this->paths = array('style' => $styles);
}
/**

View File

@ -39,11 +39,4 @@ interface phpbb_style_path_provider_interface extends Traversable
* @return null
*/
public function set_styles(array $styles);
/**
* Retrieves the path to the main style passed into set_styles()
*
* @return string Main style path
*/
public function get_main_style_path();
}

View File

@ -38,12 +38,6 @@ class phpbb_style_resource_locator
*/
private $roots = array();
/**
* Index of the main style in the roots array.
* @var int
*/
private $main_root_id = 0;
/**
* Location of templates directory within style directories.
* Must have trailing slash. Empty if templates are stored in root
@ -69,17 +63,6 @@ class phpbb_style_resource_locator
*/
private $filenames = array();
/**
* Set main style location (must have been added through set_paths first).
*
* @param string $style_path Path to style directory
* @return null
*/
public function set_main_style($style_path)
{
$this->main_root_id = array_search($style_path, $this->roots, true);
}
/**
* Sets the list of style paths
*
@ -95,16 +78,18 @@ class phpbb_style_resource_locator
$this->roots = array();
$this->files = array();
$this->filenames = array();
$this->main_root_id = 0;
foreach ($style_paths as $path)
foreach ($style_paths as $key => $paths)
{
// Make sure $path has no ending slash
if (substr($path, -1) === '/')
foreach ($paths as $path)
{
$path = substr($path, 0, -1);
// Make sure $path has no ending slash
if (substr($path, -1) === '/')
{
$path = substr($path, 0, -1);
}
$this->roots[$key][] = $path;
}
$this->roots[] = $path;
}
}
@ -125,9 +110,12 @@ class phpbb_style_resource_locator
$this->filename[$handle] = $filename;
foreach ($this->roots as $root_index => $root)
foreach ($this->roots as $root_key => $root_paths)
{
$this->files[$root_index][$handle] = $root . '/' . $this->template_path . $filename;
foreach ($root_paths as $root_index => $root)
{
$this->files[$root_key][$root_index][$handle] = $root . '/' . $this->template_path . $filename;
}
}
}
}
@ -174,12 +162,12 @@ class phpbb_style_resource_locator
public function get_virtual_source_file_for_handle($handle)
{
// If we don't have a file assigned to this handle, die.
if (!isset($this->files[$this->main_root_id][$handle]))
if (!isset($this->files['style'][0][$handle]))
{
trigger_error("style resource locator: No file specified for handle $handle", E_USER_ERROR);
}
$source_file = $this->files[$this->main_root_id][$handle];
$source_file = $this->files['style'][0][$handle];
return $source_file;
}
@ -202,26 +190,28 @@ class phpbb_style_resource_locator
public function get_source_file_for_handle($handle)
{
// If we don't have a file assigned to this handle, die.
if (!isset($this->files[$this->main_root_id][$handle]))
if (!isset($this->files['style'][0][$handle]))
{
trigger_error("style resource locator: No file specified for handle $handle", E_USER_ERROR);
}
// locate a source file that exists
$source_file = $this->files[0][$handle];
$source_file = $this->files['style'][0][$handle];
$tried = $source_file;
for ($i = 1, $n = count($this->roots); $i < $n && !file_exists($source_file); $i++)
foreach ($this->roots as $root_key => $root_paths)
{
$source_file = $this->files[$i][$handle];
$tried .= ', ' . $source_file;
foreach ($root_paths as $root_index => $root)
{
$source_file = $this->files[$root_key][$root_index][$handle];
if (file_exists($source_file))
{
return $source_file;
}
$tried .= ', ' . $source_file;
}
}
// search failed
if (!file_exists($source_file))
{
trigger_error("style resource locator: File for handle $handle does not exist. Could not find: $tried", E_USER_ERROR);
}
return $source_file;
trigger_error("style resource locator: File for handle $handle does not exist. Could not find: $tried", E_USER_ERROR);
}
}

View File

@ -92,6 +92,9 @@ class phpbb_style
$paths[] = $this->get_style_path($dir);
}
// Add 'all' path, used as last fallback path by hooks and extensions
$paths[] = $this->get_style_path('all');
return $this->set_custom_style($style_name, $paths);
}
@ -113,7 +116,6 @@ class phpbb_style
$this->provider->set_styles($paths);
$this->locator->set_paths($this->provider);
$this->locator->set_main_style($this->provider->get_main_style_path());
$this->template->cachepath = $this->phpbb_root_path . 'cache/tpl_' . str_replace('_', '-', $name) . '_';