Skip to content
This repository was archived by the owner on Sep 16, 2021. It is now read-only.

Commit 10fa629

Browse files
committed
Little loader refactoring
1 parent b754296 commit 10fa629

File tree

7 files changed

+42
-42
lines changed

7 files changed

+42
-42
lines changed

AutoRoute/Mapping/Loader/XmlFileLoader.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,16 +75,16 @@ protected function parseMappingNode(\DOMElement $mappingNode, $path)
7575
}
7676
$classMetadata = new ClassMetadata($className);
7777

78-
$classMetadata->setUrlSchema(
79-
$this->readAttribute($mappingNode, 'url-schema', sprintf('for "%s" in "%s"', $className, $path))
80-
);
81-
8278
try {
79+
$classMetadata->setUrlSchema(
80+
$this->readAttribute($mappingNode, 'url-schema', sprintf('for "%s" in "%s"', $className, $path))
81+
);
82+
8383
$classMetadata->setExtendedClass(
8484
$this->readAttribute($mappingNode, 'extend', sprintf('for "%s" in "%s"', $className, $path))
8585
);
8686
} catch (\InvalidArgumentException $e) {
87-
// the extend attribute may be omitted
87+
// the extend and url-schema attributes may be omitted
8888
}
8989

9090
$conflictResolverNodes = $mappingNode->getElementsByTagNameNS(self::NAMESPACE_URI, 'conflict-resolver');

AutoRoute/Mapping/Loader/YmlFileLoader.php

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -58,39 +58,46 @@ public function load($file, $type = null)
5858
}
5959

6060
$metadatas = array();
61-
foreach ($config as $className => $metadata) {
62-
if (!class_exists($className)) {
63-
throw new \InvalidArgumentException(sprintf('Configuration found for unknown class "%s" in "%s".', $className, $path));
64-
}
65-
$data = new ClassMetadata($className);
61+
foreach ($config as $className => $mappingNode) {
62+
$metadatas[] = $this->parseMappingNode($className, $mappingNode, $path);
63+
}
6664

67-
if (!isset($metadata['url_schema'])) {
68-
throw new \InvalidArgumentException(sprintf('No URL schema specified for "%s" in "%s".', $className, $path));
69-
}
70-
$data->setUrlSchema($metadata['url_schema']);
65+
return $metadatas;
66+
}
7167

72-
if (isset($metadata['conflict_resolver'])) {
73-
$data->setConflictResolver($this->parseServiceConfig($metadata['conflict_resolver'], $className, $path));
74-
}
68+
/**
69+
* @param string $className
70+
* @param array $mappingNode
71+
* @param string $path
72+
*/
73+
protected function parseMappingNode($className, $mappingNode, $path)
74+
{
75+
if (!class_exists($className)) {
76+
throw new \InvalidArgumentException(sprintf('Configuration found for unknown class "%s" in "%s".', $className, $path));
77+
}
78+
$classMetadata = new ClassMetadata($className);
7579

76-
if (isset($metadata['extend'])) {
77-
$data->setExtendedClass($metadata['extend']);
78-
}
80+
if (isset($mappingNode['url_schema'])) {
81+
$classMetadata->setUrlSchema($mappingNode['url_schema']);
82+
}
7983

80-
// token providers can be omitted if the schema is constructed of
81-
// inherited token providers only
82-
if (isset($metadata['token_providers'])) {
83-
foreach ($metadata['token_providers'] as $tokenName => $provider) {
84-
$data->addTokenProvider($tokenName, $this->parseServiceConfig($provider, $className, $path));
85-
}
86-
}
84+
if (isset($mappingNode['conflict_resolver'])) {
85+
$classMetadata->setConflictResolver($this->parseServiceConfig($mappingNode['conflict_resolver'], $className, $path));
86+
}
8787

88-
// add ClassMetadata to registered metadatas in the end, to ensure no
89-
// incomplete metadatas are registered.
90-
$metadatas[] = $data;
88+
if (isset($mappingNode['extend'])) {
89+
$classMetadata->setExtendedClass($mappingNode['extend']);
9190
}
9291

93-
return $metadatas;
92+
// token providers can be omitted if the schema is constructed of
93+
// inherited token providers only
94+
if (isset($mappingNode['token_providers'])) {
95+
foreach ($mappingNode['token_providers'] as $tokenName => $provider) {
96+
$classMetadata->addTokenProvider($tokenName, $this->parseServiceConfig($provider, $className, $path));
97+
}
98+
}
99+
100+
return $classMetadata;
94101
}
95102

96103
/**

Tests/Resources/Fixtures/loader_config/invalid2.xml

Lines changed: 0 additions & 4 deletions
This file was deleted.
Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
stdClass: ~
1+
stdClass:
2+
url_schema: /forum/{category}/{post_title}
3+
token_providers:
4+
category: [foo]

Tests/Resources/Fixtures/loader_config/invalid4.yml

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

Tests/Unit/AutoRoute/Mapping/Loader/XmlFileLoaderTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,6 @@ public function getFailsOnInvalidConfigFilesData()
7474
{
7575
$files = array(
7676
'invalid1.xml',
77-
'invalid2.xml',
7877
);
7978

8079
return array_map(function ($file) {

Tests/Unit/AutoRoute/Mapping/Loader/YmlFileLoaderTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ public function getFailsOnInvalidConfigFilesData()
7676
'invalid1.yml',
7777
'invalid2.yml',
7878
'invalid3.yml',
79-
'invalid4.yml',
8079
);
8180

8281
return array_map(function ($file) {

0 commit comments

Comments
 (0)