Skip to content

Commit

Permalink
Merge pull request #1 from druidfi/code-refactor
Browse files Browse the repository at this point in the history
Code refactor
  • Loading branch information
back-2-95 authored Aug 19, 2022
2 parents 0054388 + c807aef commit 6c1b79c
Show file tree
Hide file tree
Showing 24 changed files with 1,503 additions and 1,537 deletions.
15 changes: 15 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ jobs:

strategy:
matrix:
#mysql-versions: ['5.7', '8.0']
mysql-versions: ['5.7']
php-versions: ['7.4', '8.0', '8.1']

services:
db:
image: druidfi/mysql:${{ matrix.mysql-versions }}-drupal
ports:
- 3306:3306
options: --health-cmd="mysqladmin ping" --health-interval=10s --health-timeout=5s --health-retries=3

steps:

- name: Checkout code
Expand All @@ -38,3 +47,9 @@ jobs:

- name: Run PHPunit tests
run: vendor/bin/phpunit

# - name: Create user and databases for testing
# run: ./tests/scripts/create_users.sh 127.0.0.1
#
# - name: Run test script
# run: ./tests/scripts/test.sh 127.0.0.1
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/.settings
# Vim swap files
.*.sw*

**/*.checksum
/composer.lock
/composer.phar
/vendor/
Expand Down
22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

11 changes: 11 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
FROM php:8.1-alpine

RUN apk --update add --no-cache \
bash mysql-client \
&& rm -rf /var/cache/apk/*

RUN docker-php-ext-install mysqli pdo pdo_mysql

WORKDIR /app

CMD ["tail", "-f", "/dev/null"]
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,14 @@ Some tests are skipped if mysql server doesn't support them.
A couple of tests are only comparing between original sql code and mysqldump-php generated sql, because some options
are not available in mysqldump.

Local setup for tests:

```
docker-compose up -d --build
docker-compose exec php /app/tests/scripts/create_users.sh
docker-compose exec -w /app/tests/scripts php ./test.sh
```

## Bugs (from mysqldump, not from mysqldump-php)

After [this](https://bugs.mysql.com/bug.php?id=80150) bug report, a new one has been introduced. _binary is appended
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
],
"require": {
"php": "^7.4 || ^8.0",
"composer-runtime-api": "^2"
"composer-runtime-api": "^2",
"ext-pdo": "*"
},
"require-dev": {
"squizlabs/php_codesniffer": "3.*",
Expand Down
19 changes: 19 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
services:

db:
container_name: "mysqldump-php-db"
image: druidfi/mysql:5.7-drupal

db2:
container_name: "mysqldump-php-db2"
image: druidfi/mysql:8.0-drupal

php:
container_name: "mysqldump-php"
image: tester:latest
build:
context: .
volumes:
- .:/app
depends_on:
- db
45 changes: 45 additions & 0 deletions src/Ifsnop/Mysqldump/Compress/CompressBzip2.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace Ifsnop\Mysqldump\Compress;

class CompressBzip2 implements CompressInterface
{
private $fileHandler;

public function __construct()
{
if (!function_exists("bzopen")) {
throw new Exception("Compression is enabled, but bzip2 lib is not installed or configured properly");
}
}

/**
* @param string $filename
*/
public function open($filename)
{
$this->fileHandler = bzopen($filename, "w");

if (false === $this->fileHandler) {
throw new Exception("Output file is not writable");
}

return true;
}

public function write(string $str): int
{
$bytesWritten = bzwrite($this->fileHandler, $str);

if (false === $bytesWritten) {
throw new Exception("Writting to file failed! Probably, there is no more free space left?");
}

return $bytesWritten;
}

public function close(): bool
{
return bzclose($this->fileHandler);
}
}
44 changes: 44 additions & 0 deletions src/Ifsnop/Mysqldump/Compress/CompressGzip.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace Ifsnop\Mysqldump\Compress;

use Exception;

class CompressGzip implements CompressInterface
{
private $fileHandler;

public function __construct()
{
if (!function_exists("gzopen")) {
throw new Exception("Compression is enabled, but gzip lib is not installed or configured properly");
}
}

public function open(string $filename)
{
$this->fileHandler = gzopen($filename, "wb");

if (false === $this->fileHandler) {
throw new Exception("Output file is not writable");
}

return true;
}

public function write(string $str): int
{
$bytesWritten = gzwrite($this->fileHandler, $str);

if (false === $bytesWritten) {
throw new Exception("Writing to file failed! Probably, there is no more free space left?");
}

return $bytesWritten;
}

public function close(): bool
{
return gzclose($this->fileHandler);
}
}
42 changes: 42 additions & 0 deletions src/Ifsnop/Mysqldump/Compress/CompressGzipstream.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace Ifsnop\Mysqldump\Compress;

use Exception;

class CompressGzipstream implements CompressInterface
{
private $fileHandler;
private $compressContext;

public function open($filename)
{
$this->fileHandler = fopen($filename, "wb");

if (false === $this->fileHandler) {
throw new Exception("Output file is not writable");
}

$this->compressContext = deflate_init(ZLIB_ENCODING_GZIP, ['level' => 9]);

return true;
}

public function write(string $str): int
{
$bytesWritten = fwrite($this->fileHandler, deflate_add($this->compressContext, $str, ZLIB_NO_FLUSH));

if (false === $bytesWritten) {
throw new Exception("Writing to file failed! Probably, there is no more free space left?");
}

return $bytesWritten;
}

public function close(): bool
{
fwrite($this->fileHandler, deflate_add($this->compressContext, '', ZLIB_FINISH));

return fclose($this->fileHandler);
}
}
12 changes: 12 additions & 0 deletions src/Ifsnop/Mysqldump/Compress/CompressInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace Ifsnop\Mysqldump\Compress;

interface CompressInterface
{
public function open(string $filename);

public function write(string $str): int;

public function close(): bool;
}
21 changes: 21 additions & 0 deletions src/Ifsnop/Mysqldump/Compress/CompressManagerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace Ifsnop\Mysqldump\Compress;

use Exception;

abstract class CompressManagerFactory
{
public static function create(string $c): CompressInterface
{
$c = ucfirst(strtolower($c));

if (!CompressMethod::isValid($c)) {
throw new Exception("Compression method ($c) is not defined yet");
}

$methodClass = __NAMESPACE__."\\"."Compress".$c;

return new $methodClass;
}
}
27 changes: 27 additions & 0 deletions src/Ifsnop/Mysqldump/Compress/CompressMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Ifsnop\Mysqldump\Compress;

use Ifsnop\Mysqldump\Mysqldump;

/**
* Enum with all available compression methods.
*/
abstract class CompressMethod
{
public static array $enums = [
Mysqldump::NONE,
Mysqldump::GZIP,
Mysqldump::BZIP2,
Mysqldump::GZIPSTREAM,
];

/**
* @param string $c
* @return boolean
*/
public static function isValid(string $c): bool
{
return in_array($c, self::$enums);
}
}
37 changes: 37 additions & 0 deletions src/Ifsnop/Mysqldump/Compress/CompressNone.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Ifsnop\Mysqldump\Compress;

use Exception;

class CompressNone implements CompressInterface
{
private $fileHandler;

public function open(string $filename)
{
$this->fileHandler = fopen($filename, "wb");

if (false === $this->fileHandler) {
throw new Exception("Output file is not writable");
}

return true;
}

public function write(string $str): int
{
$bytesWritten = fwrite($this->fileHandler, $str);

if (false === $bytesWritten) {
throw new Exception("Writing to file failed! Probably, there is no more free space left?");
}

return $bytesWritten;
}

public function close(): bool
{
return fclose($this->fileHandler);
}
}
Loading

0 comments on commit 6c1b79c

Please sign in to comment.