Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- require __DIR__ . '/LoggerException.php';
- require __DIR__ . '/helpers/LoggerOptionConverter.php';
- /** A base class from which all classes which have configurable properties are extended. */
- abstract class LoggerBaseClass {
- // Various property types
- const PROP_TYPE_BOOLEAN = 'boolean';
- const PROP_TYPE_INTEGER = 'integer';
- const PROP_TYPE_FLOAT = 'float';
- const PROP_TYPE_STRING = 'string';
- const PROP_TYPE_FILESIZE = 'filesize';
- const PROP_TYPE_LEVEL = 'level';
- /**
- * The main setter function, used for setting all properties.
- *
- * @return TRUE if the property was successfully set, FALSE if an error occured.
- */
- protected function set($property, $value, $type, $check = null)
- {
- if (!property_exists($this, $property)) {
- $this->warn("Property '$property' does not exist.");
- return;
- }
- switch($type) {
- case self::PROP_TYPE_BOOLEAN:
- return $this->setBoolean($property, $value);
- case self::PROP_TYPE_STRING:
- return $this->setString($property, $value);
- default:
- $this->warn("No setter defined for property type [$type].");
- return false;
- }
- }
- /** Setter function for boolean type. */
- private function setBoolean($property, $value)
- {
- try {
- $this->$property = LoggerOptionConverter::toBooleanEx($value);
- } catch (Exception $ex) {
- $value = var_export($value, true);
- $propValue = var_export($this->$property, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected a boolean value. Property not changed, current value: [$propValue].");
- }
- }
- /** Setter function for string type. */
- private function setString($property, $value)
- {
- if (is_string($value)) {
- $this->$property = $value;
- } else {
- $value = var_export($value, true);
- $propValue = var_export($this->$property, true);
- $this->warn("Invalid value given for '$property' property: [$value]. Expected a string. Property not changed, current value: [$propValue].");
- }
- }
- /*
- * TODO: implement other setters here
- */
- /** Triggers a warning. */
- protected function warn($message)
- {
- $class = get_class($this);
- trigger_error("log4php: $class: $message", E_USER_WARNING);
- }
- }
- abstract class LoggerAppender extends LoggerBaseClass { }
- /** Sample appender class with two properties. */
- class LoggerAppenderFile extends LoggerAppender {
- const DEFAULT_APPEND = true;
- /** A string property. */
- protected $file;
- /** A boolean property. */
- protected $append = self::DEFAULT_APPEND;
- public function setAppend($value) {
- $this->set('append', $value, self::PROP_TYPE_BOOLEAN);
- }
- public function getAppend() {
- return $this->append;
- }
- public function setFile($value) {
- $this->set('file', $value, self::PROP_TYPE_STRING);
- }
- public function getFile() {
- return $this->file;
- }
- }
- // Testing
- $c = new LoggerAppenderFile();
- // These work ok.
- $c->setAppend('false');
- $c->setFile('/var/log/log.txt');
- var_dump($c->getAppend());
- var_dump($c->getFile());
- // These create a warning
- $c->setAppend('bla'); // not convertable to boolean
- $c->setFile(13); // not a string
- ?>
Advertisement
Add Comment
Please, Sign In to add comment