Issue #3021898 by longwave, alexpott, AaronBauman, daffie, opdavies: Support...
Issue #3021898 by longwave, alexpott, AaronBauman, daffie, opdavies: Support _defaults key in service.yml files for public, tags and autowire settings
(cherry picked from commit 3d682d17)
thrownewInvalidArgumentException(sprintf('Service "_defaults" key must be an array, "%s" given in "%s".',\gettype($defaults),$file));
}
foreach($defaultsas$key=>$default){
if(!isset(self::DEFAULTS_KEYWORDS[$key])){
thrownewInvalidArgumentException(sprintf('The configuration key "%s" cannot be used to define a default value in "%s". Allowed keys are "%s".',$key,$file,implode('", "',self::DEFAULTS_KEYWORDS)));
}
}
if(isset($defaults['tags'])){
if(!\is_array($tags=$defaults['tags'])){
thrownewInvalidArgumentException(sprintf('Parameter "tags" in "_defaults" must be an array in "%s". Check your YAML syntax.',$file));
}
foreach($tagsas$tag){
if(!\is_array($tag)){
$tag=['name'=>$tag];
}
if(!isset($tag['name'])){
thrownewInvalidArgumentException(sprintf('A "tags" entry in "_defaults" is missing a "name" key in "%s".',$file));
}
$name=$tag['name'];
unset($tag['name']);
if(!\is_string($name)||''===$name){
thrownewInvalidArgumentException(sprintf('The tag name in "_defaults" must be a non-empty string in "%s".',$file));
}
foreach($tagas$attribute=>$value){
if(!is_scalar($value)&&null!==$value){
thrownewInvalidArgumentException(sprintf('Tag "%s", attribute "%s" in "_defaults" must be of a scalar-type in "%s". Check your YAML syntax.',$name,$attribute,$file));
@@ -176,6 +244,18 @@ private function parseDefinition($id, $service, $file)
$definition=newDefinition();
}
// Drupal services are public by default.
$definition->setPublic(true);
if(isset($defaults['public'])){
$definition->setPublic($defaults['public']);
}
if(isset($defaults['autowire'])){
$definition->setAutowired($defaults['autowire']);
}
$definition->setChanges([]);
if(isset($service['class'])){
$definition->setClass($service['class']);
}
@@ -195,9 +275,6 @@ private function parseDefinition($id, $service, $file)
if(isset($service['public'])){
$definition->setPublic($service['public']);
}
else{
$definition->setPublic(true);
}
if(isset($service['abstract'])){
$definition->setAbstract($service['abstract']);
@@ -271,32 +348,38 @@ private function parseDefinition($id, $service, $file)
}
}
if(isset($service['tags'])){
if(!is_array($service['tags'])){
thrownewInvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in %s. Check your YAML syntax.',$id,$file));
$tags=$service['tags']??[];
if(!\is_array($tags)){
thrownewInvalidArgumentException(sprintf('Parameter "tags" must be an array for service "%s" in "%s". Check your YAML syntax.',$id,$file));
}
foreach($service['tags']as$tag){
if(!is_array($tag)){
thrownewInvalidArgumentException(sprintf('A "tags" entry must be an array for service "%s" in %s. Check your YAML syntax.',$id,$file));
if(isset($defaults['tags'])){
$tags=array_merge($tags,$defaults['tags']);
}
if(!isset($tag['name'])){
thrownewInvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in %s.',$id,$file));
foreach($tagsas$tag){
if(!\is_array($tag)){
$tag=['name'=>$tag];
}
if(!isset($tag['name'])){
thrownewInvalidArgumentException(sprintf('A "tags" entry is missing a "name" key for service "%s" in "%s".',$id,$file));
}
$name=$tag['name'];
unset($tag['name']);
if(!\is_string($name)||''===$name){
thrownewInvalidArgumentException(sprintf('The tag name for service "%s" in "%s" must be a non-empty string.',$id,$file));
}
foreach($tagas$attribute=>$value){
if(!is_scalar($value)&&null!==$value){
thrownewInvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in %s. Check your YAML syntax.',$id,$name,$attribute,$file));
thrownewInvalidArgumentException(sprintf('A "tags" attribute must be of a scalar-type for service "%s", tag "%s", attribute "%s" in "%s". Check your YAML syntax.',$id,$name,$attribute,$file));