Commit e5b70636 authored by catch's avatar catch

Issue #3020165 by alexpott: Dependency objects can be optimised for serialisation

parent 56ccba06
...@@ -190,4 +190,14 @@ public static function createFromString($dependency) { ...@@ -190,4 +190,14 @@ public static function createFromString($dependency) {
return new static($name, $project, $version_string); return new static($name, $project, $version_string);
} }
/**
* Prevents unnecessary serialization of constraint objects.
*
* @return array
* The properties to seriailize.
*/
public function __sleep() {
return ['name', 'project', 'constraintString'];
}
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
namespace Drupal\Tests\Core\Extension; namespace Drupal\Tests\Core\Extension;
use Drupal\Component\Version\Constraint;
use Drupal\Core\Extension\Dependency; use Drupal\Core\Extension\Dependency;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
...@@ -100,4 +101,19 @@ public function testOffsetSet() { ...@@ -100,4 +101,19 @@ public function testOffsetSet() {
$dependency['name'] = 'foo'; $dependency['name'] = 'foo';
} }
/**
* Ensures that constraint objects are not serialized.
*
* @covers ::__sleep
*/
public function testSerialization() {
$dependency = new Dependency('paragraphs_demo', 'paragraphs', '>8.x-1.1');
$this->assertTrue($dependency->isCompatible('1.2'));
$this->assertInstanceOf(Constraint::class, $this->getObjectAttribute($dependency, 'constraint'));
$dependency = unserialize(serialize($dependency));
$this->assertNull($this->getObjectAttribute($dependency, 'constraint'));
$this->assertTrue($dependency->isCompatible('1.2'));
$this->assertInstanceOf(Constraint::class, $this->getObjectAttribute($dependency, 'constraint'));
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment