Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
followLinks example
$dirs
[
]
=
$defaultDir
;
}
else
{
$nonExistingDirs
[
]
=
$defaultDir
;
}
// Register translation resources
if
(
$dirs
)
{
$files
=
[
]
;
foreach
(
$dirs
as
$dir
)
{
$finder
= Finder::
create
(
)
->
followLinks
(
)
->
files
(
)
->
filter
(
fn
(
\SplFileInfo
$file
)
=> 2 <=
substr_count
(
$file
->
getBasename
(
)
, '.'
)
&&
preg_match
(
'/\.\w+$/',
$file
->
getBasename
(
)
)
)
->
in
(
$dir
)
->
sortByName
(
)
;
foreach
(
$finder
as
$file
)
{
$fileNameParts
=
explode
(
'.',
basename
(
$file
)
)
;
$locale
=
$fileNameParts
[
\
count
(
$fileNameParts
)
- 2
]
;
if
(
!
isset
(
$files
[
$locale
]
)
)
{
$files
[
$locale
]
=
[
]
;
}
}
return
;
}
if
(
!
class_exists
(
Finder::
class
)
)
{
throw
new
\
LogicException
(
'Extended glob patterns cannot be used as the Finder component is not installed. Try running "composer require symfony/finder".'
)
;
}
yield from
(
new
Finder
(
)
)
->
followLinks
(
)
->
filter
(
function
D\SplFileInfo
$info
)
use
(
$regex
,
$prefixLen
,
$prefix
)
{
$normalizedPath
=
str_replace
(
'\\', '/',
$info
->
getPathname
(
)
)
;
if
(
!
preg_match
(
$regex
,
substr
(
$normalizedPath
,
$prefixLen
)
)
|| !
$info
->
isFile
(
)
)
{
return
false;
}
if
(
$this
->excludedPrefixes
)
{
do
{
if
(
isset
(
$this
->excludedPrefixes
[
$dirPath
=
$normalizedPath
]
)
)
{
return
false;
}
}
while
(
$prefix
!==
$dirPath
&&
$dirPath
!==
$normalizedPath
= \
dirname
(
$dirPath
)
)
;
}
while
(
$file
->
valid
(
)
)
;
}
}
else
{
$alternatives
=
[
]
;
if
(
$paths
)
{
$shortnames
=
[
]
;
$dirs
=
[
]
;
foreach
(
current
(
$paths
)
as
$path
)
{
$dirs
[
]
=
$this
->
isAbsolutePath
(
$path
)
?
$path
:
$this
->projectDir.'/'.
$path
;
}
foreach
(
Finder::
create
(
)
->
files
(
)
->
followLinks
(
)
->
in
(
$dirs
)
as
$file
)
{
$shortnames
[
]
=
str_replace
(
'\\', '/',
$file
->
getRelativePathname
(
)
)
;
}
[
$namespace
,
$shortname
]
=
$this
->
parseTemplateName
(
$name
)
;
$alternatives
=
$this
->
findAlternatives
(
$shortname
,
$shortnames
)
;
if
(
FilesystemLoader::MAIN_NAMESPACE !==
$namespace
)
{
$alternatives
=
array_map
(
fn
(
$shortname
)
=> '@'.
$namespace
.'/'.
$shortname
,
$alternatives
)
;
}
}
$this
->
error
(
$io
,
sprintf
(
'Template name "%s" not found',
$name
)
,
$alternatives
)
;
}
$this
->
assertSame
(
$finder
,
$finder
->
filter
(
fn
(
\SplFileInfo
$f
)
=>
str_contains
(
$f
, 'test'
)
)
)
;
$this
->
assertIterator
(
$this
->
toAbsolute
(
[
'test.php', 'test.py'
]
)
,
$finder
->
in
(
self::
$tmpDir
)
->
getIterator
(
)
)
;
}
public
function
testFollowLinks
(
)
{
if
(
'\\' == \DIRECTORY_SEPARATOR
)
{
$this
->
markTestSkipped
(
'symlinks are not supported on Windows'
)
;
}
$finder
=
$this
->
buildFinder
(
)
;
$this
->
assertSame
(
$finder
,
$finder
->
followLinks
(
)
)
;
$this
->
assertIterator
(
$this
->
toAbsolute
(
[
'foo',
'foo/bar.tmp',
'test.php',
'test.py',
'toto',
'foo bar',
'qux',
'qux/baz_100_1.py',
'zebulon.php',
'Zephire.php',
private
function
findTemplatesInDirectory
(
string
$dir
, string
$namespace
= null, array
$excludeDirs
=
[
]
)
: array
{
if
(
!
is_dir
(
$dir
)
)
{
return
[
]
;
}
$templates
=
[
]
;
foreach
(
Finder::
create
(
)
->
files
(
)
->
followLinks
(
)
->
in
(
$dir
)
->
exclude
(
$excludeDirs
)
->
name
(
$this
->namePatterns
)
as
$file
)
{
$templates
[
]
=
(
null !==
$namespace
? '@'.
$namespace
.'/' : ''
)
.
str_replace
(
'\\', '/',
$file
->
getRelativePathname
(
)
)
;
}
return
$templates
;
}
}