From 18f5018270834ef825ace69dbd2d8416b087444b Mon Sep 17 00:00:00 2001 From: Liam Martens Date: Thu, 15 Apr 2021 17:58:49 +0200 Subject: [PATCH] Use controller namespace to determine which class is should be used --- src/Loader.php | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/Loader.php b/src/Loader.php index 11bc8c4..e623897 100644 --- a/src/Loader.php +++ b/src/Loader.php @@ -5,6 +5,7 @@ class Loader { protected $path; + protected $namespace; protected $files; protected $instance; protected $instances = []; @@ -17,6 +18,7 @@ public function __construct() return; } + $this->setNamespace(); $this->setDocumentClasses(); $this->setFileList(); $this->includeTraits(); @@ -33,6 +35,16 @@ protected function setPath() $this->path = (has_filter('sober/controller/path') ? apply_filters('sober/controller/path', rtrim($this->path)) : dirname(get_template_directory()) . '/app/controllers'); } + /** + * Set Namespace + * + * Set the default namespace or get custom namespace + */ + protected function setNamespace() + { + $this->namespace = (has_filter('sober/controller/namespace') ? apply_filters('sober/controller/namespace', rtrim($this->namespace)) : 'App\\Controllers'); + } + /** * Set File List * @@ -77,8 +89,14 @@ protected function setDocumentClasses() */ protected function setInstance() { + $namespace_length = strlen($this->namespace); $class = get_declared_classes(); - $class = '\\' . end($class); + $classCount = count($class); + $classIndex = $classCount - 1; + while(substr($class[$classIndex], 0, $namespace_length) != $this->namespace) { + $classIndex--; + } + $class = '\\' . $class[$classIndex]; $template = pathinfo($this->instance, PATHINFO_FILENAME); // Convert camel case to match template $template = strtolower(preg_replace('/(?