Skip to content

Commit

Permalink
refactor: use Factories::get()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Mar 2, 2024
1 parent 377bdfb commit 05831b3
Show file tree
Hide file tree
Showing 59 changed files with 134 additions and 105 deletions.
2 changes: 1 addition & 1 deletion system/CLI/BaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ protected function showError(Throwable $e)
{
$exception = $e;
$message = $e->getMessage();
$config = config(Exceptions::class);
$config = \CodeIgniter\Config\Factories::get('config', Exceptions::class);

require $config->errorViewPath . '/cli/error_exception.php';
}
Expand Down
3 changes: 2 additions & 1 deletion system/CLI/Console.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CodeIgniter\CLI;

use CodeIgniter\CodeIgniter;
use CodeIgniter\Config\Factories;
use Config\App;
use Config\Services;
use Exception;
Expand All @@ -35,7 +36,7 @@ class Console
public function run()
{
// Create CLIRequest
$appConfig = config(App::class);
$appConfig = Factories::get('config', App::class);
Services::createRequest($appConfig, true);
// Load Routes
Services::routes()->loadRoutes();
Expand Down
2 changes: 1 addition & 1 deletion system/CLI/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ private function normalizeInputClassName(): string
protected function renderTemplate(array $data = []): string
{
try {
$template = $this->templatePath ?? config(Generators::class)->views[$this->name];
$template = $this->templatePath ?? \CodeIgniter\Config\Factories::get('config', Generators::class)->views[$this->name];

return view($template, $data, ['debug' => false]);
} catch (Throwable $e) {
Expand Down
3 changes: 2 additions & 1 deletion system/Cache/Handlers/BaseHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use Closure;
use CodeIgniter\Cache\CacheInterface;
use CodeIgniter\Config\Factories;
use Config\Cache;
use Exception;
use InvalidArgumentException;
Expand Down Expand Up @@ -66,7 +67,7 @@ public static function validateKey($key, $prefix = ''): string
throw new InvalidArgumentException('Cache key cannot be empty.');
}

$reserved = config(Cache::class)->reservedCharacters ?? self::RESERVED_CHARACTERS;
$reserved = Factories::get('config', Cache::class)->reservedCharacters ?? self::RESERVED_CHARACTERS;
if ($reserved && strpbrk($key, $reserved) !== false) {
throw new InvalidArgumentException('Cache key contains reserved characters ' . $reserved);
}
Expand Down
6 changes: 3 additions & 3 deletions system/CodeIgniter.php
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public function run(?RouteCollectionInterface $routes = null, bool $returnRespon
$this->response = $possibleResponse;
} else {
try {
$this->response = $this->handleRequest($routes, config(Cache::class), $returnResponse);
$this->response = $this->handleRequest($routes, Config\Factories::get('config', Cache::class), $returnResponse);
} catch (ResponsableInterface|DeprecatedRedirectException $e) {
$this->outputBufferingEnd();
if ($e instanceof DeprecatedRedirectException) {
Expand Down Expand Up @@ -469,7 +469,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
if ($routeFilters !== null) {
$filters->enableFilters($routeFilters, 'before');

if (! config(Feature::class)->oldFilterOrder) {
if (! Config\Factories::get('config', Feature::class)->oldFilterOrder) {
$routeFilters = array_reverse($routeFilters);
}

Expand Down Expand Up @@ -965,7 +965,7 @@ protected function display404errors(PageNotFoundException $e)

unset($override);

$cacheConfig = config(Cache::class);
$cacheConfig = Config\Factories::get('config', Cache::class);
$this->gatherOutput($cacheConfig, $returned);

return $this->response;
Expand Down
3 changes: 2 additions & 1 deletion system/Commands/Cache/ClearCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CodeIgniter\Cache\CacheFactory;
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Config\Factories;
use Config\Cache;

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ class ClearCache extends BaseCommand
*/
public function run(array $params)
{
$config = config(Cache::class);
$config = Factories::get('config', Cache::class);
$handler = $params[0] ?? $config->handler;

if (! array_key_exists($handler, $config->validHandlers)) {
Expand Down
2 changes: 1 addition & 1 deletion system/Commands/Cache/InfoCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class InfoCache extends BaseCommand
*/
public function run(array $params)
{
$config = config(Cache::class);
$config = \CodeIgniter\Config\Factories::get('config', Cache::class);
helper('number');

if ($config->handler !== 'file') {
Expand Down
2 changes: 1 addition & 1 deletion system/Commands/Database/CreateDatabase.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public function run(array $params)
}

try {
$config = config(Database::class);
$config = Factories::get('config', Database::class);

// Set to an empty database to prevent connection errors.
$group = ENVIRONMENT === 'testing' ? 'tests' : $config->defaultGroup;
Expand Down
4 changes: 2 additions & 2 deletions system/Commands/Generators/CellGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,13 +81,13 @@ public function run(array $params)

$params = array_merge($params, ['suffix' => null]);

$this->templatePath = config(Generators::class)->views[$this->name]['class'];
$this->templatePath = \CodeIgniter\Config\Factories::get('config', Generators::class)->views[$this->name]['class'];
$this->template = 'cell.tpl.php';
$this->classNameLang = 'CLI.generator.className.cell';

$this->generateClass($params);

$this->templatePath = config(Generators::class)->views[$this->name]['view'];
$this->templatePath = \CodeIgniter\Config\Factories::get('config', Generators::class)->views[$this->name]['view'];
$this->template = 'cell_view.tpl.php';
$this->classNameLang = 'CLI.generator.viewName.cell';

Expand Down
4 changes: 2 additions & 2 deletions system/Commands/Generators/MigrationGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,10 @@ protected function prepare(string $class): string
$data['session'] = true;
$data['table'] = is_string($table) ? $table : 'ci_sessions';
$data['DBGroup'] = is_string($DBGroup) ? $DBGroup : 'default';
$data['DBDriver'] = config(Database::class)->{$data['DBGroup']}['DBDriver'];
$data['DBDriver'] = \CodeIgniter\Config\Factories::get('config', Database::class)->{$data['DBGroup']}['DBDriver'];

/** @var SessionConfig|null $session */
$session = config(SessionConfig::class);
$session = \CodeIgniter\Config\Factories::get('config', SessionConfig::class);

$data['matchIP'] = $session->matchIP;
}
Expand Down
5 changes: 3 additions & 2 deletions system/Commands/Translation/LocalizationFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Config\Factories;
use CodeIgniter\Helpers\Array\ArrayHelper;
use Config\App;
use Locale;
Expand Down Expand Up @@ -67,10 +68,10 @@ public function run(array $params)
}

if (is_string($optionLocale)) {
if (! in_array($optionLocale, config(App::class)->supportedLocales, true)) {
if (! in_array($optionLocale, Factories::get('config', App::class)->supportedLocales, true)) {
CLI::error(
'Error: "' . $optionLocale . '" is not supported. Supported locales: '
. implode(', ', config(App::class)->supportedLocales)
. implode(', ', Factories::get('config', App::class)->supportedLocales)
);

return EXIT_USER_INPUT;
Expand Down
3 changes: 2 additions & 1 deletion system/Commands/Utilities/ConfigCheck.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\Config\BaseConfig;
use CodeIgniter\Config\Factories;
use Kint\Kint;

/**
Expand Down Expand Up @@ -87,7 +88,7 @@ public function run(array $params)
/** @var class-string<BaseConfig> $class */
$class = $params[0];

$config = config($class);
$config = Factories::get('config', $class);

if ($config === null) {
CLI::error('No such Config class: ' . $class);
Expand Down
5 changes: 3 additions & 2 deletions system/Commands/Utilities/Routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\AutoRouteCollector as AutoRouteCollectorImproved;
use CodeIgniter\Commands\Utilities\Routes\FilterCollector;
use CodeIgniter\Commands\Utilities\Routes\SampleURIGenerator;
use CodeIgniter\Config\Factories;
use CodeIgniter\Router\DefinedRouteCollector;
use CodeIgniter\Router\Router;
use Config\Feature;
Expand Down Expand Up @@ -126,7 +127,7 @@ public function run(array $params)
}

if ($collection->shouldAutoRoute()) {
$autoRoutesImproved = config(Feature::class)->autoRoutesImproved ?? false;
$autoRoutesImproved = Factories::get('config', Feature::class)->autoRoutesImproved ?? false;

if ($autoRoutesImproved) {
$autoRouteCollector = new AutoRouteCollectorImproved(
Expand All @@ -140,7 +141,7 @@ public function run(array $params)
$autoRoutes = $autoRouteCollector->get();

// Check for Module Routes.
if ($routingConfig = config(Routing::class)) {
if ($routingConfig = Factories::get('config', Routing::class)) {
foreach ($routingConfig->moduleRoutes as $uri => $namespace) {
$autoRouteCollector = new AutoRouteCollectorImproved(
$namespace,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;

use CodeIgniter\Config\Factories;
use Config\Routing;
use ReflectionClass;
use ReflectionMethod;
Expand Down Expand Up @@ -45,7 +46,7 @@ public function __construct(string $namespace, array $httpMethods)
$this->namespace = $namespace;
$this->httpMethods = $httpMethods;

$config = config(Routing::class);
$config = Factories::get('config', Routing::class);
$this->translateURIDashes = $config->translateURIDashes;
$this->translateUriToCamelCase = $config->translateUriToCamelCase;
}
Expand Down
3 changes: 2 additions & 1 deletion system/Commands/Utilities/Routes/FilterCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Commands\Utilities\Routes;

use CodeIgniter\Config\Factories;
use CodeIgniter\Config\Services;
use CodeIgniter\Filters\Filters;
use CodeIgniter\HTTP\Method;
Expand Down Expand Up @@ -112,7 +113,7 @@ private function createRouter(Request $request): Router

private function createFilters(Request $request): Filters
{
$config = config(FiltersConfig::class);
$config = Factories::get('config', FiltersConfig::class);

return new Filters($config, $request, Services::response());
}
Expand Down
3 changes: 2 additions & 1 deletion system/Commands/Utilities/Routes/FilterFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Commands\Utilities\Routes;

use CodeIgniter\Config\Factories;
use CodeIgniter\Exceptions\PageNotFoundException;
use CodeIgniter\Filters\Filters;
use CodeIgniter\HTTP\Exceptions\RedirectException;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function find(string $uri): array

$this->filters->enableFilters($routeFilters, 'before');

if (! config(Feature::class)->oldFilterOrder) {
if (! Factories::get('config', Feature::class)->oldFilterOrder) {
$routeFilters = array_reverse($routeFilters);
}

Expand Down
3 changes: 2 additions & 1 deletion system/Commands/Utilities/Routes/SampleURIGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace CodeIgniter\Commands\Utilities\Routes;

use CodeIgniter\Config\Factories;
use CodeIgniter\Config\Services;
use CodeIgniter\Router\RouteCollection;
use Config\App;
Expand Down Expand Up @@ -57,7 +58,7 @@ public function get(string $routeKey): string
if (strpos($routeKey, '{locale}') !== false) {
$sampleUri = str_replace(
'{locale}',
config(App::class)->defaultLocale,
Factories::get('config', App::class)->defaultLocale,
$routeKey
);
}
Expand Down
6 changes: 3 additions & 3 deletions system/Common.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
*/
function app_timezone(): string
{
$config = config(App::class);
$config = Factories::get('config', App::class);

return $config->appTimezone;
}
Expand Down Expand Up @@ -1063,7 +1063,7 @@ function single_service(string $name, ...$params): ?object
*/
function slash_item(string $item): ?string
{
$config = config(App::class);
$config = Factories::get('config', App::class);

if (! property_exists($config, $item)) {
return null;
Expand Down Expand Up @@ -1171,7 +1171,7 @@ function view(string $name, array $data = [], array $options = []): string
{
$renderer = Services::renderer();

$config = config(View::class);
$config = Factories::get('config', View::class);
$saveData = $config->saveData;

if (array_key_exists('saveData', $options)) {
Expand Down
4 changes: 2 additions & 2 deletions system/Config/Factories.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* large performance boost and helps keep code clean of lengthy
* instantiation checks.
*
* @method static BaseConfig|null config(...$arguments)
* @method static BaseConfig|null \CodeIgniter\Config\Factories::get('config', ...$arguments)
* @method static Model|null models(string $alias, array $options = [], ?ConnectionInterface &$conn = null)
* @see \CodeIgniter\Config\FactoriesTest
*/
Expand Down Expand Up @@ -395,7 +395,7 @@ public static function getOptions(string $component): array
// Handle Config as a special case to prevent logic loops
? self::$configOptions
// Load values from the best Factory configuration (will include Registrars)
: config('Factory')->{$component} ?? [];
: Factories::get('config', 'Factory')->{$component} ?? [];

// The setOptions() reset the component. So getOptions() may reset
// the component.
Expand Down
Loading

0 comments on commit 05831b3

Please sign in to comment.