1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ConsoleTools\Controller; |
4
|
|
|
|
5
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
6
|
|
|
use Zend\Console\ColorInterface as Color; |
7
|
|
|
use Zend\Console\Adapter\AdapterInterface as Console; |
8
|
|
|
use Zend\Console\Exception\RuntimeException; |
9
|
|
|
use ConsoleTools\Model\Fixture; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Apply fixtures to database |
13
|
|
|
* |
14
|
|
|
* @author V.Leontiev <[email protected]> |
15
|
|
|
* @license http://opensource.org/licenses/MIT MIT |
16
|
|
|
* @since php 5.6 or higher |
17
|
|
|
* @see https://github.com/newage/console-tools |
18
|
|
|
*/ |
19
|
|
|
class FixtureController extends AbstractActionController |
20
|
|
|
{ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Folder for fixtures |
24
|
|
|
* Folder must be there for use completion-bash |
25
|
|
|
*/ |
26
|
|
|
const FOLDER_FIXTURES = '/data/fixtures/'; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Folder to fixture files |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $fixtureFolder = null; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Apply fixture |
37
|
|
|
* @TODO Need use transaction |
38
|
|
|
* |
39
|
|
|
* @throws RuntimeException |
40
|
|
|
*/ |
41
|
|
|
public function applyAction() |
42
|
|
|
{ |
43
|
|
|
$console = $this->getServiceLocator()->get('console'); |
44
|
|
|
if (!$console instanceof Console) { |
45
|
|
|
throw new RuntimeException('Cannot obtain console adapter. Are we running in a console?'); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
$fixturePath = $this->getFixtureFolder(); |
49
|
|
|
if (!is_dir($fixturePath)) { |
50
|
|
|
mkdir($fixturePath, 0777); |
51
|
|
|
$console->writeLine('Don\'t exists folder of fixtures!', Color::RED); |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$adapter = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter'); |
56
|
|
|
$model = new Fixture($adapter); |
|
|
|
|
57
|
|
|
$request = $this->getRequest(); |
58
|
|
|
$fixtureName = $request->getParam('name', 'all'); |
59
|
|
|
|
60
|
|
|
if ($fixtureName == 'all') { |
61
|
|
|
$fixtureFiles = scandir($fixturePath); |
62
|
|
|
unset($fixtureFiles[0]); |
63
|
|
|
unset($fixtureFiles[1]); |
64
|
|
|
} else { |
65
|
|
|
$fixtureFiles = array($fixtureName . '.php'); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
foreach ($fixtureFiles as $fixtureFile) { |
69
|
|
|
$fixture = include $fixturePath . $fixtureFile; |
70
|
|
|
|
71
|
|
|
foreach ($fixture as $tableName => $rows) { |
72
|
|
|
$console->writeLine( |
73
|
|
|
'Will apply fixture of the table "'.$tableName.'" from file: '.$fixtureFile, |
74
|
|
|
Color::GREEN |
75
|
|
|
); |
76
|
|
|
$values = isset($rows['values']) ? $rows['values'] : $rows; |
77
|
|
|
|
78
|
|
|
foreach ($values as $rowNumber => $row) { |
79
|
|
|
try { |
80
|
|
|
$row = isset($rows['keys']) ? array_combine($rows['keys'], $row) : $row; |
81
|
|
|
|
82
|
|
|
$model->insert($tableName, $row); |
83
|
|
|
} catch (\Exception $err) { |
84
|
|
|
$console->writeLine(' - error in row: ' . $rowNumber, Color::RED); |
85
|
|
|
$console->writeLine($err->getMessage(), Color::RED); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$console->writeLine('Fixture files applied', Color::GREEN); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get migration folder from config file |
96
|
|
|
* |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
|
|
protected function getFixtureFolder() |
100
|
|
|
{ |
101
|
|
|
if ($this->fixtureFolder === null) { |
102
|
|
|
$this->fixtureFolder = getcwd() . self::FOLDER_FIXTURES; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $this->fixtureFolder; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: