Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
NotFoundResourceException example
if
(
!
class_exists
(
XmlUtils::
class
)
)
{
throw
new
RuntimeException
(
'Loading translations from the Xliff format requires the Symfony Config component.'
)
;
}
if
(
!
$this
->
isXmlString
(
$resource
)
)
{
if
(
!
stream_is_local
(
$resource
)
)
{
throw
new
InvalidResourceException
(
sprintf
(
'This is not a local file "%s".',
$resource
)
)
;
}
if
(
!
file_exists
(
$resource
)
)
{
throw
new
NotFoundResourceException
(
sprintf
(
'File "%s" not found.',
$resource
)
)
;
}
if
(
!
is_file
(
$resource
)
)
{
throw
new
InvalidResourceException
(
sprintf
(
'This is neither a file nor an XLIFF string "%s".',
$resource
)
)
;
}
}
try
{
if
(
$this
->
isXmlString
(
$resource
)
)
{
$dom
= XmlUtils::
parse
(
$resource
)
;
}
else
{
public
function
load
(
mixed
$resource
, string
$locale
, string
$domain
= 'messages'
)
: MessageCatalogue
{
if
(
!
class_exists
(
XmlUtils::
class
)
)
{
throw
new
RuntimeException
(
'Loading translations from the QT format requires the Symfony Config component.'
)
;
}
if
(
!
stream_is_local
(
$resource
)
)
{
throw
new
InvalidResourceException
(
sprintf
(
'This is not a local file "%s".',
$resource
)
)
;
}
if
(
!
file_exists
(
$resource
)
)
{
throw
new
NotFoundResourceException
(
sprintf
(
'File "%s" not found.',
$resource
)
)
;
}
try
{
$dom
= XmlUtils::
loadFile
(
$resource
)
;
}
catch
(
\InvalidArgumentException
$e
)
{
throw
new
InvalidResourceException
(
sprintf
(
'Unable to load "%s".',
$resource
)
,
$e
->
getCode
(
)
,
$e
)
;
}
$internalErrors
=
libxml_use_internal_errors
(
true
)
;
libxml_clear_errors
(
)
;
abstract
class
FileLoader
extends
ArrayLoader
{
public
function
load
(
mixed
$resource
, string
$locale
, string
$domain
= 'messages'
)
: MessageCatalogue
{
if
(
!
stream_is_local
(
$resource
)
)
{
throw
new
InvalidResourceException
(
sprintf
(
'This is not a local file "%s".',
$resource
)
)
;
}
if
(
!
file_exists
(
$resource
)
)
{
throw
new
NotFoundResourceException
(
sprintf
(
'File "%s" not found.',
$resource
)
)
;
}
$messages
=
$this
->
loadResource
(
$resource
)
;
// empty resource
$messages
??=
[
]
;
// not an array
if
(
!\
is_array
(
$messages
)
)
{
throw
new
InvalidResourceException
(
sprintf
(
'Unable to load file "%s".',
$resource
)
)
;
}
class
IcuDatFileLoader
extends
IcuResFileLoader
{
public
function
load
(
mixed
$resource
, string
$locale
, string
$domain
= 'messages'
)
: MessageCatalogue
{
if
(
!
stream_is_local
(
$resource
.'.dat'
)
)
{
throw
new
InvalidResourceException
(
sprintf
(
'This is not a local file "%s".',
$resource
)
)
;
}
if
(
!
file_exists
(
$resource
.'.dat'
)
)
{
throw
new
NotFoundResourceException
(
sprintf
(
'File "%s" not found.',
$resource
)
)
;
}
try
{
$rb
=
new
\
ResourceBundle
(
$locale
,
$resource
)
;
}
catch
(
\Exception
)
{
$rb
= null;
}
if
(
!
$rb
)
{
throw
new
InvalidResourceException
(
sprintf
(
'Cannot load resource "%s".',
$resource
)
)
;
}
elseif
(
intl_is_failure
(
$rb
->
getErrorCode
(
)
)
)
{
private
string
$delimiter
= ';';
private
string
$enclosure
= '"';
private
string
$escape
= '\\';
protected
function
loadResource
(
string
$resource
)
: array
{
$messages
=
[
]
;
try
{
$file
=
new
\
SplFileObject
(
$resource
, 'rb'
)
;
}
catch
(
\RuntimeException
$e
)
{
throw
new
NotFoundResourceException
(
sprintf
(
'Error opening file "%s".',
$resource
)
, 0,
$e
)
;
}
$file
->
setFlags
(
\SplFileObject::READ_CSV | \SplFileObject::SKIP_EMPTY
)
;
$file
->
setCsvControl
(
$this
->delimiter,
$this
->enclosure,
$this
->escape
)
;
foreach
(
$file
as
$data
)
{
if
(
false ===
$data
)
{
continue
;
}
if
(
!
str_starts_with
(
$data
[
0
]
, '#'
)
&&
isset
(
$data
[
1
]
)
&& 2 === \
count
(
$data
)
)
{
class
IcuResFileLoader
implements
LoaderInterface
{
public
function
load
(
mixed
$resource
, string
$locale
, string
$domain
= 'messages'
)
: MessageCatalogue
{
if
(
!
stream_is_local
(
$resource
)
)
{
throw
new
InvalidResourceException
(
sprintf
(
'This is not a local file "%s".',
$resource
)
)
;
}
if
(
!
is_dir
(
$resource
)
)
{
throw
new
NotFoundResourceException
(
sprintf
(
'File "%s" not found.',
$resource
)
)
;
}
try
{
$rb
=
new
\
ResourceBundle
(
$locale
,
$resource
)
;
}
catch
(
\Exception
)
{
$rb
= null;
}
if
(
!
$rb
)
{
throw
new
InvalidResourceException
(
sprintf
(
'Cannot load resource "%s".',
$resource
)
)
;
}
elseif
(
intl_is_failure
(
$rb
->
getErrorCode
(
)
)
)
{