From 75d2409a7ccd394fbe9e726abd1bafacc22c668d Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Tue, 1 Jul 2014 20:50:51 +0200 Subject: [PATCH 1/3] Added ThreadedLoggerAttachments --- src/pocketmine/PocketMine.php | 1 + src/pocketmine/utils/MainLogger.php | 5 ++ src/spl/ThreadedLogger.php | 46 +++++++++++++++ src/spl/ThreadedLoggerAttachment.php | 85 ++++++++++++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 src/spl/ThreadedLoggerAttachment.php diff --git a/src/pocketmine/PocketMine.php b/src/pocketmine/PocketMine.php index 888d55374..212f4e72d 100644 --- a/src/pocketmine/PocketMine.php +++ b/src/pocketmine/PocketMine.php @@ -95,6 +95,7 @@ namespace pocketmine { } if(!class_exists("ThreadedLogger", false)){ + require_once(\pocketmine\PATH . "src/spl/ThreadedLoggerAttachment.php"); require_once(\pocketmine\PATH . "src/spl/ThreadedLogger.php"); } diff --git a/src/pocketmine/utils/MainLogger.php b/src/pocketmine/utils/MainLogger.php index dfa4fb087..cd6bb2eed 100644 --- a/src/pocketmine/utils/MainLogger.php +++ b/src/pocketmine/utils/MainLogger.php @@ -145,6 +145,11 @@ class MainLogger extends \ThreadedLogger{ }else{ echo $message; } + + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->call($message); + } + $this->logStream .= date("Y-m-d", $now) . " " . $cleanMessage; } diff --git a/src/spl/ThreadedLogger.php b/src/spl/ThreadedLogger.php index 62c3b6cb0..8b345980e 100644 --- a/src/spl/ThreadedLogger.php +++ b/src/spl/ThreadedLogger.php @@ -21,4 +21,50 @@ abstract class ThreadedLogger extends \Thread implements Logger{ + /** @var \ThreadedLoggerAttachment */ + protected $attachment = null; + + /** + * @param ThreadedLoggerAttachment $attachment + */ + public function addAttachment(\ThreadedLoggerAttachment $attachment){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->addAttachment($attachment); + }else{ + $this->attachment = $attachment; + } + } + + /** + * @param ThreadedLoggerAttachment $attachment + */ + public function removeAttachment(\ThreadedLoggerAttachment $attachment){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + if($this->attachment === $attachment){ + $this->attachment = null; + foreach($attachment->getAttachments() as $attachment){ + $this->addAttachment($attachment); + } + } + } + } + + public function removeAttachments(){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->removeAttachments(); + $this->attachment = null; + } + } + + /** + * @return \ThreadedLoggerAttachment[] + */ + public function getAttachments(){ + $attachments = []; + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $attachments[] = $this->attachment; + $attachments += $this->attachment->getAttachments(); + } + return $attachments; + } } \ No newline at end of file diff --git a/src/spl/ThreadedLoggerAttachment.php b/src/spl/ThreadedLoggerAttachment.php new file mode 100644 index 000000000..a5f21f713 --- /dev/null +++ b/src/spl/ThreadedLoggerAttachment.php @@ -0,0 +1,85 @@ +log($message); + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->call($message); + } + } + + /** + * @param ThreadedLoggerAttachment $attachment + */ + public function addAttachment(\ThreadedLoggerAttachment $attachment){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->addAttachment($attachment); + }else{ + $this->attachment = $attachment; + } + } + + /** + * @param ThreadedLoggerAttachment $attachment + */ + public function removeAttachment(\ThreadedLoggerAttachment $attachment){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + if($this->attachment === $attachment){ + $this->attachment = null; + foreach($attachment->getAttachments() as $attachment){ + $this->addAttachment($attachment); + } + } + } + } + + public function removeAttachments(){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->removeAttachments(); + $this->attachment = null; + } + } + + /** + * @return \ThreadedLoggerAttachment[] + */ + public function getAttachments(){ + $attachments = []; + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $attachments[] = $this->attachment; + $attachments += $this->attachment->getAttachments(); + } + return $attachments; + } +} \ No newline at end of file From 6c116678d70651ea05d83023a483440f71fa7875 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Tue, 1 Jul 2014 23:34:24 +0200 Subject: [PATCH 2/3] Added autoloading of base namespace classes --- src/pocketmine/PocketMine.php | 16 +++------------- .../level/generator/GenerationThread.php | 1 - src/spl/SplClassLoader.php | 8 ++++++++ 3 files changed, 11 insertions(+), 14 deletions(-) diff --git a/src/pocketmine/PocketMine.php b/src/pocketmine/PocketMine.php index 212f4e72d..b860b519e 100644 --- a/src/pocketmine/PocketMine.php +++ b/src/pocketmine/PocketMine.php @@ -89,21 +89,11 @@ namespace pocketmine { require_once(\pocketmine\PATH . "src/spl/SplClassLoader.php"); } - if(!class_exists("Logger", false)){ - require_once(\pocketmine\PATH . "src/spl/Logger.php"); - require_once(\pocketmine\PATH . "src/spl/LogLevel.php"); - } - - if(!class_exists("ThreadedLogger", false)){ - require_once(\pocketmine\PATH . "src/spl/ThreadedLoggerAttachment.php"); - require_once(\pocketmine\PATH . "src/spl/ThreadedLogger.php"); - } - - $autoloader = new \SplClassLoader(); - $autoloader->add("pocketmine", array( + $autoloader->setMode(\SplAutoloader::MODE_DEBUG); + $autoloader->add("pocketmine", [ \pocketmine\PATH . "src" - )); + ]); $autoloader->register(true); if(!class_exists("raklib\\RakLib", false)){ diff --git a/src/pocketmine/level/generator/GenerationThread.php b/src/pocketmine/level/generator/GenerationThread.php index 84aa9f12c..d9ae29816 100644 --- a/src/pocketmine/level/generator/GenerationThread.php +++ b/src/pocketmine/level/generator/GenerationThread.php @@ -52,7 +52,6 @@ class GenerationThread extends \Thread{ $this->loader = $loader; $this->logger = $logger; $loadPaths = []; - $this->addDependency($loadPaths, new \ReflectionClass($this->logger)); $this->addDependency($loadPaths, new \ReflectionClass($this->loader)); $this->loadPaths = array_reverse($loadPaths); diff --git a/src/spl/SplClassLoader.php b/src/spl/SplClassLoader.php index 5bd8b7f28..698d45287 100644 --- a/src/spl/SplClassLoader.php +++ b/src/spl/SplClassLoader.php @@ -141,6 +141,14 @@ class SplClassLoader implements SplAutoloader{ public function load($resourceName){ $resourceAbsolutePath = $this->getResourceAbsolutePath($resourceName); if($resourceAbsolutePath == ""){ + if($resourceName{0} !== "\\"){ + if(file_exists(dirname(__FILE__) ."/" . $resourceName . $this->fileExtension)){ + require dirname(__FILE__) ."/" . $resourceName . $this->fileExtension; + if($this->isResourceDeclared($resourceName)){ + return; + } + } + } throw new \RuntimeException( sprintf('Autoloader couldn\'t find a file to include for %s', $resourceName) ); From c8dd8f02eb618d6c0ca4a22316ca3bbd830e15a1 Mon Sep 17 00:00:00 2001 From: Shoghi Cervantes Date: Wed, 2 Jul 2014 23:11:39 +0200 Subject: [PATCH 3/3] Finished #1555 todo list --- src/spl/AttachableLogger.php | 40 ++++++++++++++++ src/spl/AttachableThreadedLogger.php | 70 ++++++++++++++++++++++++++++ src/spl/LoggerAttachment.php | 34 ++++++++++++++ src/spl/ThreadedLogger.php | 46 ------------------ src/spl/ThreadedLoggerAttachment.php | 7 +-- 5 files changed, 145 insertions(+), 52 deletions(-) create mode 100644 src/spl/AttachableLogger.php create mode 100644 src/spl/AttachableThreadedLogger.php create mode 100644 src/spl/LoggerAttachment.php diff --git a/src/spl/AttachableLogger.php b/src/spl/AttachableLogger.php new file mode 100644 index 000000000..dfe76830f --- /dev/null +++ b/src/spl/AttachableLogger.php @@ -0,0 +1,40 @@ +attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->addAttachment($attachment); + }else{ + $this->attachment = $attachment; + } + } + + /** + * @param ThreadedLoggerAttachment $attachment + */ + public function removeAttachment(\ThreadedLoggerAttachment $attachment){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + if($this->attachment === $attachment){ + $this->attachment = null; + foreach($attachment->getAttachments() as $attachment){ + $this->addAttachment($attachment); + } + } + } + } + + public function removeAttachments(){ + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $this->attachment->removeAttachments(); + $this->attachment = null; + } + } + + /** + * @return \ThreadedLoggerAttachment[] + */ + public function getAttachments(){ + $attachments = []; + if($this->attachment instanceof \ThreadedLoggerAttachment){ + $attachments[] = $this->attachment; + $attachments += $this->attachment->getAttachments(); + } + return $attachments; + } +} \ No newline at end of file diff --git a/src/spl/LoggerAttachment.php b/src/spl/LoggerAttachment.php new file mode 100644 index 000000000..912808754 --- /dev/null +++ b/src/spl/LoggerAttachment.php @@ -0,0 +1,34 @@ +attachment instanceof \ThreadedLoggerAttachment){ - $this->attachment->addAttachment($attachment); - }else{ - $this->attachment = $attachment; - } - } - - /** - * @param ThreadedLoggerAttachment $attachment - */ - public function removeAttachment(\ThreadedLoggerAttachment $attachment){ - if($this->attachment instanceof \ThreadedLoggerAttachment){ - if($this->attachment === $attachment){ - $this->attachment = null; - foreach($attachment->getAttachments() as $attachment){ - $this->addAttachment($attachment); - } - } - } - } - - public function removeAttachments(){ - if($this->attachment instanceof \ThreadedLoggerAttachment){ - $this->attachment->removeAttachments(); - $this->attachment = null; - } - } - - /** - * @return \ThreadedLoggerAttachment[] - */ - public function getAttachments(){ - $attachments = []; - if($this->attachment instanceof \ThreadedLoggerAttachment){ - $attachments[] = $this->attachment; - $attachments += $this->attachment->getAttachments(); - } - return $attachments; - } } \ No newline at end of file diff --git a/src/spl/ThreadedLoggerAttachment.php b/src/spl/ThreadedLoggerAttachment.php index a5f21f713..f66df2b7f 100644 --- a/src/spl/ThreadedLoggerAttachment.php +++ b/src/spl/ThreadedLoggerAttachment.php @@ -19,16 +19,11 @@ * */ -abstract class ThreadedLoggerAttachment extends \Threaded{ +abstract class ThreadedLoggerAttachment extends \Threaded implements \LoggerAttachment{ /** @var \ThreadedLoggerAttachment */ protected $attachment = null; - /** - * @param string $message - */ - protected abstract function log($message); - /** * @param string $message */