Skip to content

Commit 9b719ee

Browse files
laoneowilsonge
authored andcommitted
Config service provider (#19658)
1 parent e7d3fb2 commit 9b719ee

File tree

9 files changed

+78
-117
lines changed

9 files changed

+78
-117
lines changed

libraries/src/Application/Autoconfigurable.php

Lines changed: 0 additions & 100 deletions
This file was deleted.

libraries/src/Application/CliApplication.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
*/
3333
abstract class CliApplication extends AbstractApplication implements DispatcherAwareInterface, CMSApplicationInterface
3434
{
35-
use Autoconfigurable, DispatcherAwareTrait, EventAware, IdentityAware, ContainerAwareTrait;
35+
use DispatcherAwareTrait, EventAware, IdentityAware, ContainerAwareTrait;
3636

3737
/**
3838
* Output object
@@ -102,9 +102,6 @@ public function __construct(Input $input = null, Registry $config = null, CliOut
102102
// Set the current directory.
103103
$this->set('cwd', getcwd());
104104

105-
// Load the configuration object.
106-
$this->loadConfiguration($this->fetchConfigurationData());
107-
108105
// Set up the environment
109106
$this->input->set('format', 'cli');
110107
}

libraries/src/Application/ConsoleApplication.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*/
3131
class ConsoleApplication extends Application implements DispatcherAwareInterface, CMSApplicationInterface
3232
{
33-
use Autoconfigurable, DispatcherAwareTrait, EventAware, IdentityAware, ContainerAwareTrait;
33+
use DispatcherAwareTrait, EventAware, IdentityAware, ContainerAwareTrait;
3434

3535
/**
3636
* The application message queue.
@@ -80,9 +80,6 @@ public function __construct(Cli $input = null, Registry $config = null, Dispatch
8080
$this->setDispatcher($dispatcher);
8181
}
8282

83-
// Load the configuration object.
84-
$this->loadConfiguration($this->fetchConfigurationData());
85-
8683
// Set the execution datetime and timestamp;
8784
$this->set('execution.datetime', gmdate('Y-m-d H:i:s'));
8885
$this->set('execution.timestamp', time());

libraries/src/Application/WebApplication.php

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
*/
3030
abstract class WebApplication extends AbstractWebApplication implements DispatcherAwareInterface
3131
{
32-
use Autoconfigurable, DispatcherAwareTrait, EventAware, IdentityAware;
32+
use DispatcherAwareTrait, EventAware, IdentityAware;
3333

3434
/**
3535
* The application document object.
@@ -81,9 +81,6 @@ public function __construct(Input $input = null, Registry $config = null, WebCli
8181

8282
parent::__construct($input, $config, $client, $response);
8383

84-
// Load the configuration object.
85-
$this->loadConfiguration($this->fetchConfigurationData());
86-
8784
// Set the execution datetime and timestamp;
8885
$this->set('execution.datetime', gmdate('Y-m-d H:i:s'));
8986
$this->set('execution.timestamp', time());
@@ -422,4 +419,16 @@ protected function loadSystemUris($requestUri = null)
422419
$this->set('uri.media.path', $this->get('uri.base.path') . 'media/');
423420
}
424421
}
422+
423+
/**
424+
* Retrieve the application configuration object.
425+
*
426+
* @return Registry
427+
*
428+
* @since __DEPLOY_VERSION__
429+
*/
430+
public function getConfig()
431+
{
432+
return $this->config;
433+
}
425434
}

libraries/src/Factory.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ protected static function createContainer(): Container
500500
$container = (new Container)
501501
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Application)
502502
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Authentication)
503+
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Config)
503504
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Console)
504505
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Database)
505506
->registerServiceProvider(new \Joomla\CMS\Service\Provider\Dispatcher)

libraries/src/Service/Provider/Application.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function register(Container $container)
4949
'JApplicationAdministrator',
5050
function (Container $container)
5151
{
52-
$app = new AdministratorApplication(null, null, null, $container);
52+
$app = new AdministratorApplication(null, $container->get('config'), null, $container);
5353

5454
// The session service provider needs Factory::$application, set it if still null
5555
if (Factory::$application === null)
@@ -71,7 +71,7 @@ function (Container $container)
7171
'JApplicationSite',
7272
function (Container $container)
7373
{
74-
$app = new SiteApplication(null, null, null, $container);
74+
$app = new SiteApplication(null, $container->get('config'), null, $container);
7575

7676
// The session service provider needs Factory::$application, set it if still null
7777
if (Factory::$application === null)
@@ -93,7 +93,7 @@ function (Container $container)
9393
BaseConsoleApplication::class,
9494
function (Container $container)
9595
{
96-
$app = new ConsoleApplication;
96+
$app = new ConsoleApplication(null, $container->get('config'));
9797

9898
$dispatcher = $container->get('Joomla\Event\DispatcherInterface');
9999

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* Joomla! Content Management System
4+
*
5+
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
6+
* @license GNU General Public License version 2 or later; see LICENSE.txt
7+
*/
8+
9+
namespace Joomla\CMS\Service\Provider;
10+
11+
defined('JPATH_PLATFORM') or die;
12+
13+
use Joomla\DI\Container;
14+
use Joomla\DI\ServiceProviderInterface;
15+
use Joomla\Registry\Registry;
16+
17+
/**
18+
* Service provider for the application's config dependency
19+
*
20+
* @since 4.0
21+
*/
22+
class Config implements ServiceProviderInterface
23+
{
24+
/**
25+
* Registers the service provider with a DI container.
26+
*
27+
* @param Container $container The DI container.
28+
*
29+
* @return void
30+
*
31+
* @since 4.0
32+
*/
33+
public function register(Container $container)
34+
{
35+
$container->alias('config', 'JConfig')
36+
->share(
37+
'JConfig',
38+
function (Container $container)
39+
{
40+
if (!file_exists(JPATH_CONFIGURATION . '/configuration.php'))
41+
{
42+
return [];
43+
}
44+
45+
\JLoader::register('JConfig', JPATH_CONFIGURATION . '/configuration.php');
46+
47+
if (!class_exists('JConfig'))
48+
{
49+
throw new \RuntimeException('Configuration class does not exist.');
50+
}
51+
52+
return new Registry(new \JConfig);
53+
},
54+
true
55+
);
56+
}
57+
}

libraries/src/Service/Provider/Database.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function register(Container $container)
4343
DatabaseInterface::class,
4444
function (Container $container)
4545
{
46-
$conf = \JFactory::getConfig();
46+
$conf = $container->get('config');
4747

4848
$dbtype = $conf->get('dbtype');
4949

libraries/src/Service/Provider/Session.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function register(Container $container)
5151
'Joomla\Session\SessionInterface',
5252
function (Container $container)
5353
{
54-
$config = Factory::getConfig();
54+
$config = $container->get('config');
5555
$app = Factory::getApplication();
5656

5757
// Generate a session name.

0 commit comments

Comments
 (0)