Skip to content

Commit 6e79dea

Browse files
committed
feature #19507 [FrameworkBundle] Introduce a cache warmer for Serializer based on PhpArrayAdapter (tgalopin)
This PR was merged into the 3.2-dev branch. Discussion ---------- [FrameworkBundle] Introduce a cache warmer for Serializer based on PhpArrayAdapter | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Following the cache warmer for annotations (#18533) and for the validator (#19485), this PR introduces a cache warmer for the Serializer YAML and XML metadata configuration (mainly groups). Based on the PhpArrayAdapter, it uses the naming conventions (Resources/config/serialization) to find the files and compile them into a single PHP file stored in the cache directory. This file uses shared memory on PHP 7. The benefit of this PR are the same than the ones of the previous PR: - serialization metadata cache can be warmed up offline - on PHP 7, there is no need for user extension to get maximum performances (ie. if you use this PR and the other one, you probably won't need to enable APCu to have great performances) - on PHP 7 again, we are not sensitive to APCu memory fragmentation last but not least, global performance is slightly better (I get 30us per class gain in Blackfire) As previous work on the Serializer cache system introduced issues (see symfony/symfony@96e418a), it would be interesting to pay careful attention to the backward compatibility during the review (ping @Ener-Getick). Commits ------- 810f469 [FrameworkBundle] Introduce a cache warmer for Serializer based on PhpArrayAdapter
2 parents 80e172d + 37c0bf7 commit 6e79dea

File tree

2 files changed

+71
-24
lines changed

2 files changed

+71
-24
lines changed

Mapping/Loader/XmlFileLoader.php

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,11 @@ class XmlFileLoader extends FileLoader
3636
public function loadClassMetadata(ClassMetadataInterface $classMetadata)
3737
{
3838
if (null === $this->classes) {
39-
$this->classes = array();
40-
$xml = $this->parseFile($this->file);
39+
$this->classes = $this->getClassesFromXml();
40+
}
4141

42-
foreach ($xml->class as $class) {
43-
$this->classes[(string) $class['name']] = $class;
44-
}
42+
if (!$this->classes) {
43+
return false;
4544
}
4645

4746
$attributesMetadata = $classMetadata->getAttributesMetadata();
@@ -74,6 +73,20 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
7473
return false;
7574
}
7675

76+
/**
77+
* Return the names of the classes mapped in this file.
78+
*
79+
* @return string[] The classes names
80+
*/
81+
public function getMappedClasses()
82+
{
83+
if (null === $this->classes) {
84+
$this->classes = $this->getClassesFromXml();
85+
}
86+
87+
return array_keys($this->classes);
88+
}
89+
7790
/**
7891
* Parses a XML File.
7992
*
@@ -93,4 +106,16 @@ private function parseFile($file)
93106

94107
return simplexml_import_dom($dom);
95108
}
109+
110+
private function getClassesFromXml()
111+
{
112+
$xml = $this->parseFile($this->file);
113+
$classes = array();
114+
115+
foreach ($xml->class as $class) {
116+
$classes[(string) $class['name']] = $class;
117+
}
118+
119+
return $classes;
120+
}
96121
}

Mapping/Loader/YamlFileLoader.php

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -38,26 +38,11 @@ class YamlFileLoader extends FileLoader
3838
public function loadClassMetadata(ClassMetadataInterface $classMetadata)
3939
{
4040
if (null === $this->classes) {
41-
if (!stream_is_local($this->file)) {
42-
throw new MappingException(sprintf('This is not a local file "%s".', $this->file));
43-
}
44-
45-
if (null === $this->yamlParser) {
46-
$this->yamlParser = new Parser();
47-
}
48-
49-
$classes = $this->yamlParser->parse(file_get_contents($this->file));
50-
51-
if (empty($classes)) {
52-
return false;
53-
}
54-
55-
// not an array
56-
if (!is_array($classes)) {
57-
throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
58-
}
41+
$this->classes = $this->getClassesFromYaml();
42+
}
5943

60-
$this->classes = $classes;
44+
if (!$this->classes) {
45+
return false;
6146
}
6247

6348
if (isset($this->classes[$classMetadata->getName()])) {
@@ -103,4 +88,41 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata)
10388

10489
return false;
10590
}
91+
92+
/**
93+
* Return the names of the classes mapped in this file.
94+
*
95+
* @return string[] The classes names
96+
*/
97+
public function getMappedClasses()
98+
{
99+
if (null === $this->classes) {
100+
$this->classes = $this->getClassesFromYaml();
101+
}
102+
103+
return array_keys($this->classes);
104+
}
105+
106+
private function getClassesFromYaml()
107+
{
108+
if (!stream_is_local($this->file)) {
109+
throw new MappingException(sprintf('This is not a local file "%s".', $this->file));
110+
}
111+
112+
if (null === $this->yamlParser) {
113+
$this->yamlParser = new Parser();
114+
}
115+
116+
$classes = $this->yamlParser->parse(file_get_contents($this->file));
117+
118+
if (empty($classes)) {
119+
return array();
120+
}
121+
122+
if (!is_array($classes)) {
123+
throw new MappingException(sprintf('The file "%s" must contain a YAML array.', $this->file));
124+
}
125+
126+
return $classes;
127+
}
106128
}

0 commit comments

Comments
 (0)