mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-28 06:08:52 +00:00
[ticket/15087] Optimize creation of metadata objects by caching
Code for [ticket/15080] Save unneeded file loads for extension metadata for phpbb 3.2. PHPBB3-15087
This commit is contained in:
parent
5266821e1b
commit
d3a687df27
1 changed files with 16 additions and 48 deletions
|
@ -81,13 +81,11 @@ class metadata_manager
|
||||||
*/
|
*/
|
||||||
public function get_metadata($element = 'all')
|
public function get_metadata($element = 'all')
|
||||||
{
|
{
|
||||||
$this->set_metadata_file();
|
// Fetch and clean the metadata if not done yet
|
||||||
|
if ($this->metadata === array())
|
||||||
// Fetch the metadata
|
{
|
||||||
$this->fetch_metadata();
|
$this->fetch_metadata_from_file();
|
||||||
|
}
|
||||||
// Clean the metadata
|
|
||||||
$this->clean_metadata_array();
|
|
||||||
|
|
||||||
switch ($element)
|
switch ($element)
|
||||||
{
|
{
|
||||||
|
@ -121,52 +119,32 @@ class metadata_manager
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets the filepath of the metadata file
|
* Sets the path of the metadata file, gets its contents and cleans loaded file
|
||||||
*
|
*
|
||||||
* @throws \phpbb\extension\exception
|
* @throws \phpbb\extension\exception
|
||||||
*/
|
*/
|
||||||
private function set_metadata_file()
|
private function fetch_metadata_from_file()
|
||||||
{
|
{
|
||||||
$ext_filepath = $this->extension_manager->get_extension_path($this->ext_name);
|
$ext_filepath = $this->extension_manager->get_extension_path($this->ext_name);
|
||||||
$metadata_filepath = $this->phpbb_root_path . $ext_filepath . 'composer.json';
|
$this->metadata_file = $this->phpbb_root_path . $ext_filepath . 'composer.json';
|
||||||
|
|
||||||
$this->metadata_file = $metadata_filepath;
|
|
||||||
|
|
||||||
if (!file_exists($this->metadata_file))
|
if (!file_exists($this->metadata_file))
|
||||||
{
|
{
|
||||||
throw new \phpbb\extension\exception('FILE_NOT_FOUND', array($this->metadata_file));
|
throw new \phpbb\extension\exception('FILE_NOT_FOUND', array($this->metadata_file));
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
if (!($file_contents = file_get_contents($this->metadata_file)))
|
||||||
* Gets the contents of the composer.json file
|
|
||||||
*
|
|
||||||
* @return bool True if success, throws an exception on failure
|
|
||||||
* @throws \phpbb\extension\exception
|
|
||||||
*/
|
|
||||||
private function fetch_metadata()
|
|
||||||
{
|
|
||||||
if (!file_exists($this->metadata_file))
|
|
||||||
{
|
{
|
||||||
throw new \phpbb\extension\exception('FILE_NOT_FOUND', array($this->metadata_file));
|
throw new \phpbb\extension\exception('FILE_CONTENT_ERR', array($this->metadata_file));
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
if (($metadata = json_decode($file_contents, true)) === null)
|
||||||
{
|
{
|
||||||
if (!($file_contents = file_get_contents($this->metadata_file)))
|
throw new \phpbb\extension\exception('FILE_JSON_DECODE_ERR', array($this->metadata_file));
|
||||||
{
|
|
||||||
throw new \phpbb\extension\exception('FILE_CONTENT_ERR', array($this->metadata_file));
|
|
||||||
}
|
|
||||||
|
|
||||||
if (($metadata = json_decode($file_contents, true)) === null)
|
|
||||||
{
|
|
||||||
throw new \phpbb\extension\exception('FILE_JSON_DECODE_ERR', array($this->metadata_file));
|
|
||||||
}
|
|
||||||
|
|
||||||
array_walk_recursive($metadata, array($this, 'sanitize_json'));
|
|
||||||
$this->metadata = $metadata;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
array_walk_recursive($metadata, array($this, 'sanitize_json'));
|
||||||
|
$this->metadata = $metadata;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -180,16 +158,6 @@ class metadata_manager
|
||||||
$value = htmlspecialchars($value);
|
$value = htmlspecialchars($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* This array handles the cleaning of the array
|
|
||||||
*
|
|
||||||
* @return array Contains the cleaned metadata array
|
|
||||||
*/
|
|
||||||
private function clean_metadata_array()
|
|
||||||
{
|
|
||||||
return $this->metadata;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Validate fields
|
* Validate fields
|
||||||
*
|
*
|
||||||
|
|
Loading…
Add table
Reference in a new issue