Skip to content
This repository was archived by the owner on Jul 10, 2023. It is now read-only.

Use controller namespace to determine which class is should be used #142

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion src/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
class Loader
{
protected $path;
protected $namespace;
protected $files;
protected $instance;
protected $instances = [];
Expand All @@ -17,6 +18,7 @@ public function __construct()
return;
}

$this->setNamespace();
$this->setDocumentClasses();
$this->setFileList();
$this->includeTraits();
Expand All @@ -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
*
Expand Down Expand Up @@ -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('/(?<!^)[A-Z]/', '-$0', $template));
Expand Down