Primo Committ
This commit is contained in:
10
vendor/spatie/db-dumper/src/Compressors/Compressor.php
vendored
Normal file
10
vendor/spatie/db-dumper/src/Compressors/Compressor.php
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DbDumper\Compressors;
|
||||
|
||||
interface Compressor
|
||||
{
|
||||
public function useCommand(): string;
|
||||
|
||||
public function useExtension(): string;
|
||||
}
|
||||
16
vendor/spatie/db-dumper/src/Compressors/GzipCompressor.php
vendored
Normal file
16
vendor/spatie/db-dumper/src/Compressors/GzipCompressor.php
vendored
Normal file
@@ -0,0 +1,16 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DbDumper\Compressors;
|
||||
|
||||
class GzipCompressor implements Compressor
|
||||
{
|
||||
public function useCommand(): string
|
||||
{
|
||||
return 'gzip';
|
||||
}
|
||||
|
||||
public function useExtension(): string
|
||||
{
|
||||
return 'gz';
|
||||
}
|
||||
}
|
||||
131
vendor/spatie/db-dumper/src/Databases/MongoDb.php
vendored
Normal file
131
vendor/spatie/db-dumper/src/Databases/MongoDb.php
vendored
Normal file
@@ -0,0 +1,131 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DbDumper\Databases;
|
||||
|
||||
use Spatie\DbDumper\DbDumper;
|
||||
use Spatie\DbDumper\Exceptions\CannotStartDump;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class MongoDb extends DbDumper
|
||||
{
|
||||
protected $port = 27017;
|
||||
|
||||
/** @var null|string */
|
||||
protected $collection = null;
|
||||
|
||||
/** @var null|string */
|
||||
protected $authenticationDatabase = null;
|
||||
|
||||
/**
|
||||
* Dump the contents of the database to the given file.
|
||||
*
|
||||
* @param string $dumpFile
|
||||
*
|
||||
* @throws \Spatie\DbDumper\Exceptions\CannotStartDump
|
||||
* @throws \Spatie\DbDumper\Exceptions\DumpFailed
|
||||
*/
|
||||
public function dumpToFile(string $dumpFile)
|
||||
{
|
||||
$this->guardAgainstIncompleteCredentials();
|
||||
|
||||
$process = $this->getProcess($dumpFile);
|
||||
|
||||
$process->run();
|
||||
|
||||
$this->checkIfDumpWasSuccessFul($process, $dumpFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies if the dbname and host options are set.
|
||||
*
|
||||
* @throws \Spatie\DbDumper\Exceptions\CannotStartDump
|
||||
* @return void
|
||||
*/
|
||||
public function guardAgainstIncompleteCredentials()
|
||||
{
|
||||
foreach (['dbName', 'host'] as $requiredProperty) {
|
||||
if (strlen($this->$requiredProperty) === 0) {
|
||||
throw CannotStartDump::emptyParameter($requiredProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $collection
|
||||
*
|
||||
* @return \Spatie\DbDumper\Databases\MongoDb
|
||||
*/
|
||||
public function setCollection(string $collection)
|
||||
{
|
||||
$this->collection = $collection;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $authenticationDatabase
|
||||
*
|
||||
* @return \Spatie\DbDumper\Databases\MongoDb
|
||||
*/
|
||||
public function setAuthenticationDatabase(string $authenticationDatabase)
|
||||
{
|
||||
$this->authenticationDatabase = $authenticationDatabase;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate the dump command for MongoDb.
|
||||
*
|
||||
* @param string $filename
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDumpCommand(string $filename): string
|
||||
{
|
||||
$quote = $this->determineQuote();
|
||||
|
||||
$command = [
|
||||
"{$quote}{$this->dumpBinaryPath}mongodump{$quote}",
|
||||
"--db {$this->dbName}",
|
||||
'--archive',
|
||||
];
|
||||
|
||||
if ($this->userName) {
|
||||
$command[] = "--username '{$this->userName}'";
|
||||
}
|
||||
|
||||
if ($this->password) {
|
||||
$command[] = "--password '{$this->password}'";
|
||||
}
|
||||
|
||||
if (isset($this->host)) {
|
||||
$command[] = "--host {$this->host}";
|
||||
}
|
||||
|
||||
if (isset($this->port)) {
|
||||
$command[] = "--port {$this->port}";
|
||||
}
|
||||
|
||||
if (isset($this->collection)) {
|
||||
$command[] = "--collection {$this->collection}";
|
||||
}
|
||||
|
||||
if ($this->authenticationDatabase) {
|
||||
$command[] = "--authenticationDatabase {$this->authenticationDatabase}";
|
||||
}
|
||||
|
||||
return $this->echoToFile(implode(' ', $command), $filename);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dumpFile
|
||||
* @return Process
|
||||
*/
|
||||
public function getProcess(string $dumpFile): Process
|
||||
{
|
||||
$command = $this->getDumpCommand($dumpFile);
|
||||
|
||||
return Process::fromShellCommandline($command, null, null, null, $this->timeout);
|
||||
}
|
||||
}
|
||||
368
vendor/spatie/db-dumper/src/Databases/MySql.php
vendored
Normal file
368
vendor/spatie/db-dumper/src/Databases/MySql.php
vendored
Normal file
@@ -0,0 +1,368 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DbDumper\Databases;
|
||||
|
||||
use Spatie\DbDumper\DbDumper;
|
||||
use Spatie\DbDumper\Exceptions\CannotStartDump;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class MySql extends DbDumper
|
||||
{
|
||||
/** @var bool */
|
||||
protected $skipComments = true;
|
||||
|
||||
/** @var bool */
|
||||
protected $useExtendedInserts = true;
|
||||
|
||||
/** @var bool */
|
||||
protected $useSingleTransaction = false;
|
||||
|
||||
/** @var bool */
|
||||
protected $skipLockTables = false;
|
||||
|
||||
/** @var bool */
|
||||
protected $doNotUseColumnStatistics = false;
|
||||
|
||||
/** @var bool */
|
||||
protected $useQuick = false;
|
||||
|
||||
/** @var string */
|
||||
protected $defaultCharacterSet = '';
|
||||
|
||||
/** @var bool */
|
||||
protected $dbNameWasSetAsExtraOption = false;
|
||||
|
||||
/** @var bool */
|
||||
protected $allDatabasesWasSetAsExtraOption = false;
|
||||
|
||||
/** @var string */
|
||||
protected $setGtidPurged = 'AUTO';
|
||||
|
||||
/** @var bool */
|
||||
protected $createTables = true;
|
||||
|
||||
/** @var false|resource */
|
||||
private $tempFileHandle;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->port = 3306;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function skipComments()
|
||||
{
|
||||
$this->skipComments = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function dontSkipComments()
|
||||
{
|
||||
$this->skipComments = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function useExtendedInserts()
|
||||
{
|
||||
$this->useExtendedInserts = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function dontUseExtendedInserts()
|
||||
{
|
||||
$this->useExtendedInserts = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function useSingleTransaction()
|
||||
{
|
||||
$this->useSingleTransaction = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function dontUseSingleTransaction()
|
||||
{
|
||||
$this->useSingleTransaction = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function skipLockTables()
|
||||
{
|
||||
$this->skipLockTables = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function doNotUseColumnStatistics()
|
||||
{
|
||||
$this->doNotUseColumnStatistics = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function dontSkipLockTables()
|
||||
{
|
||||
$this->skipLockTables = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function useQuick()
|
||||
{
|
||||
$this->useQuick = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function dontUseQuick()
|
||||
{
|
||||
$this->useQuick = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $characterSet
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDefaultCharacterSet(string $characterSet)
|
||||
{
|
||||
$this->defaultCharacterSet = $characterSet;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function setGtidPurged(string $setGtidPurged)
|
||||
{
|
||||
$this->setGtidPurged = $setGtidPurged;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump the contents of the database to the given file.
|
||||
*
|
||||
* @param string $dumpFile
|
||||
*
|
||||
* @throws \Spatie\DbDumper\Exceptions\CannotStartDump
|
||||
* @throws \Spatie\DbDumper\Exceptions\DumpFailed
|
||||
*/
|
||||
public function dumpToFile(string $dumpFile)
|
||||
{
|
||||
$this->guardAgainstIncompleteCredentials();
|
||||
|
||||
$tempFileHandle = tmpfile();
|
||||
$this->setTempFileHandle($tempFileHandle);
|
||||
|
||||
$process = $this->getProcess($dumpFile);
|
||||
|
||||
$process->run();
|
||||
|
||||
$this->checkIfDumpWasSuccessFul($process, $dumpFile);
|
||||
}
|
||||
|
||||
public function addExtraOption(string $extraOption)
|
||||
{
|
||||
if (strpos($extraOption, '--all-databases') !== false) {
|
||||
$this->dbNameWasSetAsExtraOption = true;
|
||||
$this->allDatabasesWasSetAsExtraOption = true;
|
||||
}
|
||||
|
||||
if (preg_match('/^--databases (\S+)/', $extraOption, $matches) === 1) {
|
||||
$this->setDbName($matches[1]);
|
||||
$this->dbNameWasSetAsExtraOption = true;
|
||||
}
|
||||
|
||||
return parent::addExtraOption($extraOption);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function doNotCreateTables()
|
||||
{
|
||||
$this->createTables = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command that should be performed to dump the database.
|
||||
*
|
||||
* @param string $dumpFile
|
||||
* @param string $temporaryCredentialsFile
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDumpCommand(string $dumpFile, string $temporaryCredentialsFile): string
|
||||
{
|
||||
$quote = $this->determineQuote();
|
||||
|
||||
$command = [
|
||||
"{$quote}{$this->dumpBinaryPath}mysqldump{$quote}",
|
||||
"--defaults-extra-file=\"{$temporaryCredentialsFile}\"",
|
||||
];
|
||||
|
||||
if (! $this->createTables) {
|
||||
$command[] = '--no-create-info';
|
||||
}
|
||||
|
||||
if ($this->skipComments) {
|
||||
$command[] = '--skip-comments';
|
||||
}
|
||||
|
||||
$command[] = $this->useExtendedInserts ? '--extended-insert' : '--skip-extended-insert';
|
||||
|
||||
if ($this->useSingleTransaction) {
|
||||
$command[] = '--single-transaction';
|
||||
}
|
||||
|
||||
if ($this->skipLockTables) {
|
||||
$command[] = '--skip-lock-tables';
|
||||
}
|
||||
|
||||
if ($this->doNotUseColumnStatistics) {
|
||||
$command[] = '--column-statistics=0';
|
||||
}
|
||||
|
||||
if ($this->useQuick) {
|
||||
$command[] = '--quick';
|
||||
}
|
||||
|
||||
if ($this->socket !== '') {
|
||||
$command[] = "--socket={$this->socket}";
|
||||
}
|
||||
|
||||
foreach ($this->excludeTables as $tableName) {
|
||||
$command[] = "--ignore-table={$this->dbName}.{$tableName}";
|
||||
}
|
||||
|
||||
if (! empty($this->defaultCharacterSet)) {
|
||||
$command[] = '--default-character-set='.$this->defaultCharacterSet;
|
||||
}
|
||||
|
||||
foreach ($this->extraOptions as $extraOption) {
|
||||
$command[] = $extraOption;
|
||||
}
|
||||
|
||||
if ($this->setGtidPurged !== 'AUTO') {
|
||||
$command[] = '--set-gtid-purged='.$this->setGtidPurged;
|
||||
}
|
||||
|
||||
if (! $this->dbNameWasSetAsExtraOption) {
|
||||
$command[] = $this->dbName;
|
||||
}
|
||||
|
||||
if (! empty($this->includeTables)) {
|
||||
$includeTables = implode(' ', $this->includeTables);
|
||||
$command[] = "--tables {$includeTables}";
|
||||
}
|
||||
|
||||
foreach ($this->extraOptionsAfterDbName as $extraOptionAfterDbName) {
|
||||
$command[] = $extraOptionAfterDbName;
|
||||
}
|
||||
|
||||
return $this->echoToFile(implode(' ', $command), $dumpFile);
|
||||
}
|
||||
|
||||
public function getContentsOfCredentialsFile(): string
|
||||
{
|
||||
$contents = [
|
||||
'[client]',
|
||||
"user = '{$this->userName}'",
|
||||
"password = '{$this->password}'",
|
||||
"port = '{$this->port}'",
|
||||
];
|
||||
|
||||
if ($this->socket === '') {
|
||||
$contents[] = "host = '{$this->host}'";
|
||||
}
|
||||
|
||||
return implode(PHP_EOL, $contents);
|
||||
}
|
||||
|
||||
public function guardAgainstIncompleteCredentials()
|
||||
{
|
||||
foreach (['userName', 'host'] as $requiredProperty) {
|
||||
if (strlen($this->$requiredProperty) === 0) {
|
||||
throw CannotStartDump::emptyParameter($requiredProperty);
|
||||
}
|
||||
}
|
||||
|
||||
if (strlen($this->dbName) === 0 && ! $this->allDatabasesWasSetAsExtraOption) {
|
||||
throw CannotStartDump::emptyParameter('dbName');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dumpFile
|
||||
* @return Process
|
||||
*/
|
||||
public function getProcess(string $dumpFile): Process
|
||||
{
|
||||
fwrite($this->getTempFileHandle(), $this->getContentsOfCredentialsFile());
|
||||
$temporaryCredentialsFile = stream_get_meta_data($this->getTempFileHandle())['uri'];
|
||||
|
||||
$command = $this->getDumpCommand($dumpFile, $temporaryCredentialsFile);
|
||||
|
||||
return Process::fromShellCommandline($command, null, null, null, $this->timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|resource
|
||||
*/
|
||||
public function getTempFileHandle()
|
||||
{
|
||||
return $this->tempFileHandle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param false|resource $tempFileHandle
|
||||
*/
|
||||
public function setTempFileHandle($tempFileHandle)
|
||||
{
|
||||
$this->tempFileHandle = $tempFileHandle;
|
||||
}
|
||||
}
|
||||
169
vendor/spatie/db-dumper/src/Databases/PostgreSql.php
vendored
Normal file
169
vendor/spatie/db-dumper/src/Databases/PostgreSql.php
vendored
Normal file
@@ -0,0 +1,169 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DbDumper\Databases;
|
||||
|
||||
use Spatie\DbDumper\DbDumper;
|
||||
use Spatie\DbDumper\Exceptions\CannotStartDump;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class PostgreSql extends DbDumper
|
||||
{
|
||||
/** @var bool */
|
||||
protected $useInserts = false;
|
||||
|
||||
/** @var bool */
|
||||
protected $createTables = true;
|
||||
|
||||
/** @var false|resource */
|
||||
private $tempFileHandle;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->port = 5432;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function useInserts()
|
||||
{
|
||||
$this->useInserts = true;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Dump the contents of the database to the given file.
|
||||
*
|
||||
* @param string $dumpFile
|
||||
*
|
||||
* @throws \Spatie\DbDumper\Exceptions\CannotStartDump
|
||||
* @throws \Spatie\DbDumper\Exceptions\DumpFailed
|
||||
*/
|
||||
public function dumpToFile(string $dumpFile)
|
||||
{
|
||||
$this->guardAgainstIncompleteCredentials();
|
||||
|
||||
$tempFileHandle = tmpfile();
|
||||
$this->setTempFileHandle($tempFileHandle);
|
||||
|
||||
$process = $this->getProcess($dumpFile);
|
||||
|
||||
$process->run();
|
||||
|
||||
$this->checkIfDumpWasSuccessFul($process, $dumpFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command that should be performed to dump the database.
|
||||
*
|
||||
* @param string $dumpFile
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDumpCommand(string $dumpFile): string
|
||||
{
|
||||
$quote = $this->determineQuote();
|
||||
|
||||
$command = [
|
||||
"{$quote}{$this->dumpBinaryPath}pg_dump{$quote}",
|
||||
"-U {$this->userName}",
|
||||
'-h '.($this->socket === '' ? $this->host : $this->socket),
|
||||
"-p {$this->port}",
|
||||
];
|
||||
|
||||
if ($this->useInserts) {
|
||||
$command[] = '--inserts';
|
||||
}
|
||||
|
||||
if (! $this->createTables) {
|
||||
$command[] = '--data-only';
|
||||
}
|
||||
|
||||
foreach ($this->extraOptions as $extraOption) {
|
||||
$command[] = $extraOption;
|
||||
}
|
||||
|
||||
if (! empty($this->includeTables)) {
|
||||
$command[] = '-t '.implode(' -t ', $this->includeTables);
|
||||
}
|
||||
|
||||
if (! empty($this->excludeTables)) {
|
||||
$command[] = '-T '.implode(' -T ', $this->excludeTables);
|
||||
}
|
||||
|
||||
return $this->echoToFile(implode(' ', $command), $dumpFile);
|
||||
}
|
||||
|
||||
public function getContentsOfCredentialsFile(): string
|
||||
{
|
||||
$contents = [
|
||||
$this->host,
|
||||
$this->port,
|
||||
$this->dbName,
|
||||
$this->userName,
|
||||
$this->password,
|
||||
];
|
||||
|
||||
return implode(':', $contents);
|
||||
}
|
||||
|
||||
public function guardAgainstIncompleteCredentials()
|
||||
{
|
||||
foreach (['userName', 'dbName', 'host'] as $requiredProperty) {
|
||||
if (empty($this->$requiredProperty)) {
|
||||
throw CannotStartDump::emptyParameter($requiredProperty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected function getEnvironmentVariablesForDumpCommand(string $temporaryCredentialsFile): array
|
||||
{
|
||||
return [
|
||||
'PGPASSFILE' => $temporaryCredentialsFile,
|
||||
'PGDATABASE' => $this->dbName,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @return $this
|
||||
*/
|
||||
public function doNotCreateTables()
|
||||
{
|
||||
$this->createTables = false;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dumpFile
|
||||
* @return Process
|
||||
*/
|
||||
public function getProcess(string $dumpFile): Process
|
||||
{
|
||||
$command = $this->getDumpCommand($dumpFile);
|
||||
|
||||
fwrite($this->getTempFileHandle(), $this->getContentsOfCredentialsFile());
|
||||
$temporaryCredentialsFile = stream_get_meta_data($this->getTempFileHandle())['uri'];
|
||||
|
||||
$envVars = $this->getEnvironmentVariablesForDumpCommand($temporaryCredentialsFile);
|
||||
|
||||
return Process::fromShellCommandline($command, null, $envVars, null, $this->timeout);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return false|resource
|
||||
*/
|
||||
public function getTempFileHandle()
|
||||
{
|
||||
return $this->tempFileHandle;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param false|resource $tempFileHandle
|
||||
*/
|
||||
public function setTempFileHandle($tempFileHandle)
|
||||
{
|
||||
$this->tempFileHandle = $tempFileHandle;
|
||||
}
|
||||
}
|
||||
60
vendor/spatie/db-dumper/src/Databases/Sqlite.php
vendored
Normal file
60
vendor/spatie/db-dumper/src/Databases/Sqlite.php
vendored
Normal file
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DbDumper\Databases;
|
||||
|
||||
use Spatie\DbDumper\DbDumper;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class Sqlite extends DbDumper
|
||||
{
|
||||
/**
|
||||
* Dump the contents of the database to a given file.
|
||||
*
|
||||
* @param string $dumpFile
|
||||
*
|
||||
* @throws \Spatie\DbDumper\Exceptions\DumpFailed
|
||||
*/
|
||||
public function dumpToFile(string $dumpFile)
|
||||
{
|
||||
$process = $this->getProcess($dumpFile);
|
||||
|
||||
$process->run();
|
||||
|
||||
$this->checkIfDumpWasSuccessFul($process, $dumpFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the command that should be performed to dump the database.
|
||||
*
|
||||
* @param string $dumpFile
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getDumpCommand(string $dumpFile): string
|
||||
{
|
||||
$dumpInSqlite = "echo 'BEGIN IMMEDIATE;\n.dump'";
|
||||
if ($this->isWindows()) {
|
||||
$dumpInSqlite = '(echo BEGIN IMMEDIATE; & echo .dump)';
|
||||
}
|
||||
$quote = $this->determineQuote();
|
||||
|
||||
$command = sprintf(
|
||||
"{$dumpInSqlite} | {$quote}%ssqlite3{$quote} --bail {$quote}%s{$quote}",
|
||||
$this->dumpBinaryPath,
|
||||
$this->dbName
|
||||
);
|
||||
|
||||
return $this->echoToFile($command, $dumpFile);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dumpFile
|
||||
* @return Process
|
||||
*/
|
||||
public function getProcess(string $dumpFile): Process
|
||||
{
|
||||
$command = $this->getDumpCommand($dumpFile);
|
||||
|
||||
return Process::fromShellCommandline($command, null, null, null, $this->timeout);
|
||||
}
|
||||
}
|
||||
306
vendor/spatie/db-dumper/src/DbDumper.php
vendored
Normal file
306
vendor/spatie/db-dumper/src/DbDumper.php
vendored
Normal file
@@ -0,0 +1,306 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DbDumper;
|
||||
|
||||
use Spatie\DbDumper\Compressors\Compressor;
|
||||
use Spatie\DbDumper\Compressors\GzipCompressor;
|
||||
use Spatie\DbDumper\Exceptions\CannotSetParameter;
|
||||
use Spatie\DbDumper\Exceptions\DumpFailed;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
abstract class DbDumper
|
||||
{
|
||||
/** @var string */
|
||||
protected $dbName;
|
||||
|
||||
/** @var string */
|
||||
protected $userName;
|
||||
|
||||
/** @var string */
|
||||
protected $password;
|
||||
|
||||
/** @var string */
|
||||
protected $host = 'localhost';
|
||||
|
||||
/** @var int */
|
||||
protected $port = 5432;
|
||||
|
||||
/** @var string */
|
||||
protected $socket = '';
|
||||
|
||||
/** @var int */
|
||||
protected $timeout = 0;
|
||||
|
||||
/** @var string */
|
||||
protected $dumpBinaryPath = '';
|
||||
|
||||
/** @var array */
|
||||
protected $includeTables = [];
|
||||
|
||||
/** @var array */
|
||||
protected $excludeTables = [];
|
||||
|
||||
/** @var array */
|
||||
protected $extraOptions = [];
|
||||
|
||||
/** @var array */
|
||||
protected $extraOptionsAfterDbName = [];
|
||||
|
||||
/** @var object */
|
||||
protected $compressor = null;
|
||||
|
||||
public static function create()
|
||||
{
|
||||
return new static();
|
||||
}
|
||||
|
||||
public function getDbName(): string
|
||||
{
|
||||
return $this->dbName;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $dbName
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setDbName(string $dbName)
|
||||
{
|
||||
$this->dbName = $dbName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $userName
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setUserName(string $userName)
|
||||
{
|
||||
$this->userName = $userName;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $password
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setPassword(string $password)
|
||||
{
|
||||
$this->password = $password;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $host
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setHost(string $host)
|
||||
{
|
||||
$this->host = $host;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getHost(): string
|
||||
{
|
||||
return $this->host;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $port
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setPort(int $port)
|
||||
{
|
||||
$this->port = $port;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $socket
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setSocket(string $socket)
|
||||
{
|
||||
$this->socket = $socket;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $timeout
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function setTimeout(int $timeout)
|
||||
{
|
||||
$this->timeout = $timeout;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function setDumpBinaryPath(string $dumpBinaryPath)
|
||||
{
|
||||
if ($dumpBinaryPath !== '' && substr($dumpBinaryPath, -1) !== '/') {
|
||||
$dumpBinaryPath .= '/';
|
||||
}
|
||||
|
||||
$this->dumpBinaryPath = $dumpBinaryPath;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function enableCompression()
|
||||
{
|
||||
$this->compressor = new GzipCompressor();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function getCompressorExtension(): string
|
||||
{
|
||||
return $this->compressor->useExtension();
|
||||
}
|
||||
|
||||
public function useCompressor(Compressor $compressor)
|
||||
{
|
||||
$this->compressor = $compressor;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $includeTables
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws \Spatie\DbDumper\Exceptions\CannotSetParameter
|
||||
*/
|
||||
public function includeTables($includeTables)
|
||||
{
|
||||
if (! empty($this->excludeTables)) {
|
||||
throw CannotSetParameter::conflictingParameters('includeTables', 'excludeTables');
|
||||
}
|
||||
|
||||
if (! is_array($includeTables)) {
|
||||
$includeTables = explode(', ', $includeTables);
|
||||
}
|
||||
|
||||
$this->includeTables = $includeTables;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string|array $excludeTables
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws \Spatie\DbDumper\Exceptions\CannotSetParameter
|
||||
*/
|
||||
public function excludeTables($excludeTables)
|
||||
{
|
||||
if (! empty($this->includeTables)) {
|
||||
throw CannotSetParameter::conflictingParameters('excludeTables', 'includeTables');
|
||||
}
|
||||
|
||||
if (! is_array($excludeTables)) {
|
||||
$excludeTables = explode(', ', $excludeTables);
|
||||
}
|
||||
|
||||
$this->excludeTables = $excludeTables;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $extraOption
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addExtraOption(string $extraOption)
|
||||
{
|
||||
if (! empty($extraOption)) {
|
||||
$this->extraOptions[] = $extraOption;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $extraOptionAtEnd
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addExtraOptionAfterDbName(string $extraOptionAfterDbName)
|
||||
{
|
||||
if (! empty($extraOptionAfterDbName)) {
|
||||
$this->extraOptionsAfterDbName[] = $extraOptionAfterDbName;
|
||||
}
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
abstract public function dumpToFile(string $dumpFile);
|
||||
|
||||
public function checkIfDumpWasSuccessFul(Process $process, string $outputFile)
|
||||
{
|
||||
if (! $process->isSuccessful()) {
|
||||
throw DumpFailed::processDidNotEndSuccessfully($process);
|
||||
}
|
||||
|
||||
if (! file_exists($outputFile)) {
|
||||
throw DumpFailed::dumpfileWasNotCreated();
|
||||
}
|
||||
|
||||
if (filesize($outputFile) === 0) {
|
||||
throw DumpFailed::dumpfileWasEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
protected function getCompressCommand(string $command, string $dumpFile): string
|
||||
{
|
||||
$compressCommand = $this->compressor->useCommand();
|
||||
|
||||
if ($this->isWindows()) {
|
||||
return "{$command} | {$compressCommand} > {$dumpFile}";
|
||||
}
|
||||
|
||||
return "(((({$command}; echo \$? >&3) | {$compressCommand} > {$dumpFile}) 3>&1) | (read x; exit \$x))";
|
||||
}
|
||||
|
||||
protected function echoToFile(string $command, string $dumpFile): string
|
||||
{
|
||||
$dumpFile = '"'.addcslashes($dumpFile, '\\"').'"';
|
||||
|
||||
if ($this->compressor) {
|
||||
return $this->getCompressCommand($command, $dumpFile);
|
||||
}
|
||||
|
||||
return $command.' > '.$dumpFile;
|
||||
}
|
||||
|
||||
protected function determineQuote(): string
|
||||
{
|
||||
return $this->isWindows() ? '"' : "'";
|
||||
}
|
||||
|
||||
protected function isWindows(): bool
|
||||
{
|
||||
return strtoupper(substr(PHP_OS, 0, 3)) === 'WIN';
|
||||
}
|
||||
}
|
||||
19
vendor/spatie/db-dumper/src/Exceptions/CannotSetParameter.php
vendored
Normal file
19
vendor/spatie/db-dumper/src/Exceptions/CannotSetParameter.php
vendored
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DbDumper\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class CannotSetParameter extends Exception
|
||||
{
|
||||
/**
|
||||
* @param string $name
|
||||
* @param string $conflictName
|
||||
*
|
||||
* @return \Spatie\DbDumper\Exceptions\CannotSetParameter
|
||||
*/
|
||||
public static function conflictingParameters($name, $conflictName)
|
||||
{
|
||||
return new static("Cannot set `{$name}` because it conflicts with parameter `{$conflictName}`.");
|
||||
}
|
||||
}
|
||||
18
vendor/spatie/db-dumper/src/Exceptions/CannotStartDump.php
vendored
Normal file
18
vendor/spatie/db-dumper/src/Exceptions/CannotStartDump.php
vendored
Normal file
@@ -0,0 +1,18 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DbDumper\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class CannotStartDump extends Exception
|
||||
{
|
||||
/**
|
||||
* @param string $name
|
||||
*
|
||||
* @return \Spatie\DbDumper\Exceptions\CannotStartDump
|
||||
*/
|
||||
public static function emptyParameter($name)
|
||||
{
|
||||
return new static("Parameter `{$name}` cannot be empty.");
|
||||
}
|
||||
}
|
||||
35
vendor/spatie/db-dumper/src/Exceptions/DumpFailed.php
vendored
Normal file
35
vendor/spatie/db-dumper/src/Exceptions/DumpFailed.php
vendored
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
|
||||
namespace Spatie\DbDumper\Exceptions;
|
||||
|
||||
use Exception;
|
||||
use Symfony\Component\Process\Process;
|
||||
|
||||
class DumpFailed extends Exception
|
||||
{
|
||||
/**
|
||||
* @param \Symfony\Component\Process\Process $process
|
||||
*
|
||||
* @return \Spatie\DbDumper\Exceptions\DumpFailed
|
||||
*/
|
||||
public static function processDidNotEndSuccessfully(Process $process)
|
||||
{
|
||||
return new static("The dump process failed with exitcode {$process->getExitCode()} : {$process->getExitCodeText()} : {$process->getErrorOutput()}");
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Spatie\DbDumper\Exceptions\DumpFailed
|
||||
*/
|
||||
public static function dumpfileWasNotCreated()
|
||||
{
|
||||
return new static('The dumpfile could not be created');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return \Spatie\DbDumper\Exceptions\DumpFailed
|
||||
*/
|
||||
public static function dumpfileWasEmpty()
|
||||
{
|
||||
return new static('The created dumpfile is empty');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user