Passed
Push — devel-3.0 ( f3e30e...950ad4 )
by Rubén
03:36
created

MySQLFileParser::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * sysPass
4
 *
5
 * @author    nuxsmin
6
 * @link      https://syspass.org
7
 * @copyright 2012-2018, Rubén Domínguez nuxsmin@$syspass.org
8
 *
9
 * This file is part of sysPass.
10
 *
11
 * sysPass is free software: you can redistribute it and/or modify
12
 * it under the terms of the GNU General Public License as published by
13
 * the Free Software Foundation, either version 3 of the License, or
14
 * (at your option) any later version.
15
 *
16
 * sysPass is distributed in the hope that it will be useful,
17
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19
 * GNU General Public License for more details.
20
 *
21
 * You should have received a copy of the GNU General Public License
22
 *  along with sysPass.  If not, see <http://www.gnu.org/licenses/>.
23
 */
24
25
namespace SP\Storage\Database;
26
27
use SP\Storage\File\FileException;
28
use SP\Storage\File\FileHandler;
29
30
/**
31
 * Class MysqlFileParser
32
 *
33
 * @package SP\Storage
34
 */
35
final class MySQLFileParser implements DatabaseFileInterface
36
{
37
    /**
38
     * @var FileHandler
39
     */
40
    private $fileHandler;
41
42
    /**
43
     * MySQLFileParser constructor.
44
     *
45
     * @param FileHandler $fileHandler
46
     */
47
    public function __construct(FileHandler $fileHandler)
48
    {
49
        $this->fileHandler = $fileHandler;
50
    }
51
52
    /**
53
     * Parses a database script file and returns an array of lines parsed
54
     *
55
     * @param string $delimiter
56
     *
57
     * @return array
58
     * @throws FileException
59
     */
60
    public function parse($delimiter = ';')
61
    {
62
        $queries = [];
63
        $query = '';
64
        $delimiterLength = strlen($delimiter);
65
66
        $this->fileHandler->checkIsReadable();
67
68
        $handle = $this->fileHandler->open('rb');
69
70
        while (($buffer = fgets($handle)) !== false) {
71
            $buffer = trim($buffer);
72
            $length = strlen($buffer);
73
74
            if ($length > 0
75
                && strpos($buffer, '--') !== 0
76
            ) {
77
                // CHecks if delimiter based EOL is reached
78
                $end = strrpos($buffer, $delimiter) === $length - $delimiterLength;
79
80
                // Checks if line is an SQL statement wrapped by a comment
81
                if (preg_match('#^(?<stmt>/\*!\d+.*\*/)#', $buffer, $matches)) {
82
                    if (!$end) {
83
                        $query .= $matches['stmt'] . PHP_EOL;
84
                    } else {
85
                        $queries[] = $query . $matches['stmt'];
86
87
                        $query = '';
88
                    }
89
                } else {
90
                    if (!$end) {
91
                        $query .= $buffer . PHP_EOL;
92
                    } elseif ($end && strpos($buffer, 'DELIMITER') === false) {
93
                        $queries[] = $query . trim(substr_replace($buffer, '', $length - $delimiterLength), $delimiterLength);
94
95
                        $query = '';
96
                    }
97
                }
98
            }
99
        }
100
101
        $this->fileHandler->close();
102
103
        return $queries;
104
    }
105
}