mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-29 14:48:53 +00:00
[ticket/10631] Simplify exceptions
PHPBB-10631
This commit is contained in:
parent
89f4cf6a8c
commit
2a7e129291
4 changed files with 55 additions and 85 deletions
|
@ -49,9 +49,12 @@ class acp_extensions
|
||||||
{
|
{
|
||||||
$md_manager = new phpbb_extension_metadata_manager($ext_name, $db, $phpbb_extension_manager, $phpbb_root_path, ".$phpEx", $template, $config);
|
$md_manager = new phpbb_extension_metadata_manager($ext_name, $db, $phpbb_extension_manager, $phpbb_root_path, ".$phpEx", $template, $config);
|
||||||
|
|
||||||
try{
|
try
|
||||||
|
{
|
||||||
$md_manager->get_metadata('all');
|
$md_manager->get_metadata('all');
|
||||||
} catch( Exception $e ) {
|
}
|
||||||
|
catch(phpbb_extension_exception $e)
|
||||||
|
{
|
||||||
trigger_error($e);
|
trigger_error($e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -172,7 +175,8 @@ class acp_extensions
|
||||||
{
|
{
|
||||||
$md_manager = $phpbb_extension_manager->get_extension_metadata_manager($name, $this->template);
|
$md_manager = $phpbb_extension_manager->get_extension_metadata_manager($name, $this->template);
|
||||||
|
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
$this->template->assign_block_vars('enabled', array(
|
$this->template->assign_block_vars('enabled', array(
|
||||||
'EXT_NAME' => $md_manager->get_metadata('display-name'),
|
'EXT_NAME' => $md_manager->get_metadata('display-name'),
|
||||||
|
|
||||||
|
@ -183,7 +187,9 @@ class acp_extensions
|
||||||
'DISABLE' => $this->u_action . '&action=disable_pre&ext_name=' . $name,
|
'DISABLE' => $this->u_action . '&action=disable_pre&ext_name=' . $name,
|
||||||
'PURGE' => $this->u_action . '&action=purge_pre&ext_name=' . $name,
|
'PURGE' => $this->u_action . '&action=purge_pre&ext_name=' . $name,
|
||||||
));
|
));
|
||||||
} catch( Exception $e ) {
|
}
|
||||||
|
catch(phpbb_extension_exception $e)
|
||||||
|
{
|
||||||
$this->template->assign_block_vars('disabled', array(
|
$this->template->assign_block_vars('disabled', array(
|
||||||
'EXT_NAME' => $this->user->lang('EXTENSION_INVALID_LIST', $name, $e),
|
'EXT_NAME' => $this->user->lang('EXTENSION_INVALID_LIST', $name, $e),
|
||||||
));
|
));
|
||||||
|
@ -204,7 +210,8 @@ class acp_extensions
|
||||||
{
|
{
|
||||||
$md_manager = $phpbb_extension_manager->get_extension_metadata_manager($name, $this->template);
|
$md_manager = $phpbb_extension_manager->get_extension_metadata_manager($name, $this->template);
|
||||||
|
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
$this->template->assign_block_vars('disabled', array(
|
$this->template->assign_block_vars('disabled', array(
|
||||||
'EXT_NAME' => $md_manager->get_metadata('display-name'),
|
'EXT_NAME' => $md_manager->get_metadata('display-name'),
|
||||||
|
|
||||||
|
@ -215,7 +222,9 @@ class acp_extensions
|
||||||
'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . $name,
|
'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . $name,
|
||||||
'PURGE' => $this->u_action . '&action=purge_pre&ext_name=' . $name,
|
'PURGE' => $this->u_action . '&action=purge_pre&ext_name=' . $name,
|
||||||
));
|
));
|
||||||
} catch( Exception $e ) {
|
}
|
||||||
|
catch(phpbb_extension_exception $e)
|
||||||
|
{
|
||||||
$this->template->assign_block_vars('disabled', array(
|
$this->template->assign_block_vars('disabled', array(
|
||||||
'EXT_NAME' => $this->user->lang('EXTENSION_INVALID_LIST', $name, $e),
|
'EXT_NAME' => $this->user->lang('EXTENSION_INVALID_LIST', $name, $e),
|
||||||
));
|
));
|
||||||
|
@ -238,7 +247,8 @@ class acp_extensions
|
||||||
{
|
{
|
||||||
$md_manager = $phpbb_extension_manager->get_extension_metadata_manager($name, $this->template);
|
$md_manager = $phpbb_extension_manager->get_extension_metadata_manager($name, $this->template);
|
||||||
|
|
||||||
try {
|
try
|
||||||
|
{
|
||||||
$this->template->assign_block_vars('disabled', array(
|
$this->template->assign_block_vars('disabled', array(
|
||||||
'EXT_NAME' => $md_manager->get_metadata('display-name'),
|
'EXT_NAME' => $md_manager->get_metadata('display-name'),
|
||||||
|
|
||||||
|
@ -248,7 +258,9 @@ class acp_extensions
|
||||||
$this->output_actions('disabled', array(
|
$this->output_actions('disabled', array(
|
||||||
'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . $name,
|
'ENABLE' => $this->u_action . '&action=enable_pre&ext_name=' . $name,
|
||||||
));
|
));
|
||||||
} catch( Exception $e ) {
|
}
|
||||||
|
catch(phpbb_extension_exception $e)
|
||||||
|
{
|
||||||
$this->template->assign_block_vars('disabled', array(
|
$this->template->assign_block_vars('disabled', array(
|
||||||
'EXT_NAME' => $this->user->lang('EXTENSION_INVALID_LIST', $name, $e),
|
'EXT_NAME' => $this->user->lang('EXTENSION_INVALID_LIST', $name, $e),
|
||||||
));
|
));
|
||||||
|
|
|
@ -1,69 +0,0 @@
|
||||||
<?php
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* @package extension
|
|
||||||
* @copyright (c) 2012 phpBB Group
|
|
||||||
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @ignore
|
|
||||||
*/
|
|
||||||
if (!defined('IN_PHPBB'))
|
|
||||||
{
|
|
||||||
exit;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Exception class for metadata
|
|
||||||
*/
|
|
||||||
class phpbb_exception_metadata extends LogicException
|
|
||||||
{
|
|
||||||
const NOT_SET = 10001;
|
|
||||||
const INVALID = 10002;
|
|
||||||
const FILE_GET_CONTENTS = 10003;
|
|
||||||
const JSON_DECODE = 10004;
|
|
||||||
const FILE_DOES_NOT_EXIST = 10005;
|
|
||||||
|
|
||||||
public function __construct($code, $field_name)
|
|
||||||
{
|
|
||||||
$this->code = $code;
|
|
||||||
$this->field_name = $field_name;
|
|
||||||
}
|
|
||||||
|
|
||||||
public function __toString()
|
|
||||||
{
|
|
||||||
return sprintf($this->getErrorMessage(), $this->field_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
public function getErrorMessage()
|
|
||||||
{
|
|
||||||
switch ($this->code)
|
|
||||||
{
|
|
||||||
case self::NOT_SET:
|
|
||||||
return 'The "%s" meta field has not been set.';
|
|
||||||
break;
|
|
||||||
|
|
||||||
case self::INVALID:
|
|
||||||
return 'The "%s" meta field is not valid.';
|
|
||||||
break;
|
|
||||||
|
|
||||||
case self::FILE_GET_CONTENTS:
|
|
||||||
return 'file_get_contents failed on %s';
|
|
||||||
break;
|
|
||||||
|
|
||||||
case self::JSON_DECODE:
|
|
||||||
return 'json_decode failed on %s';
|
|
||||||
break;
|
|
||||||
|
|
||||||
case self::FILE_DOES_NOT_EXIST:
|
|
||||||
return 'Required file does not exist at %s';
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
|
||||||
return 'An unexpected error has occurred.';
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
27
phpBB/includes/extension/exception.php
Normal file
27
phpBB/includes/extension/exception.php
Normal file
|
@ -0,0 +1,27 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @package extension
|
||||||
|
* @copyright (c) 2012 phpBB Group
|
||||||
|
* @license http://opensource.org/licenses/gpl-2.0.php GNU General Public License v2
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @ignore
|
||||||
|
*/
|
||||||
|
if (!defined('IN_PHPBB'))
|
||||||
|
{
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Exception class for metadata
|
||||||
|
*/
|
||||||
|
class phpbb_extension_exception extends UnexpectedValueException
|
||||||
|
{
|
||||||
|
public function __toString()
|
||||||
|
{
|
||||||
|
return $this->getMessage();
|
||||||
|
}
|
||||||
|
}
|
|
@ -114,7 +114,7 @@ class phpbb_extension_metadata_manager
|
||||||
|
|
||||||
if (!file_exists($this->metadata_file))
|
if (!file_exists($this->metadata_file))
|
||||||
{
|
{
|
||||||
throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_DOES_NOT_EXIST, $this->metadata_file);
|
throw new phpbb_extension_exception('The required file does not exist: ' . $this->metadata_file);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -127,18 +127,18 @@ class phpbb_extension_metadata_manager
|
||||||
{
|
{
|
||||||
if (!file_exists($this->metadata_file))
|
if (!file_exists($this->metadata_file))
|
||||||
{
|
{
|
||||||
throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_DOES_NOT_EXIST, $this->metadata_file);
|
throw new phpbb_extension_exception('The required file does not exist: ' . $this->metadata_file);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
if (!($file_contents = file_get_contents($this->metadata_file)))
|
if (!($file_contents = file_get_contents($this->metadata_file)))
|
||||||
{
|
{
|
||||||
throw new phpbb_exception_metadata(phpbb_exception_metadata::FILE_GET_CONTENTS, $this->metadata_file);
|
throw new phpbb_extension_exception('file_get_contents failed on ' . $this->metadata_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (($metadata = json_decode($file_contents, true)) === NULL)
|
if (($metadata = json_decode($file_contents, true)) === NULL)
|
||||||
{
|
{
|
||||||
throw new phpbb_exception_metadata(phpbb_exception_metadata::JSON_DECODE, $this->metadata_file);
|
throw new phpbb_extension_exception('json_decode failed on ' . $this->metadata_file);
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->metadata = $metadata;
|
$this->metadata = $metadata;
|
||||||
|
@ -182,12 +182,12 @@ class phpbb_extension_metadata_manager
|
||||||
{
|
{
|
||||||
if (!isset($this->metadata[$name]))
|
if (!isset($this->metadata[$name]))
|
||||||
{
|
{
|
||||||
throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, $name);
|
throw new phpbb_extension_exception("Required meta field '$name' has not been set.");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!preg_match($fields[$name], $this->metadata[$name]))
|
if (!preg_match($fields[$name], $this->metadata[$name]))
|
||||||
{
|
{
|
||||||
throw new phpbb_exception_metadata(phpbb_exception_metadata::INVALID, $name);
|
throw new phpbb_extension_exception("Meta field '$name' is invalid.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -222,14 +222,14 @@ class phpbb_extension_metadata_manager
|
||||||
{
|
{
|
||||||
if (empty($this->metadata['authors']))
|
if (empty($this->metadata['authors']))
|
||||||
{
|
{
|
||||||
throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, 'authors');
|
throw new phpbb_extension_exception("Required meta field 'authors' has not been set.");
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($this->metadata['authors'] as $author)
|
foreach ($this->metadata['authors'] as $author)
|
||||||
{
|
{
|
||||||
if (!isset($author['name']))
|
if (!isset($author['name']))
|
||||||
{
|
{
|
||||||
throw new phpbb_exception_metadata(phpbb_exception_metadata::NOT_SET, 'author name');
|
throw new phpbb_extension_exception("Required meta field 'author name' has not been set.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue