Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
getProvidedTypes example
if
(
!
class_exists
(
BaseNode::
class
)
|| !
$extensions
=
$container
->
getExtensions
(
)
)
{
return
;
}
$resolvingBag
=
$container
->
getParameterBag
(
)
;
if
(
!
$resolvingBag
instanceof EnvPlaceholderParameterBag
)
{
return
;
}
$defaultBag
=
new
ParameterBag
(
$resolvingBag
->
all
(
)
)
;
$envTypes
=
$resolvingBag
->
getProvidedTypes
(
)
;
foreach
(
$resolvingBag
->
getEnvPlaceholders
(
)
+
$resolvingBag
->
getUnusedEnvPlaceholders
(
)
as
$env
=>
$placeholders
)
{
$values
=
[
]
;
if
(
false ===
$i
=
strpos
(
$env
, ':'
)
)
{
$default
=
$defaultBag
->
has
(
"env(
$env
)"
)
?
$defaultBag
->
get
(
"env(
$env
)"
)
: self::TYPE_FIXTURES
[
'string'
]
;
$defaultType
= null !==
$default
?
get_debug_type
(
$default
)
: 'string';
$values
[
$defaultType
]
=
$default
;
}
else
{
$prefix
=
substr
(
$env
, 0,
$i
)
;
foreach
(
$envTypes
[
$prefix
]
??
[
'string'
]
as
$type
)
{
$values
[
$type
]
= self::TYPE_FIXTURES
[
$type
]
?? null;
}
}
public
function
process
(
ContainerBuilder
$container
)
{
$bag
=
$container
->
getParameterBag
(
)
;
$types
=
[
]
;
$processors
=
[
]
;
foreach
(
$container
->
findTaggedServiceIds
(
'container.env_var_processor'
)
as
$id
=>
$tags
)
{
if
(
!
$r
=
$container
->
getReflectionClass
(
$class
=
$container
->
getDefinition
(
$id
)
->
getClass
(
)
)
)
{
throw
new
InvalidArgumentException
(
sprintf
(
'Class "%s" used for service "%s" cannot be found.',
$class
,
$id
)
)
;
}
elseif
(
!
$r
->
isSubclassOf
(
EnvVarProcessorInterface::
class
)
)
{
throw
new
InvalidArgumentException
(
sprintf
(
'Service "%s" must implement interface "%s".',
$id
, EnvVarProcessorInterface::
class
)
)
;
}
foreach
(
$class
::
getProvidedTypes
(
)
as
$prefix
=>
$type
)
{
$processors
[
$prefix
]
=
new
Reference
(
$id
)
;
$types
[
$prefix
]
= self::
validateProvidedTypes
(
$type
,
$class
)
;
}
}
if
(
$bag
instanceof EnvPlaceholderParameterBag
)
{
foreach
(
EnvVarProcessor::
getProvidedTypes
(
)
as
$prefix
=>
$type
)
{
if
(
!
isset
(
$types
[
$prefix
]
)
)
{
$types
[
$prefix
]
= self::
validateProvidedTypes
(
$type
, EnvVarProcessor::
class
)
;
}
}
$env
=
$this
->container->
getParameter
(
"env(
$name
)"
)
;
}
}
if
(
null ===
$env
)
{
if
(
$returnNull
)
{
return
null;
}
if
(
!
isset
(
$this
->
getProvidedTypes
(
)
[
$prefix
]
)
)
{
throw
new
RuntimeException
(
sprintf
(
'Unsupported env var prefix "%s".',
$prefix
)
)
;
}
if
(
!\
in_array
(
$prefix
,
[
'string', 'bool', 'not', 'int', 'float'
]
, true
)
)
{
return
null;
}
}
if
(
'shuffle' ===
$prefix
)
{
\
is_array
(
$env
)
?
shuffle
(
$env
)
:
throw
new
RuntimeException
(
sprintf
(
'Env var "%s" cannot be shuffled, expected array, got "%s".',
$name
,
get_debug_type
(
$env
)
)
)
;
'query_string' =>
[
'array'
]
,
'resolve' =>
[
'string'
]
,
'default' =>
[
'bool', 'int', 'float', 'string', 'array'
]
,
'string' =>
[
'string'
]
,
'trim' =>
[
'string'
]
,
'require' =>
[
'bool', 'int', 'float', 'string', 'array'
]
,
'enum' =>
[
\BackedEnum::
class
]
,
'shuffle' =>
[
'array'
]
,
'defined' =>
[
'bool'
]
,
]
;
$this
->
assertSame
(
$expected
,
$container
->
getParameterBag
(
)
->
getProvidedTypes
(
)
)
;
}
public
function
testNoProcessor
(
)
{
$container
=
new
ContainerBuilder
(
)
;
(
new
RegisterEnvVarProcessorsPass
(
)
)
->
process
(
$container
)
;
$this
->
assertFalse
(
$container
->
has
(
'container.env_var_processors_locator'
)
)
;
}
$env
=
$this
->container->
getParameter
(
"env(
$name
)"
)
;
}
}
if
(
null ===
$env
)
{
if
(
$returnNull
)
{
return
null;
}
if
(
!
isset
(
$this
->
getProvidedTypes
(
)
[
$prefix
]
)
)
{
throw
new
RuntimeException
(
sprintf
(
'Unsupported env var prefix "%s".',
$prefix
)
)
;
}
if
(
!\
in_array
(
$prefix
,
[
'string', 'bool', 'not', 'int', 'float'
]
, true
)
)
{
return
null;
}
}
if
(
'shuffle' ===
$prefix
)
{
\
is_array
(
$env
)
?
shuffle
(
$env
)
:
throw
new
RuntimeException
(
sprintf
(
'Env var "%s" cannot be shuffled, expected array, got "%s".',
$name
,
get_debug_type
(
$env
)
)
)
;
public
function
process
(
ContainerBuilder
$container
)
{
$bag
=
$container
->
getParameterBag
(
)
;
$types
=
[
]
;
$processors
=
[
]
;
foreach
(
$container
->
findTaggedServiceIds
(
'container.env_var_processor'
)
as
$id
=>
$tags
)
{
if
(
!
$r
=
$container
->
getReflectionClass
(
$class
=
$container
->
getDefinition
(
$id
)
->
getClass
(
)
)
)
{
throw
new
InvalidArgumentException
(
sprintf
(
'Class "%s" used for service "%s" cannot be found.',
$class
,
$id
)
)
;
}
elseif
(
!
$r
->
isSubclassOf
(
EnvVarProcessorInterface::
class
)
)
{
throw
new
InvalidArgumentException
(
sprintf
(
'Service "%s" must implement interface "%s".',
$id
, EnvVarProcessorInterface::
class
)
)
;
}
foreach
(
$class
::
getProvidedTypes
(
)
as
$prefix
=>
$type
)
{
$processors
[
$prefix
]
=
new
Reference
(
$id
)
;
$types
[
$prefix
]
= self::
validateProvidedTypes
(
$type
,
$class
)
;
}
}
if
(
$bag
instanceof EnvPlaceholderParameterBag
)
{
foreach
(
EnvVarProcessor::
getProvidedTypes
(
)
as
$prefix
=>
$type
)
{
if
(
!
isset
(
$types
[
$prefix
]
)
)
{
$types
[
$prefix
]
= self::
validateProvidedTypes
(
$type
, EnvVarProcessor::
class
)
;
}
}
if
(
!
class_exists
(
BaseNode::
class
)
|| !
$extensions
=
$container
->
getExtensions
(
)
)
{
return
;
}
$resolvingBag
=
$container
->
getParameterBag
(
)
;
if
(
!
$resolvingBag
instanceof EnvPlaceholderParameterBag
)
{
return
;
}
$defaultBag
=
new
ParameterBag
(
$resolvingBag
->
all
(
)
)
;
$envTypes
=
$resolvingBag
->
getProvidedTypes
(
)
;
foreach
(
$resolvingBag
->
getEnvPlaceholders
(
)
+
$resolvingBag
->
getUnusedEnvPlaceholders
(
)
as
$env
=>
$placeholders
)
{
$values
=
[
]
;
if
(
false ===
$i
=
strpos
(
$env
, ':'
)
)
{
$default
=
$defaultBag
->
has
(
"env(
$env
)"
)
?
$defaultBag
->
get
(
"env(
$env
)"
)
: self::TYPE_FIXTURES
[
'string'
]
;
$defaultType
= null !==
$default
?
get_debug_type
(
$default
)
: 'string';
$values
[
$defaultType
]
=
$default
;
}
else
{
$prefix
=
substr
(
$env
, 0,
$i
)
;
foreach
(
$envTypes
[
$prefix
]
??
[
'string'
]
as
$type
)
{
$values
[
$type
]
= self::TYPE_FIXTURES
[
$type
]
?? null;
}
}