Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
project
drupal
Commits
6c70c013
Commit
6c70c013
authored
Feb 11, 2014
by
catch
Browse files
Issue
#2175917
by Gábor Hojtsy, alexpott, piyuesh23, jibran: Clean up configuration system events.
parent
435de27c
Changes
17
Hide whitespace changes
Inline
Side-by-side
core/lib/Drupal/Core/Config/Config.php
View file @
6c70c013
...
...
@@ -75,7 +75,7 @@ class Config extends DependencySerialization {
*
* @var array
*/
protected
$originalData
;
protected
$originalData
=
array
()
;
/**
* The current runtime data.
...
...
@@ -458,7 +458,7 @@ public function save() {
$this
->
storage
->
write
(
$this
->
name
,
$this
->
data
);
$this
->
isNew
=
FALSE
;
$this
->
notify
(
'save'
);
$this
->
eventDispatcher
->
dispatch
(
ConfigEvents
::
SAVE
,
new
ConfigCrudEvent
(
$this
)
);
$this
->
originalData
=
$this
->
data
;
return
$this
;
}
...
...
@@ -475,7 +475,7 @@ public function delete() {
$this
->
storage
->
delete
(
$this
->
name
);
$this
->
isNew
=
TRUE
;
$this
->
resetOverriddenData
();
$this
->
notify
(
'delete'
);
$this
->
eventDispatcher
->
dispatch
(
ConfigEvents
::
DELETE
,
new
ConfigCrudEvent
(
$this
)
);
$this
->
originalData
=
$this
->
data
;
return
$this
;
}
...
...
@@ -490,16 +490,6 @@ public function getStorage() {
return
$this
->
storage
;
}
/**
* Dispatches a configuration event.
*
* @param string $config_event_name
* The configuration event name.
*/
protected
function
notify
(
$config_event_name
)
{
$this
->
eventDispatcher
->
dispatch
(
'config.'
.
$config_event_name
,
new
ConfigEvent
(
$this
));
}
/**
* Merges data into a configuration object.
*
...
...
core/lib/Drupal/Core/Config/ConfigEvent.php
→
core/lib/Drupal/Core/Config/Config
Crud
Event.php
View file @
6c70c013
<?php
/**
* @file
* Contains \Drupal\Core\Config\ConfigCrudEvent.
*/
namespace
Drupal\Core\Config
;
use
Symfony\Component\EventDispatcher\Event
;
class
ConfigEvent
extends
Event
{
/**
* Wraps a configuration event for event listeners.
*/
class
ConfigCrudEvent
extends
Event
{
/**
* Configuration object.
...
...
@@ -24,10 +32,26 @@ public function __construct(Config $config) {
}
/**
* Get configuration object.
* Gets configuration object.
*
* @return \Drupal\Core\Config\Config
* The configuration object that caused the event to fire.
*/
public
function
getConfig
()
{
return
$this
->
config
;
}
/**
* Checks to see if the provided configuration key's value has changed.
*
* @param string $key
* The configuration key to check if it has changed.
*
* @return bool
*/
public
function
isChanged
(
$key
)
{
return
$this
->
config
->
get
(
$key
)
!==
$this
->
config
->
getOriginal
(
$key
);
}
}
core/lib/Drupal/Core/Config/ConfigEvents.php
0 → 100644
View file @
6c70c013
<?php
/**
* @file
* Contains Drupal\Core\Config\Config\ConfigEvents.
*/
namespace
Drupal\Core\Config
;
/**
* Defines events for the configuration system.
*/
class
ConfigEvents
{
/**
* Name of event fired when saving the configuration object.
*
* @see \Drupal\Core\Config\Config::save()
* @see \Drupal\Core\Config\ConfigFactory::onConfigSave()
*/
const
SAVE
=
'config.save'
;
/**
* Name of event fired when deleting the configuration object.
*
* @see \Drupal\Core\Config\Config::delete()
*/
const
DELETE
=
'config.delete'
;
/**
* Name of event fired when renaming a configuration object.
*
* @see \Drupal\Core\Config\ConfigFactory::rename().
*/
const
RENAME
=
'config.rename'
;
/**
* Name of event fired when collecting overrides for configuration objects.
*
* @see \Drupal\Core\Config\ConfigFactory::loadModuleOverrides().
*/
const
MODULE_OVERRIDES
=
'config.module.overrides'
;
/**
* Name of event fired when validating in the configuration import process.
*
* @see \Drupal\Core\Config\ConfigImporter::validate().
* @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber::onConfigImporterValidate().
*/
const
VALIDATE
=
'config.importer.validate'
;
/**
* Name of event fired when when importing configuration to target storage.
*
* @see \Drupal\Core\Config\ConfigImporter::import().
* @see \Drupal\Core\EventSubscriber\ConfigSnapshotSubscriber::onConfigImporterImport().
*/
const
IMPORT
=
'config.importer.import'
;
}
core/lib/Drupal/Core/Config/ConfigFactory.php
View file @
6c70c013
...
...
@@ -223,7 +223,7 @@ public function loadMultiple(array $names) {
*/
protected
function
loadModuleOverrides
(
array
$names
)
{
$configOverridesEvent
=
new
ConfigModuleOverridesEvent
(
$names
,
$this
->
language
);
$this
->
eventDispatcher
->
dispatch
(
'c
onfig
.module.overrides'
,
$configOverridesEvent
);
$this
->
eventDispatcher
->
dispatch
(
C
onfig
Events
::
MODULE_OVERRIDES
,
$configOverridesEvent
);
return
$configOverridesEvent
->
getOverrides
();
}
...
...
@@ -258,12 +258,10 @@ public function rename($old_name, $new_name) {
unset
(
$this
->
cache
[
$old_cache_key
]);
}
$new_cache_key
=
$this
->
getCacheKey
(
$new_name
);
$this
->
cache
[
$new_cache_key
]
=
new
Config
(
$new_name
,
$this
->
storage
,
$this
->
eventDispatcher
,
$this
->
typedConfigManager
,
$this
->
language
);
if
(
$data
=
$this
->
storage
->
read
(
$new_name
))
{
$this
->
cache
[
$new_cache_key
]
->
initWithData
(
$data
);
}
return
$this
->
cache
[
$new_cache_key
];
// Prime the cache and load the configuration with the correct overrides.
$config
=
$this
->
get
(
$new_name
);
$this
->
eventDispatcher
->
dispatch
(
ConfigEvents
::
RENAME
,
new
ConfigRenameEvent
(
$config
,
$old_name
));
return
$config
;
}
/**
...
...
@@ -370,10 +368,10 @@ protected function canOverride($name) {
/**
* Removes stale static cache entries when configuration is saved.
*
* @param ConfigEvent $event
* @param Config
Crud
Event $event
* The configuration event.
*/
public
function
onConfigSave
(
ConfigEvent
$event
)
{
public
function
onConfigSave
(
Config
Crud
Event
$event
)
{
// Ensure that the static cache contains up to date configuration objects by
// replacing the data on any entries for the configuration object apart
// from the one that references the actual config object being saved.
...
...
@@ -390,7 +388,7 @@ public function onConfigSave(ConfigEvent $event) {
* {@inheritdoc}
*/
static
function
getSubscribedEvents
()
{
$events
[
'c
onfig
.save'
][]
=
array
(
'onConfigSave'
,
255
);
$events
[
C
onfig
Events
::
SAVE
][]
=
array
(
'onConfigSave'
,
255
);
return
$events
;
}
...
...
core/lib/Drupal/Core/Config/ConfigImporter.php
View file @
6c70c013
...
...
@@ -20,11 +20,11 @@
*
* The ConfigImporter has a identifier which is used to construct event names.
* The events fired during an import are:
* -
'c
onfig
.importer.validate'
: Events listening can throw a
* -
C
onfig
Events::VALIDATE
: Events listening can throw a
* \Drupal\Core\Config\ConfigImporterException to prevent an import from
* occurring.
* @see \Drupal\Core\EventSubscriber\ConfigImportSubscriber
* -
'c
onfig
.importer.import'
: Events listening can react to a successful import.
* -
C
onfig
Events::IMPORT
: Events listening can react to a successful import.
* @see \Drupal\Core\EventSubscriber\ConfigSnapshotSubscriber
*
* @see \Drupal\Core\Config\ConfigImporterEvent
...
...
core/lib/Drupal/Core/Config/ConfigRenameEvent.php
0 → 100644
View file @
6c70c013
<?php
/**
* @file
* Contains \Drupal\Core\Config\ConfigRenameEvent.
*/
namespace
Drupal\Core\Config
;
/**
* Configuration event fired when renaming a configuration object.
*/
class
ConfigRenameEvent
extends
ConfigCrudEvent
{
/**
* The old configuration object name.
*
* @var string
*/
protected
$oldName
;
/**
* Constructs the config rename event.
*
* @param \Drupal\Core\Config\Config $config
* The configuration that has been renamed.
* @param string $old_name
* The old configuration object name.
*/
public
function
__construct
(
Config
$config
,
$old_name
)
{
$this
->
config
=
$config
;
$this
->
oldName
=
$old_name
;
}
/**
* Gets the old configuration object name.
*
* @return string
* The old configuration object name.
*/
public
function
getOldName
()
{
return
$this
->
oldName
;
}
}
core/lib/Drupal/Core/EventSubscriber/ConfigImportSubscriber.php
View file @
6c70c013
...
...
@@ -8,6 +8,7 @@
namespace
Drupal\Core\EventSubscriber
;
use
Drupal\Core\Config\Config
;
use
Drupal\Core\Config\ConfigEvents
;
use
Drupal\Core\Config\ConfigImporterEvent
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
...
...
@@ -40,7 +41,7 @@ public function onConfigImporterValidate(ConfigImporterEvent $event) {
* An array of event listener definitions.
*/
static
function
getSubscribedEvents
()
{
$events
[
'c
onfig
.importer.validate'
][]
=
array
(
'onConfigImporterValidate'
,
40
);
$events
[
C
onfig
Events
::
VALIDATE
][]
=
array
(
'onConfigImporterValidate'
,
40
);
return
$events
;
}
...
...
core/lib/Drupal/Core/EventSubscriber/ConfigSnapshotSubscriber.php
View file @
6c70c013
...
...
@@ -7,7 +7,7 @@
namespace
Drupal\Core\EventSubscriber
;
use
Drupal\Core\Config\Config
;
use
Drupal\Core\Config\Config
Events
;
use
Drupal\Core\Config\ConfigManagerInterface
;
use
Drupal\Core\Config\StorageInterface
;
use
Drupal\Core\Config\ConfigImporterEvent
;
...
...
@@ -70,7 +70,7 @@ public function onConfigImporterImport(ConfigImporterEvent $event) {
* An array of event listener definitions.
*/
static
function
getSubscribedEvents
()
{
$events
[
'c
onfig
.importer.import'
][]
=
array
(
'onConfigImporterImport'
,
40
);
$events
[
C
onfig
Events
::
IMPORT
][]
=
array
(
'onConfigImporterImport'
,
40
);
return
$events
;
}
...
...
core/modules/config/lib/Drupal/config/Tests/ConfigEventsTest.php
0 → 100644
View file @
6c70c013
<?php
/**
* @file
* Contains \Drupal\config\Tests\ConfigEventsTest.
*/
namespace
Drupal\config\Tests
;
use
Drupal\Core\Config\Config
;
use
Drupal\Core\Config\ConfigEvents
;
use
Drupal\simpletest\DrupalUnitTestBase
;
/**
* Tests CRUD operations on configuration objects.
*/
class
ConfigEventsTest
extends
DrupalUnitTestBase
{
/**
* Modules to enable.
*
* @var array
*/
public
static
$modules
=
array
(
'config_events_test'
);
public
static
function
getInfo
()
{
return
array
(
'name'
=>
'Config events'
,
'description'
=>
'Tests events fired on configuration objects.'
,
'group'
=>
'Configuration'
,
);
}
/**
* Tests configuration events.
*/
function
testConfigEvents
()
{
$name
=
'config_events_test.test'
;
$config
=
new
Config
(
$name
,
\
Drupal
::
service
(
'config.storage'
),
\
Drupal
::
service
(
'event_dispatcher'
),
\
Drupal
::
service
(
'config.typed'
));
$config
->
set
(
'key'
,
'initial'
);
\
Drupal
::
state
()
->
get
(
'config_events_test.event'
,
FALSE
);
$this
->
assertIdentical
(
\
Drupal
::
state
()
->
get
(
'config_events_test.event'
,
array
()),
array
(),
'No events fired by creating a new configuration object'
);
$config
->
save
();
$event
=
\
Drupal
::
state
()
->
get
(
'config_events_test.event'
,
array
());
$this
->
assertIdentical
(
$event
[
'event_name'
],
ConfigEvents
::
SAVE
);
$this
->
assertIdentical
(
$event
[
'current_config_data'
],
array
(
'key'
=>
'initial'
));
$this
->
assertIdentical
(
$event
[
'raw_config_data'
],
array
(
'key'
=>
'initial'
));
$this
->
assertIdentical
(
$event
[
'original_config_data'
],
array
());
$config
->
set
(
'key'
,
'updated'
)
->
save
();
$event
=
\
Drupal
::
state
()
->
get
(
'config_events_test.event'
,
array
());
$this
->
assertIdentical
(
$event
[
'event_name'
],
ConfigEvents
::
SAVE
);
$this
->
assertIdentical
(
$event
[
'current_config_data'
],
array
(
'key'
=>
'updated'
));
$this
->
assertIdentical
(
$event
[
'raw_config_data'
],
array
(
'key'
=>
'updated'
));
$this
->
assertIdentical
(
$event
[
'original_config_data'
],
array
(
'key'
=>
'initial'
));
$config
->
delete
();
$event
=
\
Drupal
::
state
()
->
get
(
'config_events_test.event'
,
array
());
$this
->
assertIdentical
(
$event
[
'event_name'
],
ConfigEvents
::
DELETE
);
$this
->
assertIdentical
(
$event
[
'current_config_data'
],
array
());
$this
->
assertIdentical
(
$event
[
'raw_config_data'
],
array
());
$this
->
assertIdentical
(
$event
[
'original_config_data'
],
array
(
'key'
=>
'updated'
));
}
/**
* Tests configuration rename event that is fired from the ConfigFactory.
*/
function
testConfigRenameEvent
()
{
$name
=
'config_events_test.test'
;
$new_name
=
'config_events_test.test_rename'
;
$GLOBALS
[
'config'
][
$name
]
=
array
(
'key'
=>
'overridden'
);
$GLOBALS
[
'config'
][
$new_name
]
=
array
(
'key'
=>
'new overridden'
);
$config
=
\
Drupal
::
config
(
$name
);
$config
->
set
(
'key'
,
'initial'
)
->
save
();
$event
=
\
Drupal
::
state
()
->
get
(
'config_events_test.event'
,
array
());
$this
->
assertIdentical
(
$event
[
'event_name'
],
ConfigEvents
::
SAVE
);
$this
->
assertIdentical
(
$event
[
'current_config_data'
],
array
(
'key'
=>
'overridden'
));
\
Drupal
::
configFactory
()
->
rename
(
$name
,
$new_name
);
$event
=
\
Drupal
::
state
()
->
get
(
'config_events_test.event'
,
array
());
$this
->
assertIdentical
(
$event
[
'event_name'
],
ConfigEvents
::
RENAME
);
$this
->
assertIdentical
(
$event
[
'current_config_data'
],
array
(
'key'
=>
'new overridden'
));
$this
->
assertIdentical
(
$event
[
'raw_config_data'
],
array
(
'key'
=>
'initial'
));
$this
->
assertIdentical
(
$event
[
'original_config_data'
],
array
(
'key'
=>
'new overridden'
));
}
}
core/modules/config/tests/config_events_test/config_events_test.info.yml
0 → 100644
View file @
6c70c013
name
:
'
Configuration
events
test'
type
:
module
package
:
Testing
version
:
VERSION
core
:
8.x
hidden
:
true
core/modules/config/tests/config_events_test/config_events_test.module
0 → 100644
View file @
6c70c013
<?php
/**
* @file
* Provides Config event listeners for testing purposes.
*/
core/modules/config/tests/config_events_test/config_events_test.services.yml
0 → 100644
View file @
6c70c013
services
:
config_events_test.event_subscriber
:
class
:
Drupal\config_events_test\EventSubscriber
arguments
:
[
'
@state'
]
tags
:
-
{
name
:
event_subscriber
}
core/modules/config/tests/config_events_test/lib/Drupal/config_events_test/EventSubscriber.php
0 → 100644
View file @
6c70c013
<?php
/**
* @file
* Contains \Drupal\config_events_test\EventSubscriber.
*/
namespace
Drupal\config_events_test
;
use
Drupal\Core\Config\ConfigCrudEvent
;
use
Drupal\Core\Config\ConfigEvents
;
use
Drupal\Core\KeyValueStore\StateInterface
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
class
EventSubscriber
implements
EventSubscriberInterface
{
/**
* The state key value store.
*
* @var \Drupal\Core\KeyValueStore\StateInterface
*/
protected
$state
;
/**
* Constructs the Event Subscriber object.
*
* @param \Drupal\Core\KeyValueStore\StateInterface $state
* The state key value store.
*/
public
function
__construct
(
StateInterface
$state
)
{
$this
->
state
=
$state
;
}
/**
* Reacts to config event.
*
* @param \Drupal\Core\Config\ConfigCrudEvent $event
* The configuration event.
*/
public
function
configEventRecorder
(
ConfigCrudEvent
$event
)
{
$config
=
$event
->
getConfig
();
$this
->
state
->
set
(
'config_events_test.event'
,
array
(
'event_name'
=>
$event
->
getName
(),
'current_config_data'
=>
$config
->
get
(),
'original_config_data'
=>
$config
->
getOriginal
(),
'raw_config_data'
=>
$config
->
getRawData
()
));
}
/**
* {@inheritdoc}
*/
static
function
getSubscribedEvents
()
{
$events
[
ConfigEvents
::
SAVE
][]
=
array
(
'configEventRecorder'
);
$events
[
ConfigEvents
::
DELETE
][]
=
array
(
'configEventRecorder'
);
$events
[
ConfigEvents
::
RENAME
][]
=
array
(
'configEventRecorder'
);
return
$events
;
}
}
core/modules/config/tests/config_override/lib/Drupal/config_override/EventSubscriber/ConfigModuleLowPriorityOverrideSubscriber.php
View file @
6c70c013
...
...
@@ -7,6 +7,7 @@
namespace
Drupal\config_override\EventSubscriber
;
use
Drupal\Core\Config\ConfigEvents
;
use
Drupal\Core\Config\ConfigModuleOverridesEvent
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
...
...
@@ -36,7 +37,7 @@ public function onConfigModuleOverride(ConfigModuleOverridesEvent $event) {
* An array of event listener definitions.
*/
static
function
getSubscribedEvents
()
{
$events
[
'c
onfig
.module.overrides'
][]
=
array
(
'onConfigModuleOverride'
,
35
);
$events
[
C
onfig
Events
::
MODULE_OVERRIDES
][]
=
array
(
'onConfigModuleOverride'
,
35
);
return
$events
;
}
}
...
...
core/modules/config/tests/config_override/lib/Drupal/config_override/EventSubscriber/ConfigModuleOverrideSubscriber.php
View file @
6c70c013
...
...
@@ -7,6 +7,7 @@
namespace
Drupal\config_override\EventSubscriber
;
use
Drupal\Core\Config\ConfigEvents
;
use
Drupal\Core\Config\ConfigModuleOverridesEvent
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
...
...
@@ -34,7 +35,7 @@ public function onConfigModuleOverride(ConfigModuleOverridesEvent $event) {
* An array of event listener definitions.
*/
static
function
getSubscribedEvents
()
{
$events
[
'c
onfig
.module.overrides'
][]
=
array
(
'onConfigModuleOverride'
,
40
);
$events
[
C
onfig
Events
::
MODULE_OVERRIDES
][]
=
array
(
'onConfigModuleOverride'
,
40
);
return
$events
;
}
}
...
...
core/modules/language/lib/Drupal/language/EventSubscriber/ConfigSubscriber.php
View file @
6c70c013
...
...
@@ -8,7 +8,8 @@
namespace
Drupal\language\EventSubscriber
;
use
Drupal\Component\PhpStorage\PhpStorageFactory
;
use
Drupal\Core\Config\ConfigEvent
;
use
Drupal\Core\Config\ConfigCrudEvent
;
use
Drupal\Core\Config\ConfigEvents
;
use
Symfony\Component\EventDispatcher\EventSubscriberInterface
;
/**
...
...
@@ -19,12 +20,12 @@ class ConfigSubscriber implements EventSubscriberInterface {
/**
* Causes the container to be rebuilt on the next request.
*
* @param ConfigEvent $event
* @param Config
Crud
Event $event
* The configuration event.
*/
public
function
onConfigSave
(
ConfigEvent
$event
)
{
public
function
onConfigSave
(
Config
Crud
Event
$event
)
{
$saved_config
=
$event
->
getConfig
();
if
(
$saved_config
->
getName
()
==
'system.site'
&&
$
saved_config
->
get
(
'langcode'
)
!=
$saved_config
->
getOriginal
(
'langcode'
))
{
if
(
$saved_config
->
getName
()
==
'system.site'
&&
$
event
->
isChanged
(
'langcode'
))
{
// Trigger a container rebuild on the next request by deleting compiled
// from PHP storage.
PhpStorageFactory
::
get
(
'service_container'
)
->
deleteAll
();
...
...
@@ -35,7 +36,7 @@ public function onConfigSave(ConfigEvent $event) {
* {@inheritdoc}
*/
static
function
getSubscribedEvents
()
{
$events
[
'c
onfig
.save'
][]
=
array
(
'onConfigSave'
,
0
);
$events
[
C
onfig
Events
::
SAVE
][]
=
array
(
'onConfigSave'
,
0
);
return
$events
;
}
...
...
core/modules/system/lib/Drupal/system/SystemConfigSubscriber.php
View file @
6c70c013
...
...
@@ -7,6 +7,7 @@
namespace
Drupal\system
;
use
Drupal\Core\Config\ConfigEvents
;
use
Drupal\Core\Config\ConfigImporterEvent
;
use
Drupal\Core\Config\ConfigImporterException
;
use
Drupal\Core\Config\StorageDispatcher
;
...
...
@@ -21,7 +22,7 @@ class SystemConfigSubscriber implements EventSubscriberInterface {
* {@inheritdoc}
*/
static
function
getSubscribedEvents
()
{
$events
[
'c
onfig
.importer.validate'
][]
=
array
(
'onConfigImporterValidate'
,
20
);
$events
[
C
onfig
Events
::
VALIDATE
][]
=
array
(
'onConfigImporterValidate'
,
20
);
return
$events
;
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment