mirror of
https://github.com/codeigniter4/CodeIgniter4.git
synced 2025-02-20 11:44:28 +08:00
Adding registrar classes to config files for easier registration of properties from modular code
This commit is contained in:
parent
8b566453f7
commit
65178d1d19
@ -5,7 +5,7 @@ class View extends \CodeIgniter\Config\View
|
||||
/**
|
||||
* When false, the view method will clear the data between each
|
||||
* call. This keeps your data safe and ensures there is no accidental
|
||||
* leaking between calls, so you would need to explicity pass the data
|
||||
* leaking between calls, so you would need to explicitly pass the data
|
||||
* to each view. You might prefer to have the data stick around between
|
||||
* calls so that it is available to all views. If that is the case,
|
||||
* set $saveData to true.
|
||||
|
@ -47,6 +47,14 @@
|
||||
*/
|
||||
class BaseConfig
|
||||
{
|
||||
/**
|
||||
* An optional array of classes that will act as Registrars
|
||||
* for rapidly setting config class properties.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $registrars;
|
||||
|
||||
/**
|
||||
* Will attempt to get environment variables with names
|
||||
* that match the properties of the child class.
|
||||
@ -87,6 +95,8 @@ class BaseConfig
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$this->registerProperties();
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
@ -118,4 +128,43 @@ class BaseConfig
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
/**
|
||||
* Provides external libraries a simple way to register one or more
|
||||
* options into a config file.
|
||||
*/
|
||||
protected function registerProperties()
|
||||
{
|
||||
if (empty($this->registrars)) return;
|
||||
|
||||
$shortName = (new \ReflectionClass($this))->getShortName();
|
||||
|
||||
// Check the registrar class for a method named after this class' shortName
|
||||
foreach ($this->registrars as $callable)
|
||||
{
|
||||
if (! method_exists($callable, $shortName)) continue;
|
||||
|
||||
$properties = $callable::$shortName();
|
||||
|
||||
if (! is_array($properties))
|
||||
{
|
||||
throw new \RuntimeException('Registrars must return an array of properties and their values.');
|
||||
}
|
||||
|
||||
foreach ($properties as $property => $value)
|
||||
{
|
||||
if (! property_exists($this, $property)) continue;
|
||||
|
||||
if (is_array($this->$property) && is_array($value))
|
||||
{
|
||||
$this->$property = array_merge($this->$property, $value);
|
||||
}
|
||||
else {
|
||||
$this->$property = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
}
|
||||
|
@ -1,6 +1,6 @@
|
||||
<?php namespace CodeIgniter\Config;
|
||||
|
||||
class View {
|
||||
class View extends BaseConfig {
|
||||
|
||||
protected $coreFilters = [
|
||||
'abs' => '\CodeIgniter\View\Filters::abs',
|
||||
@ -35,6 +35,8 @@ class View {
|
||||
{
|
||||
$this->filters = array_merge($this->filters, $this->coreFilters);
|
||||
$this->plugins = array_merge($this->plugins, $this->corePlugins);
|
||||
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
}
|
||||
|
17
tests/_support/Config/Registrar.php
Normal file
17
tests/_support/Config/Registrar.php
Normal file
@ -0,0 +1,17 @@
|
||||
<?php namespace Tests\Support\Config;
|
||||
|
||||
/**
|
||||
* Class Registrar
|
||||
*
|
||||
* Provides a basic registrar class for testing BaseConfig registration functions.
|
||||
*/
|
||||
class Registrar
|
||||
{
|
||||
public static function RegistrarConfig()
|
||||
{
|
||||
return [
|
||||
'bar' => ['first', 'second']
|
||||
];
|
||||
}
|
||||
|
||||
}
|
@ -16,6 +16,10 @@ class BaseConfigTest extends CIUnitTestCase
|
||||
{
|
||||
require $this->fixturesFolder.'/SimpleConfig.php';
|
||||
}
|
||||
if (! class_exists('RegistrarConfig', false))
|
||||
{
|
||||
require $this->fixturesFolder.'/RegistrarConfig.php';
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
@ -96,4 +100,11 @@ class BaseConfigTest extends CIUnitTestCase
|
||||
|
||||
//--------------------------------------------------------------------
|
||||
|
||||
public function testRegistrars()
|
||||
{
|
||||
$config = new \RegistrarConfig();
|
||||
|
||||
$this->assertEquals(['baz', 'first', 'second'], $config->bar);
|
||||
}
|
||||
|
||||
}
|
||||
|
13
tests/system/Config/fixtures/RegistrarConfig.php
Normal file
13
tests/system/Config/fixtures/RegistrarConfig.php
Normal file
@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
class RegistrarConfig extends \CodeIgniter\Config\BaseConfig
|
||||
{
|
||||
public $foo = 'bar';
|
||||
public $bar = [
|
||||
'baz'
|
||||
];
|
||||
|
||||
protected $registrars = [
|
||||
\Tests\Support\Config\Registrar::class
|
||||
];
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user