mirror of
https://github.com/phpbb/phpbb.git
synced 2025-08-16 05:34:01 +02:00
Merge pull request #4698 from javiexin/ticket/15087
[ticket/15087] Optimize creation of metadata objects by caching
This commit is contained in:
@@ -18,24 +18,6 @@ namespace phpbb\extension;
|
||||
*/
|
||||
class metadata_manager
|
||||
{
|
||||
/**
|
||||
* phpBB Config instance
|
||||
* @var \phpbb\config\config
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
/**
|
||||
* phpBB Extension Manager
|
||||
* @var \phpbb\extension\manager
|
||||
*/
|
||||
protected $extension_manager;
|
||||
|
||||
/**
|
||||
* phpBB root path
|
||||
* @var string
|
||||
*/
|
||||
protected $phpbb_root_path;
|
||||
|
||||
/**
|
||||
* Name (including vendor) of the extension
|
||||
* @var string
|
||||
@@ -58,19 +40,13 @@ class metadata_manager
|
||||
* Creates the metadata manager
|
||||
*
|
||||
* @param string $ext_name Name (including vendor) of the extension
|
||||
* @param \phpbb\config\config $config phpBB Config instance
|
||||
* @param \phpbb\extension\manager $extension_manager An instance of the phpBB extension manager
|
||||
* @param string $phpbb_root_path Path to the phpbb includes directory.
|
||||
* @param string $ext_path Path to the extension directory including root path
|
||||
*/
|
||||
public function __construct($ext_name, \phpbb\config\config $config, \phpbb\extension\manager $extension_manager, $phpbb_root_path)
|
||||
public function __construct($ext_name, $ext_path)
|
||||
{
|
||||
$this->config = $config;
|
||||
$this->extension_manager = $extension_manager;
|
||||
$this->phpbb_root_path = $phpbb_root_path;
|
||||
|
||||
$this->ext_name = $ext_name;
|
||||
$this->metadata = array();
|
||||
$this->metadata_file = '';
|
||||
$this->metadata_file = $ext_path . 'composer.json';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -81,13 +57,11 @@ class metadata_manager
|
||||
*/
|
||||
public function get_metadata($element = 'all')
|
||||
{
|
||||
$this->set_metadata_file();
|
||||
|
||||
// Fetch the metadata
|
||||
$this->fetch_metadata();
|
||||
|
||||
// Clean the metadata
|
||||
$this->clean_metadata_array();
|
||||
// Fetch and clean the metadata if not done yet
|
||||
if ($this->metadata === array())
|
||||
{
|
||||
$this->fetch_metadata_from_file();
|
||||
}
|
||||
|
||||
switch ($element)
|
||||
{
|
||||
@@ -110,52 +84,29 @@ class metadata_manager
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the filepath of the metadata file
|
||||
* Gets the metadata file contents and cleans loaded file
|
||||
*
|
||||
* @throws \phpbb\extension\exception
|
||||
*/
|
||||
private function set_metadata_file()
|
||||
{
|
||||
$ext_filepath = $this->extension_manager->get_extension_path($this->ext_name);
|
||||
$metadata_filepath = $this->phpbb_root_path . $ext_filepath . 'composer.json';
|
||||
|
||||
$this->metadata_file = $metadata_filepath;
|
||||
|
||||
if (!file_exists($this->metadata_file))
|
||||
{
|
||||
throw new \phpbb\extension\exception('FILE_NOT_FOUND', array($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()
|
||||
private function fetch_metadata_from_file()
|
||||
{
|
||||
if (!file_exists($this->metadata_file))
|
||||
{
|
||||
throw new \phpbb\extension\exception('FILE_NOT_FOUND', array($this->metadata_file));
|
||||
}
|
||||
else
|
||||
|
||||
if (!($file_contents = file_get_contents($this->metadata_file)))
|
||||
{
|
||||
if (!($file_contents = file_get_contents($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;
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -169,16 +120,6 @@ class metadata_manager
|
||||
$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
|
||||
*
|
||||
@@ -201,8 +142,23 @@ class metadata_manager
|
||||
switch ($name)
|
||||
{
|
||||
case 'all':
|
||||
$this->validate_enable();
|
||||
// no break
|
||||
$this->validate('display');
|
||||
|
||||
if (!$this->validate_dir())
|
||||
{
|
||||
throw new \phpbb\extension\exception('EXTENSION_DIR_INVALID');
|
||||
}
|
||||
|
||||
if (!$this->validate_require_phpbb())
|
||||
{
|
||||
throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array('soft-require'));
|
||||
}
|
||||
|
||||
if (!$this->validate_require_php())
|
||||
{
|
||||
throw new \phpbb\extension\exception('META_FIELD_NOT_SET', array('require php'));
|
||||
}
|
||||
break;
|
||||
|
||||
case 'display':
|
||||
foreach ($fields as $field => $data)
|
||||
@@ -316,40 +272,4 @@ class metadata_manager
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Outputs the metadata into the template
|
||||
*
|
||||
* @param \phpbb\template\template $template phpBB Template instance
|
||||
*/
|
||||
public function output_template_data(\phpbb\template\template $template)
|
||||
{
|
||||
$template->assign_vars(array(
|
||||
'META_NAME' => $this->metadata['name'],
|
||||
'META_TYPE' => $this->metadata['type'],
|
||||
'META_DESCRIPTION' => (isset($this->metadata['description'])) ? $this->metadata['description'] : '',
|
||||
'META_HOMEPAGE' => (isset($this->metadata['homepage'])) ? $this->metadata['homepage'] : '',
|
||||
'META_VERSION' => (isset($this->metadata['version'])) ? $this->metadata['version'] : '',
|
||||
'META_TIME' => (isset($this->metadata['time'])) ? $this->metadata['time'] : '',
|
||||
'META_LICENSE' => $this->metadata['license'],
|
||||
|
||||
'META_REQUIRE_PHP' => (isset($this->metadata['require']['php'])) ? $this->metadata['require']['php'] : '',
|
||||
'META_REQUIRE_PHP_FAIL' => (isset($this->metadata['require']['php'])) ? false : true,
|
||||
|
||||
'META_REQUIRE_PHPBB' => (isset($this->metadata['extra']['soft-require']['phpbb/phpbb'])) ? $this->metadata['extra']['soft-require']['phpbb/phpbb'] : '',
|
||||
'META_REQUIRE_PHPBB_FAIL' => (isset($this->metadata['extra']['soft-require']['phpbb/phpbb'])) ? false : true,
|
||||
|
||||
'META_DISPLAY_NAME' => (isset($this->metadata['extra']['display-name'])) ? $this->metadata['extra']['display-name'] : '',
|
||||
));
|
||||
|
||||
foreach ($this->metadata['authors'] as $author)
|
||||
{
|
||||
$template->assign_block_vars('meta_authors', array(
|
||||
'AUTHOR_NAME' => $author['name'],
|
||||
'AUTHOR_EMAIL' => (isset($author['email'])) ? $author['email'] : '',
|
||||
'AUTHOR_HOMEPAGE' => (isset($author['homepage'])) ? $author['homepage'] : '',
|
||||
'AUTHOR_ROLE' => (isset($author['role'])) ? $author['role'] : '',
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user