@@ -7,23 +7,23 @@ |
||
7 | 7 | |
8 | 8 | class CsvTableReaderTest extends TestCase |
9 | 9 | { |
10 | - public function testCsvReading() |
|
11 | - { |
|
10 | + public function testCsvReading() |
|
11 | + { |
|
12 | 12 | |
13 | - $csv = new CsvTableReader(__DIR__ . '/fixture/input.csv'); |
|
14 | - $this->assertEquals(['Col1', 'Col2', 'Col3'], $csv->getColumns()); |
|
13 | + $csv = new CsvTableReader(__DIR__ . '/fixture/input.csv'); |
|
14 | + $this->assertEquals(['Col1', 'Col2', 'Col3'], $csv->getColumns()); |
|
15 | 15 | |
16 | - $extractedData = []; |
|
17 | - foreach ($csv as $record) { |
|
18 | - $extractedData[] = $record; |
|
19 | - } |
|
16 | + $extractedData = []; |
|
17 | + foreach ($csv as $record) { |
|
18 | + $extractedData[] = $record; |
|
19 | + } |
|
20 | 20 | |
21 | - $this->assertEquals( |
|
22 | - [ |
|
23 | - [ 'Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three' ], |
|
24 | - [ 'Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?" ] |
|
25 | - ], |
|
26 | - $extractedData |
|
27 | - ); |
|
28 | - } |
|
21 | + $this->assertEquals( |
|
22 | + [ |
|
23 | + [ 'Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three' ], |
|
24 | + [ 'Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?" ] |
|
25 | + ], |
|
26 | + $extractedData |
|
27 | + ); |
|
28 | + } |
|
29 | 29 | } |
@@ -10,7 +10,7 @@ discard block |
||
10 | 10 | public function testCsvReading() |
11 | 11 | { |
12 | 12 | |
13 | - $csv = new CsvTableReader(__DIR__ . '/fixture/input.csv'); |
|
13 | + $csv = new CsvTableReader(__DIR__.'/fixture/input.csv'); |
|
14 | 14 | $this->assertEquals(['Col1', 'Col2', 'Col3'], $csv->getColumns()); |
15 | 15 | |
16 | 16 | $extractedData = []; |
@@ -20,8 +20,8 @@ discard block |
||
20 | 20 | |
21 | 21 | $this->assertEquals( |
22 | 22 | [ |
23 | - [ 'Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three' ], |
|
24 | - [ 'Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?" ] |
|
23 | + ['Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three'], |
|
24 | + ['Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?"] |
|
25 | 25 | ], |
26 | 26 | $extractedData |
27 | 27 | ); |
@@ -7,45 +7,45 @@ |
||
7 | 7 | |
8 | 8 | class CsvTableWriterTest extends TestCase |
9 | 9 | { |
10 | - public function testCsvReading() |
|
11 | - { |
|
12 | - if (file_exists('/tmp/output.csv')) { |
|
13 | - unlink('/tmp/output.csv'); |
|
14 | - } |
|
10 | + public function testCsvReading() |
|
11 | + { |
|
12 | + if (file_exists('/tmp/output.csv')) { |
|
13 | + unlink('/tmp/output.csv'); |
|
14 | + } |
|
15 | 15 | |
16 | - $csv = new CsvTableWriter('/tmp/output.csv'); |
|
16 | + $csv = new CsvTableWriter('/tmp/output.csv'); |
|
17 | 17 | |
18 | - $csv->start(['Col1', 'Col2', 'Col3']); |
|
19 | - $csv->writeRecord([ 'Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three' ]); |
|
20 | - $csv->writeRecord([ 'Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?" ]); |
|
21 | - $csv->finish(); |
|
18 | + $csv->start(['Col1', 'Col2', 'Col3']); |
|
19 | + $csv->writeRecord([ 'Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three' ]); |
|
20 | + $csv->writeRecord([ 'Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?" ]); |
|
21 | + $csv->finish(); |
|
22 | 22 | |
23 | - $csvContent = file_get_contents('/tmp/output.csv'); |
|
24 | - unlink('/tmp/output.csv'); |
|
23 | + $csvContent = file_get_contents('/tmp/output.csv'); |
|
24 | + unlink('/tmp/output.csv'); |
|
25 | 25 | |
26 | - $fixture = file_get_contents(__DIR__ . '/fixture/input.csv'); |
|
26 | + $fixture = file_get_contents(__DIR__ . '/fixture/input.csv'); |
|
27 | 27 | |
28 | - $this->assertEquals($fixture, $csvContent); |
|
29 | - } |
|
28 | + $this->assertEquals($fixture, $csvContent); |
|
29 | + } |
|
30 | 30 | |
31 | - public function testNoStartCall() |
|
32 | - { |
|
31 | + public function testNoStartCall() |
|
32 | + { |
|
33 | 33 | |
34 | - if (file_exists('/tmp/output.csv')) { |
|
35 | - unlink('/tmp/output.csv'); |
|
36 | - } |
|
34 | + if (file_exists('/tmp/output.csv')) { |
|
35 | + unlink('/tmp/output.csv'); |
|
36 | + } |
|
37 | 37 | |
38 | - $csv = new CsvTableWriter('/tmp/output.csv'); |
|
38 | + $csv = new CsvTableWriter('/tmp/output.csv'); |
|
39 | 39 | |
40 | - $csv->writeRecord([ 'Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three' ]); |
|
41 | - $csv->writeRecord([ 'Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?" ]); |
|
42 | - $csv->finish(); |
|
40 | + $csv->writeRecord([ 'Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three' ]); |
|
41 | + $csv->writeRecord([ 'Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?" ]); |
|
42 | + $csv->finish(); |
|
43 | 43 | |
44 | - $csvContent = file_get_contents('/tmp/output.csv'); |
|
45 | - unlink('/tmp/output.csv'); |
|
44 | + $csvContent = file_get_contents('/tmp/output.csv'); |
|
45 | + unlink('/tmp/output.csv'); |
|
46 | 46 | |
47 | - $fixture = file_get_contents(__DIR__ . '/fixture/input.csv'); |
|
47 | + $fixture = file_get_contents(__DIR__ . '/fixture/input.csv'); |
|
48 | 48 | |
49 | - $this->assertEquals($fixture, $csvContent); |
|
50 | - } |
|
49 | + $this->assertEquals($fixture, $csvContent); |
|
50 | + } |
|
51 | 51 | } |
@@ -16,14 +16,14 @@ discard block |
||
16 | 16 | $csv = new CsvTableWriter('/tmp/output.csv'); |
17 | 17 | |
18 | 18 | $csv->start(['Col1', 'Col2', 'Col3']); |
19 | - $csv->writeRecord([ 'Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three' ]); |
|
20 | - $csv->writeRecord([ 'Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?" ]); |
|
19 | + $csv->writeRecord(['Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three']); |
|
20 | + $csv->writeRecord(['Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?"]); |
|
21 | 21 | $csv->finish(); |
22 | 22 | |
23 | 23 | $csvContent = file_get_contents('/tmp/output.csv'); |
24 | 24 | unlink('/tmp/output.csv'); |
25 | 25 | |
26 | - $fixture = file_get_contents(__DIR__ . '/fixture/input.csv'); |
|
26 | + $fixture = file_get_contents(__DIR__.'/fixture/input.csv'); |
|
27 | 27 | |
28 | 28 | $this->assertEquals($fixture, $csvContent); |
29 | 29 | } |
@@ -37,14 +37,14 @@ discard block |
||
37 | 37 | |
38 | 38 | $csv = new CsvTableWriter('/tmp/output.csv'); |
39 | 39 | |
40 | - $csv->writeRecord([ 'Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three' ]); |
|
41 | - $csv->writeRecord([ 'Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?" ]); |
|
40 | + $csv->writeRecord(['Col1' => 'One', 'Col2' => 2, 'Col3' => 'Three']); |
|
41 | + $csv->writeRecord(['Col1' => 'Hello, Sam', 'Col2' => 5, 'Col3' => "Nice to meet you\nWhat is your name?"]); |
|
42 | 42 | $csv->finish(); |
43 | 43 | |
44 | 44 | $csvContent = file_get_contents('/tmp/output.csv'); |
45 | 45 | unlink('/tmp/output.csv'); |
46 | 46 | |
47 | - $fixture = file_get_contents(__DIR__ . '/fixture/input.csv'); |
|
47 | + $fixture = file_get_contents(__DIR__.'/fixture/input.csv'); |
|
48 | 48 | |
49 | 49 | $this->assertEquals($fixture, $csvContent); |
50 | 50 | } |
@@ -10,20 +10,20 @@ |
||
10 | 10 | */ |
11 | 11 | class SmokeTest extends TestCase |
12 | 12 | { |
13 | - /** |
|
14 | - * Check that the help output of the binary matches what the internal function generates |
|
15 | - */ |
|
16 | - public function testHelpOutput() |
|
17 | - { |
|
18 | - $ssPak = new SSPak(null); |
|
13 | + /** |
|
14 | + * Check that the help output of the binary matches what the internal function generates |
|
15 | + */ |
|
16 | + public function testHelpOutput() |
|
17 | + { |
|
18 | + $ssPak = new SSPak(null); |
|
19 | 19 | |
20 | - // Internal call |
|
21 | - ob_start(); |
|
22 | - $ssPak->help(array()); |
|
23 | - $helpText = ob_get_contents(); |
|
24 | - ob_end_clean(); |
|
20 | + // Internal call |
|
21 | + ob_start(); |
|
22 | + $ssPak->help(array()); |
|
23 | + $helpText = ob_get_contents(); |
|
24 | + ob_end_clean(); |
|
25 | 25 | |
26 | - // Call to binary |
|
27 | - $this->assertEquals($helpText, `build/sspak.phar help &> /dev/stdout`); |
|
28 | - } |
|
26 | + // Call to binary |
|
27 | + $this->assertEquals($helpText, `build/sspak.phar help &> /dev/stdout`); |
|
28 | + } |
|
29 | 29 | } |
@@ -9,149 +9,149 @@ |
||
9 | 9 | */ |
10 | 10 | class FilesystemEntity |
11 | 11 | { |
12 | - protected $server; |
|
13 | - protected $path; |
|
14 | - protected $executor; |
|
15 | - protected $identity = null; |
|
16 | - |
|
17 | - public function __construct($path, $executor) |
|
18 | - { |
|
19 | - $this->executor = $executor; |
|
20 | - |
|
21 | - if (strpos($path, ':') !== false) { |
|
22 | - list($this->server,$this->path) = explode(':', $path, 2); |
|
23 | - } else { |
|
24 | - $this->server = null; |
|
25 | - $this->path = $path; |
|
26 | - } |
|
27 | - } |
|
28 | - |
|
29 | - public function isLocal() |
|
30 | - { |
|
31 | - return $this->server == null; |
|
32 | - } |
|
33 | - public function getPath() |
|
34 | - { |
|
35 | - return $this->path; |
|
36 | - } |
|
37 | - public function getServer() |
|
38 | - { |
|
39 | - return $this->server; |
|
40 | - } |
|
41 | - public function setSSHItentityFile($filename) |
|
42 | - { |
|
43 | - $this->identity = $filename; |
|
44 | - } |
|
45 | - |
|
46 | - /** |
|
47 | - * Execute a command on the relevant server |
|
48 | - * @param string $command Shell command, either a fully escaped string or an array |
|
49 | - */ |
|
50 | - public function exec($command, $options = array()) |
|
51 | - { |
|
52 | - return $this->createProcess($command, $options)->exec(); |
|
53 | - } |
|
54 | - |
|
55 | - /** |
|
56 | - * Create a process for later exection |
|
57 | - * @param string $command Shell command, either a fully escaped string or an array |
|
58 | - * @return Process |
|
59 | - */ |
|
60 | - public function createProcess($command, $options = array()) |
|
61 | - { |
|
62 | - if ($this->server) { |
|
63 | - if ($this->identity && !isset($options['identity'])) { |
|
64 | - $options['identity'] = $this->identity; |
|
65 | - } |
|
66 | - return $this->executor->createRemote($this->server, $command, $options); |
|
67 | - } |
|
68 | - |
|
69 | - return $this->executor->createLocal($command, $options); |
|
70 | - } |
|
71 | - |
|
72 | - /** |
|
73 | - * Upload a file to the given destination on the server |
|
74 | - * @param string $file The file to upload |
|
75 | - * @param string $dest The remote filename/dir to upload to |
|
76 | - */ |
|
77 | - public function upload($source, $dest) |
|
78 | - { |
|
79 | - if ($this->server) { |
|
80 | - $this->executor->execLocal(array("scp", $source, "$this->server:$dest")); |
|
81 | - } else { |
|
82 | - $this->executor->execLocal(array("cp", $source, $dest)); |
|
83 | - } |
|
84 | - } |
|
85 | - |
|
86 | - /** |
|
87 | - * Create a file with the given content at the given destination on the server |
|
88 | - * @param string $content The content of the file |
|
89 | - * @param string $dest The remote filename/dir to upload to |
|
90 | - */ |
|
91 | - public function uploadContent($content, $dest) |
|
92 | - { |
|
93 | - $this->exec("echo " . escapeshellarg($content) . " > " . escapeshellarg($dest)); |
|
94 | - } |
|
95 | - |
|
96 | - /** |
|
97 | - * Download a file from the given source on the server to the given file |
|
98 | - * @param string $source The remote filename to download |
|
99 | - * @param string $dest The local filename/dir to download to |
|
100 | - */ |
|
101 | - public function download($source, $dest) |
|
102 | - { |
|
103 | - if ($this->server) { |
|
104 | - $this->executor->execLocal(array("scp", "$this->server:$source", $dest)); |
|
105 | - } else { |
|
106 | - $this->executor->execLocal(array("cp", $file, $dest)); |
|
107 | - } |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * Returns true if the given file or directory exists |
|
112 | - * @param string $file The file/dir to look for |
|
113 | - * @return boolean |
|
114 | - */ |
|
115 | - public function exists($file = null) |
|
116 | - { |
|
117 | - if (!$file) { |
|
118 | - $file = $this->path; |
|
119 | - } |
|
120 | - if ($file == '@self') { |
|
121 | - return true; |
|
122 | - } |
|
123 | - |
|
124 | - if ($this->server) { |
|
125 | - $result = $this->exec("if [ -e " . escapeshellarg($file) . " ]; then echo yes; fi"); |
|
126 | - return (trim($result['output']) == 'yes'); |
|
127 | - } else { |
|
128 | - return file_exists($file); |
|
129 | - } |
|
130 | - } |
|
131 | - |
|
132 | - /** |
|
133 | - * Create the given file with the given content |
|
134 | - */ |
|
135 | - public function writeFile($file, $content) |
|
136 | - { |
|
137 | - if ($this->server) { |
|
138 | - $this->exec("echo " . escapeshellarg($content) . " > " . escapeshellarg($file)); |
|
139 | - } else { |
|
140 | - file_put_contents($file, $content); |
|
141 | - } |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Remove a file or folder from the webroot's server |
|
146 | - * |
|
147 | - * @param string $file The file to remove |
|
148 | - */ |
|
149 | - public function unlink($file) |
|
150 | - { |
|
151 | - if (!$file || $file == '/' || $file == '.') { |
|
152 | - throw new Exception("Can't unlink file '$file'"); |
|
153 | - } |
|
154 | - $this->exec(array('rm', '-rf', $file)); |
|
155 | - return true; |
|
156 | - } |
|
12 | + protected $server; |
|
13 | + protected $path; |
|
14 | + protected $executor; |
|
15 | + protected $identity = null; |
|
16 | + |
|
17 | + public function __construct($path, $executor) |
|
18 | + { |
|
19 | + $this->executor = $executor; |
|
20 | + |
|
21 | + if (strpos($path, ':') !== false) { |
|
22 | + list($this->server,$this->path) = explode(':', $path, 2); |
|
23 | + } else { |
|
24 | + $this->server = null; |
|
25 | + $this->path = $path; |
|
26 | + } |
|
27 | + } |
|
28 | + |
|
29 | + public function isLocal() |
|
30 | + { |
|
31 | + return $this->server == null; |
|
32 | + } |
|
33 | + public function getPath() |
|
34 | + { |
|
35 | + return $this->path; |
|
36 | + } |
|
37 | + public function getServer() |
|
38 | + { |
|
39 | + return $this->server; |
|
40 | + } |
|
41 | + public function setSSHItentityFile($filename) |
|
42 | + { |
|
43 | + $this->identity = $filename; |
|
44 | + } |
|
45 | + |
|
46 | + /** |
|
47 | + * Execute a command on the relevant server |
|
48 | + * @param string $command Shell command, either a fully escaped string or an array |
|
49 | + */ |
|
50 | + public function exec($command, $options = array()) |
|
51 | + { |
|
52 | + return $this->createProcess($command, $options)->exec(); |
|
53 | + } |
|
54 | + |
|
55 | + /** |
|
56 | + * Create a process for later exection |
|
57 | + * @param string $command Shell command, either a fully escaped string or an array |
|
58 | + * @return Process |
|
59 | + */ |
|
60 | + public function createProcess($command, $options = array()) |
|
61 | + { |
|
62 | + if ($this->server) { |
|
63 | + if ($this->identity && !isset($options['identity'])) { |
|
64 | + $options['identity'] = $this->identity; |
|
65 | + } |
|
66 | + return $this->executor->createRemote($this->server, $command, $options); |
|
67 | + } |
|
68 | + |
|
69 | + return $this->executor->createLocal($command, $options); |
|
70 | + } |
|
71 | + |
|
72 | + /** |
|
73 | + * Upload a file to the given destination on the server |
|
74 | + * @param string $file The file to upload |
|
75 | + * @param string $dest The remote filename/dir to upload to |
|
76 | + */ |
|
77 | + public function upload($source, $dest) |
|
78 | + { |
|
79 | + if ($this->server) { |
|
80 | + $this->executor->execLocal(array("scp", $source, "$this->server:$dest")); |
|
81 | + } else { |
|
82 | + $this->executor->execLocal(array("cp", $source, $dest)); |
|
83 | + } |
|
84 | + } |
|
85 | + |
|
86 | + /** |
|
87 | + * Create a file with the given content at the given destination on the server |
|
88 | + * @param string $content The content of the file |
|
89 | + * @param string $dest The remote filename/dir to upload to |
|
90 | + */ |
|
91 | + public function uploadContent($content, $dest) |
|
92 | + { |
|
93 | + $this->exec("echo " . escapeshellarg($content) . " > " . escapeshellarg($dest)); |
|
94 | + } |
|
95 | + |
|
96 | + /** |
|
97 | + * Download a file from the given source on the server to the given file |
|
98 | + * @param string $source The remote filename to download |
|
99 | + * @param string $dest The local filename/dir to download to |
|
100 | + */ |
|
101 | + public function download($source, $dest) |
|
102 | + { |
|
103 | + if ($this->server) { |
|
104 | + $this->executor->execLocal(array("scp", "$this->server:$source", $dest)); |
|
105 | + } else { |
|
106 | + $this->executor->execLocal(array("cp", $file, $dest)); |
|
107 | + } |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * Returns true if the given file or directory exists |
|
112 | + * @param string $file The file/dir to look for |
|
113 | + * @return boolean |
|
114 | + */ |
|
115 | + public function exists($file = null) |
|
116 | + { |
|
117 | + if (!$file) { |
|
118 | + $file = $this->path; |
|
119 | + } |
|
120 | + if ($file == '@self') { |
|
121 | + return true; |
|
122 | + } |
|
123 | + |
|
124 | + if ($this->server) { |
|
125 | + $result = $this->exec("if [ -e " . escapeshellarg($file) . " ]; then echo yes; fi"); |
|
126 | + return (trim($result['output']) == 'yes'); |
|
127 | + } else { |
|
128 | + return file_exists($file); |
|
129 | + } |
|
130 | + } |
|
131 | + |
|
132 | + /** |
|
133 | + * Create the given file with the given content |
|
134 | + */ |
|
135 | + public function writeFile($file, $content) |
|
136 | + { |
|
137 | + if ($this->server) { |
|
138 | + $this->exec("echo " . escapeshellarg($content) . " > " . escapeshellarg($file)); |
|
139 | + } else { |
|
140 | + file_put_contents($file, $content); |
|
141 | + } |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Remove a file or folder from the webroot's server |
|
146 | + * |
|
147 | + * @param string $file The file to remove |
|
148 | + */ |
|
149 | + public function unlink($file) |
|
150 | + { |
|
151 | + if (!$file || $file == '/' || $file == '.') { |
|
152 | + throw new Exception("Can't unlink file '$file'"); |
|
153 | + } |
|
154 | + $this->exec(array('rm', '-rf', $file)); |
|
155 | + return true; |
|
156 | + } |
|
157 | 157 | } |
@@ -19,7 +19,7 @@ discard block |
||
19 | 19 | $this->executor = $executor; |
20 | 20 | |
21 | 21 | if (strpos($path, ':') !== false) { |
22 | - list($this->server,$this->path) = explode(':', $path, 2); |
|
22 | + list($this->server, $this->path) = explode(':', $path, 2); |
|
23 | 23 | } else { |
24 | 24 | $this->server = null; |
25 | 25 | $this->path = $path; |
@@ -90,7 +90,7 @@ discard block |
||
90 | 90 | */ |
91 | 91 | public function uploadContent($content, $dest) |
92 | 92 | { |
93 | - $this->exec("echo " . escapeshellarg($content) . " > " . escapeshellarg($dest)); |
|
93 | + $this->exec("echo ".escapeshellarg($content)." > ".escapeshellarg($dest)); |
|
94 | 94 | } |
95 | 95 | |
96 | 96 | /** |
@@ -122,7 +122,7 @@ discard block |
||
122 | 122 | } |
123 | 123 | |
124 | 124 | if ($this->server) { |
125 | - $result = $this->exec("if [ -e " . escapeshellarg($file) . " ]; then echo yes; fi"); |
|
125 | + $result = $this->exec("if [ -e ".escapeshellarg($file)." ]; then echo yes; fi"); |
|
126 | 126 | return (trim($result['output']) == 'yes'); |
127 | 127 | } else { |
128 | 128 | return file_exists($file); |
@@ -135,7 +135,7 @@ discard block |
||
135 | 135 | public function writeFile($file, $content) |
136 | 136 | { |
137 | 137 | if ($this->server) { |
138 | - $this->exec("echo " . escapeshellarg($content) . " > " . escapeshellarg($file)); |
|
138 | + $this->exec("echo ".escapeshellarg($content)." > ".escapeshellarg($file)); |
|
139 | 139 | } else { |
140 | 140 | file_put_contents($file, $content); |
141 | 141 | } |
@@ -4,22 +4,22 @@ |
||
4 | 4 | |
5 | 5 | interface TableWriter |
6 | 6 | { |
7 | - /** |
|
8 | - * Start writing, declaring the columns that will be provided. |
|
9 | - * Must be called before writeRecord() |
|
10 | - * @param array $columns The columns to be provided as keys in writeRecord() calls |
|
11 | - */ |
|
12 | - public function start($columns); |
|
7 | + /** |
|
8 | + * Start writing, declaring the columns that will be provided. |
|
9 | + * Must be called before writeRecord() |
|
10 | + * @param array $columns The columns to be provided as keys in writeRecord() calls |
|
11 | + */ |
|
12 | + public function start($columns); |
|
13 | 13 | |
14 | - /** |
|
15 | - * Write a single record. |
|
16 | - * @param array $record A map of column => value data |
|
17 | - */ |
|
18 | - public function writeRecord($record); |
|
14 | + /** |
|
15 | + * Write a single record. |
|
16 | + * @param array $record A map of column => value data |
|
17 | + */ |
|
18 | + public function writeRecord($record); |
|
19 | 19 | |
20 | - /** |
|
21 | - * Finish writing. |
|
22 | - * writeRecord() must not be called after this |
|
23 | - */ |
|
24 | - public function finish(); |
|
20 | + /** |
|
21 | + * Finish writing. |
|
22 | + * writeRecord() must not be called after this |
|
23 | + */ |
|
24 | + public function finish(); |
|
25 | 25 | } |
@@ -4,67 +4,67 @@ |
||
4 | 4 | |
5 | 5 | class CsvTableWriter implements TableWriter |
6 | 6 | { |
7 | - private $filename; |
|
8 | - private $handle; |
|
9 | - private $columns; |
|
7 | + private $filename; |
|
8 | + private $handle; |
|
9 | + private $columns; |
|
10 | 10 | |
11 | - public function __construct($filename) |
|
12 | - { |
|
13 | - $this->filename = $filename; |
|
14 | - } |
|
11 | + public function __construct($filename) |
|
12 | + { |
|
13 | + $this->filename = $filename; |
|
14 | + } |
|
15 | 15 | |
16 | - public function start($columns) |
|
17 | - { |
|
18 | - $this->open(); |
|
19 | - $this->putRow($columns); |
|
20 | - $this->columns = $columns; |
|
21 | - } |
|
16 | + public function start($columns) |
|
17 | + { |
|
18 | + $this->open(); |
|
19 | + $this->putRow($columns); |
|
20 | + $this->columns = $columns; |
|
21 | + } |
|
22 | 22 | |
23 | - public function finish() |
|
24 | - { |
|
25 | - $this->close(); |
|
26 | - } |
|
23 | + public function finish() |
|
24 | + { |
|
25 | + $this->close(); |
|
26 | + } |
|
27 | 27 | |
28 | - public function writeRecord($record) |
|
29 | - { |
|
30 | - if (!$this->columns) { |
|
31 | - $this->start(array_keys($record)); |
|
32 | - } |
|
28 | + public function writeRecord($record) |
|
29 | + { |
|
30 | + if (!$this->columns) { |
|
31 | + $this->start(array_keys($record)); |
|
32 | + } |
|
33 | 33 | |
34 | - $this->putRow($this->mapFromColumns($record)); |
|
35 | - } |
|
34 | + $this->putRow($this->mapFromColumns($record)); |
|
35 | + } |
|
36 | 36 | |
37 | - private function mapFromColumns($record) |
|
38 | - { |
|
39 | - $row = []; |
|
40 | - foreach ($this->columns as $i => $column) { |
|
41 | - $row[$i] = isset($record[$column]) ? $record[$column] : null; |
|
42 | - } |
|
43 | - return $row; |
|
44 | - } |
|
37 | + private function mapFromColumns($record) |
|
38 | + { |
|
39 | + $row = []; |
|
40 | + foreach ($this->columns as $i => $column) { |
|
41 | + $row[$i] = isset($record[$column]) ? $record[$column] : null; |
|
42 | + } |
|
43 | + return $row; |
|
44 | + } |
|
45 | 45 | |
46 | - private function putRow($row) |
|
47 | - { |
|
48 | - return fputcsv($this->handle, $row); |
|
49 | - } |
|
46 | + private function putRow($row) |
|
47 | + { |
|
48 | + return fputcsv($this->handle, $row); |
|
49 | + } |
|
50 | 50 | |
51 | - private function open() |
|
52 | - { |
|
53 | - if ($this->handle) { |
|
54 | - fclose($this->handle); |
|
55 | - $this->handle = null; |
|
56 | - } |
|
57 | - $this->handle = fopen($this->filename, 'w'); |
|
58 | - if (!$this->handle) { |
|
59 | - throw new \LogicException("Can't open $this->filename for writing."); |
|
60 | - } |
|
61 | - } |
|
51 | + private function open() |
|
52 | + { |
|
53 | + if ($this->handle) { |
|
54 | + fclose($this->handle); |
|
55 | + $this->handle = null; |
|
56 | + } |
|
57 | + $this->handle = fopen($this->filename, 'w'); |
|
58 | + if (!$this->handle) { |
|
59 | + throw new \LogicException("Can't open $this->filename for writing."); |
|
60 | + } |
|
61 | + } |
|
62 | 62 | |
63 | - private function close() |
|
64 | - { |
|
65 | - if ($this->handle) { |
|
66 | - fclose($this->handle); |
|
67 | - $this->handle = null; |
|
68 | - } |
|
69 | - } |
|
63 | + private function close() |
|
64 | + { |
|
65 | + if ($this->handle) { |
|
66 | + fclose($this->handle); |
|
67 | + $this->handle = null; |
|
68 | + } |
|
69 | + } |
|
70 | 70 | } |
@@ -4,73 +4,73 @@ |
||
4 | 4 | |
5 | 5 | class CsvTableReader implements TableReader |
6 | 6 | { |
7 | - private $filename; |
|
8 | - private $handle; |
|
9 | - private $columns; |
|
7 | + private $filename; |
|
8 | + private $handle; |
|
9 | + private $columns; |
|
10 | 10 | |
11 | - public function __construct($filename) |
|
12 | - { |
|
13 | - $this->filename = $filename; |
|
14 | - } |
|
11 | + public function __construct($filename) |
|
12 | + { |
|
13 | + $this->filename = $filename; |
|
14 | + } |
|
15 | 15 | |
16 | - public function getColumns() |
|
17 | - { |
|
18 | - if (!$this->columns) { |
|
19 | - $this->initColumns(); |
|
20 | - } |
|
21 | - return $this->columns; |
|
22 | - } |
|
16 | + public function getColumns() |
|
17 | + { |
|
18 | + if (!$this->columns) { |
|
19 | + $this->initColumns(); |
|
20 | + } |
|
21 | + return $this->columns; |
|
22 | + } |
|
23 | 23 | |
24 | - public function getIterator() |
|
25 | - { |
|
26 | - $this->columns = null; |
|
27 | - $this->initColumns(); |
|
24 | + public function getIterator() |
|
25 | + { |
|
26 | + $this->columns = null; |
|
27 | + $this->initColumns(); |
|
28 | 28 | |
29 | - while (($row = $this->getRow()) !== false) { |
|
30 | - yield $this->mapToColumns($row); |
|
31 | - } |
|
29 | + while (($row = $this->getRow()) !== false) { |
|
30 | + yield $this->mapToColumns($row); |
|
31 | + } |
|
32 | 32 | |
33 | - $this->close(); |
|
34 | - } |
|
33 | + $this->close(); |
|
34 | + } |
|
35 | 35 | |
36 | - private function mapToColumns($row) |
|
37 | - { |
|
38 | - $record = []; |
|
39 | - foreach ($row as $i => $value) { |
|
40 | - if (isset($this->columns[$i])) { |
|
41 | - $record[$this->columns[$i]] = $value; |
|
42 | - } else { |
|
43 | - throw new \LogicException("Row contains invalid column #$i\n" . var_export($row, true)); |
|
44 | - } |
|
45 | - } |
|
46 | - return $record; |
|
47 | - } |
|
36 | + private function mapToColumns($row) |
|
37 | + { |
|
38 | + $record = []; |
|
39 | + foreach ($row as $i => $value) { |
|
40 | + if (isset($this->columns[$i])) { |
|
41 | + $record[$this->columns[$i]] = $value; |
|
42 | + } else { |
|
43 | + throw new \LogicException("Row contains invalid column #$i\n" . var_export($row, true)); |
|
44 | + } |
|
45 | + } |
|
46 | + return $record; |
|
47 | + } |
|
48 | 48 | |
49 | - private function initColumns() |
|
50 | - { |
|
51 | - $this->open(); |
|
52 | - $this->columns = $this->getRow(); |
|
53 | - } |
|
49 | + private function initColumns() |
|
50 | + { |
|
51 | + $this->open(); |
|
52 | + $this->columns = $this->getRow(); |
|
53 | + } |
|
54 | 54 | |
55 | - private function getRow() |
|
56 | - { |
|
57 | - return fgetcsv($this->handle); |
|
58 | - } |
|
55 | + private function getRow() |
|
56 | + { |
|
57 | + return fgetcsv($this->handle); |
|
58 | + } |
|
59 | 59 | |
60 | - private function open() |
|
61 | - { |
|
62 | - if ($this->handle) { |
|
63 | - fclose($this->handle); |
|
64 | - $this->handle = null; |
|
65 | - } |
|
66 | - $this->handle = fopen($this->filename, 'r'); |
|
67 | - } |
|
60 | + private function open() |
|
61 | + { |
|
62 | + if ($this->handle) { |
|
63 | + fclose($this->handle); |
|
64 | + $this->handle = null; |
|
65 | + } |
|
66 | + $this->handle = fopen($this->filename, 'r'); |
|
67 | + } |
|
68 | 68 | |
69 | - private function close() |
|
70 | - { |
|
71 | - if ($this->handle) { |
|
72 | - fclose($this->handle); |
|
73 | - $this->handle = null; |
|
74 | - } |
|
75 | - } |
|
69 | + private function close() |
|
70 | + { |
|
71 | + if ($this->handle) { |
|
72 | + fclose($this->handle); |
|
73 | + $this->handle = null; |
|
74 | + } |
|
75 | + } |
|
76 | 76 | } |
@@ -40,7 +40,7 @@ |
||
40 | 40 | if (isset($this->columns[$i])) { |
41 | 41 | $record[$this->columns[$i]] = $value; |
42 | 42 | } else { |
43 | - throw new \LogicException("Row contains invalid column #$i\n" . var_export($row, true)); |
|
43 | + throw new \LogicException("Row contains invalid column #$i\n".var_export($row, true)); |
|
44 | 44 | } |
45 | 45 | } |
46 | 46 | return $record; |
@@ -10,129 +10,129 @@ |
||
10 | 10 | */ |
11 | 11 | class DatabaseConnector |
12 | 12 | { |
13 | - private $basePath; |
|
14 | - private $isConnected = false; |
|
15 | - |
|
16 | - public function __construct($basePath) |
|
17 | - { |
|
18 | - $this->basePath = $basePath; |
|
19 | - } |
|
20 | - |
|
21 | - public function connect() |
|
22 | - { |
|
23 | - if ($this->isConnected) { |
|
24 | - return; |
|
25 | - } |
|
26 | - |
|
27 | - $this->isConnected = true; |
|
28 | - |
|
29 | - // Necessary for SilverStripe's _ss_environment.php loader to work |
|
30 | - $_SERVER['SCRIPT_FILENAME'] = $this->basePath . '/dummy.php'; |
|
31 | - |
|
32 | - global $databaseConfig; |
|
33 | - |
|
34 | - // require composers autoloader |
|
35 | - if (file_exists($this->basePath . '/vendor/autoload.php')) { |
|
36 | - require_once $this->basePath . '/vendor/autoload.php'; |
|
37 | - } |
|
38 | - |
|
39 | - if (file_exists($this->basePath . '/framework/core/Core.php')) { |
|
40 | - require_once($this->basePath . '/framework/core/Core.php'); |
|
41 | - } elseif (file_exists($this->basePath . '/sapphire/core/Core.php')) { |
|
42 | - require_once($this->basePath . '/sapphire/core/Core.php'); |
|
43 | - } else { |
|
44 | - throw new \LogicException( |
|
45 | - "No framework/core/Core.php or sapphire/core/Core.php included in project. " . |
|
46 | - "Perhaps $this->basePath is not a SilverStripe project?" |
|
47 | - ); |
|
48 | - } |
|
49 | - |
|
50 | - // Connect to database |
|
51 | - require_once('model/DB.php'); |
|
52 | - |
|
53 | - if ($databaseConfig) { |
|
54 | - DB::connect($databaseConfig); |
|
55 | - } else { |
|
56 | - throw new \LogicException("No \$databaseConfig found"); |
|
57 | - } |
|
58 | - } |
|
59 | - |
|
60 | - public function getDatabase() |
|
61 | - { |
|
62 | - $this->connect(); |
|
63 | - |
|
64 | - if (method_exists('DB', 'get_conn')) { |
|
65 | - return DB::get_conn(); |
|
66 | - } else { |
|
67 | - return DB::getConn(); |
|
68 | - } |
|
69 | - } |
|
70 | - |
|
71 | - /** |
|
72 | - * Get a list of tables from the database |
|
73 | - */ |
|
74 | - public function getTables() |
|
75 | - { |
|
76 | - $this->connect(); |
|
77 | - |
|
78 | - if (method_exists('DB', 'table_list')) { |
|
79 | - return DB::table_list(); |
|
80 | - } else { |
|
81 | - return DB::tableList(); |
|
82 | - } |
|
83 | - } |
|
84 | - |
|
85 | - /** |
|
86 | - * Get a list of tables from the database |
|
87 | - */ |
|
88 | - public function getFieldsForTable($tableName) |
|
89 | - { |
|
90 | - $this->connect(); |
|
91 | - |
|
92 | - if (method_exists('DB', 'field_list')) { |
|
93 | - return DB::field_list($tableName); |
|
94 | - } else { |
|
95 | - return DB::fieldList($tableName); |
|
96 | - } |
|
97 | - } |
|
98 | - |
|
99 | - /** |
|
100 | - * Save the named table to the given table write |
|
101 | - */ |
|
102 | - public function saveTable($tableName, TableWriter $writer) |
|
103 | - { |
|
104 | - $query = $this->getDatabase()->query("SELECT * FROM \"$tableName\""); |
|
105 | - |
|
106 | - foreach ($query as $record) { |
|
107 | - $writer->writeRecord($record); |
|
108 | - } |
|
109 | - |
|
110 | - $writer->finish(); |
|
111 | - } |
|
112 | - |
|
113 | - /** |
|
114 | - * Save the named table to the given table write |
|
115 | - */ |
|
116 | - public function loadTable($tableName, TableReader $reader) |
|
117 | - { |
|
118 | - $this->getDatabase()->clearTable($tableName); |
|
119 | - |
|
120 | - $fields = $this->getFieldsForTable($tableName); |
|
121 | - |
|
122 | - foreach ($reader as $record) { |
|
123 | - foreach ($record as $k => $v) { |
|
124 | - if (!isset($fields[$k])) { |
|
125 | - unset($record[$k]); |
|
126 | - } |
|
127 | - } |
|
128 | - // TODO: Batch records |
|
129 | - $manipulation = [ |
|
130 | - $tableName => [ |
|
131 | - 'command' => 'insert', |
|
132 | - 'fields' => $record, |
|
133 | - ], |
|
134 | - ]; |
|
135 | - DB::manipulate($manipulation); |
|
136 | - } |
|
137 | - } |
|
13 | + private $basePath; |
|
14 | + private $isConnected = false; |
|
15 | + |
|
16 | + public function __construct($basePath) |
|
17 | + { |
|
18 | + $this->basePath = $basePath; |
|
19 | + } |
|
20 | + |
|
21 | + public function connect() |
|
22 | + { |
|
23 | + if ($this->isConnected) { |
|
24 | + return; |
|
25 | + } |
|
26 | + |
|
27 | + $this->isConnected = true; |
|
28 | + |
|
29 | + // Necessary for SilverStripe's _ss_environment.php loader to work |
|
30 | + $_SERVER['SCRIPT_FILENAME'] = $this->basePath . '/dummy.php'; |
|
31 | + |
|
32 | + global $databaseConfig; |
|
33 | + |
|
34 | + // require composers autoloader |
|
35 | + if (file_exists($this->basePath . '/vendor/autoload.php')) { |
|
36 | + require_once $this->basePath . '/vendor/autoload.php'; |
|
37 | + } |
|
38 | + |
|
39 | + if (file_exists($this->basePath . '/framework/core/Core.php')) { |
|
40 | + require_once($this->basePath . '/framework/core/Core.php'); |
|
41 | + } elseif (file_exists($this->basePath . '/sapphire/core/Core.php')) { |
|
42 | + require_once($this->basePath . '/sapphire/core/Core.php'); |
|
43 | + } else { |
|
44 | + throw new \LogicException( |
|
45 | + "No framework/core/Core.php or sapphire/core/Core.php included in project. " . |
|
46 | + "Perhaps $this->basePath is not a SilverStripe project?" |
|
47 | + ); |
|
48 | + } |
|
49 | + |
|
50 | + // Connect to database |
|
51 | + require_once('model/DB.php'); |
|
52 | + |
|
53 | + if ($databaseConfig) { |
|
54 | + DB::connect($databaseConfig); |
|
55 | + } else { |
|
56 | + throw new \LogicException("No \$databaseConfig found"); |
|
57 | + } |
|
58 | + } |
|
59 | + |
|
60 | + public function getDatabase() |
|
61 | + { |
|
62 | + $this->connect(); |
|
63 | + |
|
64 | + if (method_exists('DB', 'get_conn')) { |
|
65 | + return DB::get_conn(); |
|
66 | + } else { |
|
67 | + return DB::getConn(); |
|
68 | + } |
|
69 | + } |
|
70 | + |
|
71 | + /** |
|
72 | + * Get a list of tables from the database |
|
73 | + */ |
|
74 | + public function getTables() |
|
75 | + { |
|
76 | + $this->connect(); |
|
77 | + |
|
78 | + if (method_exists('DB', 'table_list')) { |
|
79 | + return DB::table_list(); |
|
80 | + } else { |
|
81 | + return DB::tableList(); |
|
82 | + } |
|
83 | + } |
|
84 | + |
|
85 | + /** |
|
86 | + * Get a list of tables from the database |
|
87 | + */ |
|
88 | + public function getFieldsForTable($tableName) |
|
89 | + { |
|
90 | + $this->connect(); |
|
91 | + |
|
92 | + if (method_exists('DB', 'field_list')) { |
|
93 | + return DB::field_list($tableName); |
|
94 | + } else { |
|
95 | + return DB::fieldList($tableName); |
|
96 | + } |
|
97 | + } |
|
98 | + |
|
99 | + /** |
|
100 | + * Save the named table to the given table write |
|
101 | + */ |
|
102 | + public function saveTable($tableName, TableWriter $writer) |
|
103 | + { |
|
104 | + $query = $this->getDatabase()->query("SELECT * FROM \"$tableName\""); |
|
105 | + |
|
106 | + foreach ($query as $record) { |
|
107 | + $writer->writeRecord($record); |
|
108 | + } |
|
109 | + |
|
110 | + $writer->finish(); |
|
111 | + } |
|
112 | + |
|
113 | + /** |
|
114 | + * Save the named table to the given table write |
|
115 | + */ |
|
116 | + public function loadTable($tableName, TableReader $reader) |
|
117 | + { |
|
118 | + $this->getDatabase()->clearTable($tableName); |
|
119 | + |
|
120 | + $fields = $this->getFieldsForTable($tableName); |
|
121 | + |
|
122 | + foreach ($reader as $record) { |
|
123 | + foreach ($record as $k => $v) { |
|
124 | + if (!isset($fields[$k])) { |
|
125 | + unset($record[$k]); |
|
126 | + } |
|
127 | + } |
|
128 | + // TODO: Batch records |
|
129 | + $manipulation = [ |
|
130 | + $tableName => [ |
|
131 | + 'command' => 'insert', |
|
132 | + 'fields' => $record, |
|
133 | + ], |
|
134 | + ]; |
|
135 | + DB::manipulate($manipulation); |
|
136 | + } |
|
137 | + } |
|
138 | 138 | } |
@@ -27,22 +27,22 @@ |
||
27 | 27 | $this->isConnected = true; |
28 | 28 | |
29 | 29 | // Necessary for SilverStripe's _ss_environment.php loader to work |
30 | - $_SERVER['SCRIPT_FILENAME'] = $this->basePath . '/dummy.php'; |
|
30 | + $_SERVER['SCRIPT_FILENAME'] = $this->basePath.'/dummy.php'; |
|
31 | 31 | |
32 | 32 | global $databaseConfig; |
33 | 33 | |
34 | 34 | // require composers autoloader |
35 | - if (file_exists($this->basePath . '/vendor/autoload.php')) { |
|
36 | - require_once $this->basePath . '/vendor/autoload.php'; |
|
35 | + if (file_exists($this->basePath.'/vendor/autoload.php')) { |
|
36 | + require_once $this->basePath.'/vendor/autoload.php'; |
|
37 | 37 | } |
38 | 38 | |
39 | - if (file_exists($this->basePath . '/framework/core/Core.php')) { |
|
40 | - require_once($this->basePath . '/framework/core/Core.php'); |
|
41 | - } elseif (file_exists($this->basePath . '/sapphire/core/Core.php')) { |
|
42 | - require_once($this->basePath . '/sapphire/core/Core.php'); |
|
39 | + if (file_exists($this->basePath.'/framework/core/Core.php')) { |
|
40 | + require_once($this->basePath.'/framework/core/Core.php'); |
|
41 | + } elseif (file_exists($this->basePath.'/sapphire/core/Core.php')) { |
|
42 | + require_once($this->basePath.'/sapphire/core/Core.php'); |
|
43 | 43 | } else { |
44 | 44 | throw new \LogicException( |
45 | - "No framework/core/Core.php or sapphire/core/Core.php included in project. " . |
|
45 | + "No framework/core/Core.php or sapphire/core/Core.php included in project. ". |
|
46 | 46 | "Perhaps $this->basePath is not a SilverStripe project?" |
47 | 47 | ); |
48 | 48 | } |
@@ -4,15 +4,15 @@ |
||
4 | 4 | |
5 | 5 | interface TableReader extends \IteratorAggregate |
6 | 6 | { |
7 | - /** |
|
8 | - * Return an iterator that returns each record of the table reader as a map. |
|
9 | - * @return Iterator |
|
10 | - */ |
|
11 | - public function getIterator(); |
|
7 | + /** |
|
8 | + * Return an iterator that returns each record of the table reader as a map. |
|
9 | + * @return Iterator |
|
10 | + */ |
|
11 | + public function getIterator(); |
|
12 | 12 | |
13 | - /** |
|
14 | - * Return the names of the the columns in this table |
|
15 | - * @return array The column names |
|
16 | - */ |
|
17 | - public function getColumns(); |
|
13 | + /** |
|
14 | + * Return the names of the the columns in this table |
|
15 | + * @return array The column names |
|
16 | + */ |
|
17 | + public function getColumns(); |
|
18 | 18 | } |