Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
| Total | |
0.00% |
0 / 1 |
|
50.00% |
1 / 2 |
CRAP | |
86.67% |
13 / 15 |
| DependencySerializationTrait | |
0.00% |
0 / 1 |
|
50.00% |
1 / 2 |
7.12 | |
86.67% |
13 / 15 |
| __sleep | |
0.00% |
0 / 1 |
5.20 | |
80.00% |
8 / 10 |
|||
| __wakeup | |
100.00% |
1 / 1 |
2 | |
100.00% |
5 / 5 |
|||
| <?php | |
| /** | |
| * @file | |
| * Contains \Drupal\Core\DependencyInjection\DependencySerializationTrait. | |
| */ | |
| namespace Drupal\Core\DependencyInjection; | |
| use Symfony\Component\DependencyInjection\ContainerInterface; | |
| /** | |
| * Provides dependency injection friendly methods for serialization. | |
| */ | |
| trait DependencySerializationTrait { | |
| /** | |
| * An array of service IDs keyed by property name used for serialization. | |
| * | |
| * @var array | |
| */ | |
| protected $_serviceIds = array(); | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function __sleep() { | |
| $this->_serviceIds = array(); | |
| $vars = get_object_vars($this); | |
| foreach ($vars as $key => $value) { | |
| if (is_object($value) && isset($value->_serviceId)) { | |
| // If a class member was instantiated by the dependency injection | |
| // container, only store its ID so it can be used to get a fresh object | |
| // on unserialization. | |
| $this->_serviceIds[$key] = $value->_serviceId; | |
| unset($vars[$key]); | |
| } | |
| // Special case the container, which might not have a service ID. | |
| elseif ($value instanceof ContainerInterface) { | |
| $this->_serviceIds[$key] = 'service_container'; | |
| unset($vars[$key]); | |
| } | |
| } | |
| return array_keys($vars); | |
| } | |
| /** | |
| * {@inheritdoc} | |
| */ | |
| public function __wakeup() { | |
| $container = \Drupal::getContainer(); | |
| foreach ($this->_serviceIds as $key => $service_id) { | |
| $this->$key = $container->get($service_id); | |
| } | |
| $this->_serviceIds = array(); | |
| } | |
| } |