Code
Explorer
You are a developer and looking for Shopware projects?
Apply Now!
Database example
/** @var array<string, string> $dbConfig */
$dbConfig
=
$this
->
getContainer
(
)
->
getParameter
(
'shopware.db'
)
;
$rootDir
=
$this
->
getContainer
(
)
->
getParameter
(
'kernel.root_dir'
)
;
if
(
!\
is_string
(
$rootDir
)
)
{
throw
new
RuntimeException
(
'Parameter kernel.root_dir has to be a string'
)
;
}
$connection
=
$this
->
createConnection
(
$dbConfig
)
;
$database
=
new
Database
(
$connection
)
;
$steps
=
$input
->
getOption
(
'steps'
)
;
$steps
=
array_filter
(
explode
(
',',
$steps
)
)
;
foreach
(
$steps
as
$step
)
{
if
(
!\
in_array
(
$step
,
$this
->validSteps, true
)
)
{
$io
->
error
(
sprintf
(
"Unknown install step (%s). Valid steps: %s\n",
$step
,
implode
(
', ',
$this
->validSteps
)
)
)
;
return
1;
}
/** * Ensures the database Connection Manager/Factory is loaded and ready to use. */
protected
static
function
ensureFactory
(
)
{
if
(
static
::
$factory
instanceof Database
)
{
return
;
}
static
::
$factory
=
new
Database
(
)
;
}
}
protected
$arguments
=
[
'seeder_name' => 'The seeder name to run',
]
;
/** * Passes to Seeder to populate the database. */
public
function
run
(
array
$params
)
{
$seeder
=
new
Seeder
(
new
Database
(
)
)
;
$seedName
=
array_shift
(
$params
)
;
if
(
empty
(
$seedName
)
)
{
$seedName
= CLI::
prompt
(
lang
(
'Migrations.migSeeder'
)
, null, 'required'
)
; // @codeCoverageIgnore
}
try
{
$seeder
->
call
(
$seedName
)
;
}
catch
(
Throwable
$e
)
{
$this
->
showError
(
$e
)
;
}
}