Klasse für lokalisierte Exceptions
Localised Exception class
The library encompasses an class for automatically translate exceptions, which
is used in all library classes. You can throw a LException
yourself as shown
in the example code:
Die Bibliothek beinhaltet eine Klasse, die automatisch Exceptions übersetzt, und die in allen Klassen der Bibliothek verwendet wird. Es ist auch möglich selbst lokalisierte Exceptions zu werfen, wie im Beispiel angegeben:
Sample source code
Anwendungsbeispiel
<?php
include_once('swlib/swlib.class.php');
use sw\LException;
/**
* All exceptions of the library are localisable, and the lib catches uncaught
* exceptions or triggered errors with, and keeps the localiser in the loop.
*
* Example specific localiser class for this example. We need it to show that
* the "File not found" exception will be translated.
*/
class MyLocalizer extends sw\Localizer {
protected function localize($text, $const = array()) {
$lt = array( // Dummy translation "database"
'Password for ":user" is incorrect' => array( // well normally we wouldn't say that ...
'de' => 'Passwort für ":user" ist falsch'
),
'Could not change directory to :dir: Does not exist.' => array(
'de' => 'Konnte nicht ins Verzeichnis ":dir" wechseln weil es nicht existiert.'
)
);
if(isset($lt[$text]) && isset($lt[$text][$this->language()])) {
$text = $lt[$text][$this->language()];
}
return parent::localize($text, $const);
}
}
// Initialise the localiser
sw\Localizer::start('MyLocalizer', 'de');
//
// This is how we throw a localised exception:
//
try {
throw new LException('Password for ":user" is incorrect', array(':user' => 'rincewind'));
} catch(\Exception $e) {
print "Exception: " . $e->getMessage() . "\n";
}
//
// That will fail. In CLI mode the exception will shown and backtraced with
// colors.
//
\sw\FileSystem::chdir('/tmp/not-existing');
Output
Ausgabe
Exception: Passwort für "rincewind" ist falsch
Uncaught Exception: 'Konnte nicht ins Verzeichnis "/tmp/not-existing" wechseln
weil es nicht existiert.' in .../sys/FileSystem.class.php[255]
/.../lexception.test.php@48 sw\FileSystem::chdir("/tmp/not-existing")
Class source code
Klassen-Quelltext
<?php
/**
* Localizable exception class.
*
* @gpackage de.atwillys.sw.php.swLib
* @author Stefan Wilhelm
* @copyright Stefan Wilhelm, 2005-2010
* @license GPL
* @version 1.0
*/
namespace sw;
class LException extends \Exception {
/**
* Localized exception constructor
* @param string $message
* @param array $locale_const
* @param int $code
* @param Exception $previous
*/
public function __construct($message='', $locale_const=array(), $code=null, $previous=null) {
try {
$message = Localizer::translate($message, $locale_const);
} catch (\Exception $e) {
if (is_array($locale_const)) {
try {
$message = str_ireplace(array_keys($locale_const), array_values($locale_const), $message);
} catch (\Exception $e) {
if (class_exists('sw\Tracer', false)) {
Tracer::trace('!!!! Failed to replace constants in exception ' . $message);
}
}
} else if (is_numeric($locale_const)) {
$previous = $code;
$code = $locale_const;
}
}
parent::__construct($message, $code, $previous);
}
}