Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
80.39% |
41 / 51 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
Seeder | |
80.39% |
41 / 51 |
|
50.00% |
1 / 2 |
24.32 | |
0.00% |
0 / 1 |
create | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
run | |
75.61% |
31 / 41 |
|
0.00% |
0 / 1 |
22.70 |
1 | <?php |
2 | /** |
3 | * Pop PHP Framework (http://www.popphp.org/) |
4 | * |
5 | * @link https://github.com/popphp/popphp-framework |
6 | * @author Nick Sagona, III <dev@nolainteractive.com> |
7 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
8 | * @license http://www.popphp.org/license New BSD License |
9 | */ |
10 | |
11 | /** |
12 | * @namespace |
13 | */ |
14 | namespace Pop\Db\Sql; |
15 | |
16 | use Pop\Db\Adapter\AbstractAdapter; |
17 | use Pop\Db\Db; |
18 | |
19 | /** |
20 | * Sql seeder class |
21 | * |
22 | * @category Pop |
23 | * @package Pop\Db |
24 | * @author Nick Sagona, III <dev@nolainteractive.com> |
25 | * @copyright Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com) |
26 | * @license http://www.popphp.org/license New BSD License |
27 | * @version 6.5.0 |
28 | */ |
29 | class Seeder |
30 | { |
31 | |
32 | /** |
33 | * Create new migration file |
34 | * |
35 | * @param string $class |
36 | * @param ?string $path |
37 | * @throws Exception |
38 | * @return string |
39 | */ |
40 | public static function create(string $class, ?string $path = null): string |
41 | { |
42 | $file = date('YmdHis') . '_' . Parser\Table::parse($class) . '.php'; |
43 | $classContents = str_replace( |
44 | 'SeederTemplate', $class, file_get_contents(__DIR__ . '/Seeder/Template/SeederTemplate.php') |
45 | ); |
46 | |
47 | if ($path !== null) { |
48 | if (!is_dir($path)) { |
49 | throw new Exception('Error: That path does not exist'); |
50 | } |
51 | $file = $path . DIRECTORY_SEPARATOR . $file; |
52 | } |
53 | |
54 | file_put_contents($file, $classContents); |
55 | |
56 | return $file; |
57 | } |
58 | |
59 | /** |
60 | * Run the seeder |
61 | * |
62 | * @param AbstractAdapter $db |
63 | * @param string $path |
64 | * @param bool $clear |
65 | * @return array |
66 | *@throws Exception|\Pop\Db\Exception |
67 | */ |
68 | public static function run(AbstractAdapter $db, string $path, bool $clear = true): array |
69 | { |
70 | if (!file_exists($path)) { |
71 | throw new Exception("Error: That path doesn't not exist."); |
72 | } |
73 | |
74 | // Clear the database out |
75 | if ($clear) { |
76 | $schema = $db->createSchema(); |
77 | $tables = $db->getTables(); |
78 | |
79 | if (($db instanceof \Pop\Db\Adapter\Mysql) || |
80 | (($db instanceof \Pop\Db\Adapter\Pdo) && ($db->getType() == 'mysql'))) { |
81 | $db->query('SET foreign_key_checks = 0'); |
82 | foreach ($tables as $table) { |
83 | $schema->drop($table); |
84 | $db->query($schema); |
85 | } |
86 | $db->query('SET foreign_key_checks = 1'); |
87 | } else if (($db instanceof \Pop\Db\Adapter\Pgsql) || |
88 | (($db instanceof \Pop\Db\Adapter\Pdo) && ($db->getType() == 'pgsql'))) { |
89 | foreach ($tables as $table) { |
90 | $schema->drop($table)->cascade(); |
91 | $db->query($schema); |
92 | } |
93 | } else { |
94 | foreach ($tables as $table) { |
95 | $schema->drop($table); |
96 | $db->query($schema); |
97 | } |
98 | } |
99 | } |
100 | |
101 | $seedFilesRun = []; |
102 | $seedFiles = array_filter(scandir($path), function($value) { |
103 | return (($value != '.') && ($value != '..')); |
104 | }); |
105 | |
106 | // Run the SQL seeds |
107 | foreach ($seedFiles as $seed) { |
108 | if (stripos($seed, '.sql') !== false) { |
109 | Db::executeSqlFile($path . DIRECTORY_SEPARATOR . $seed, $db); |
110 | } else { |
111 | $fileContents = trim(file_get_contents($path . DIRECTORY_SEPARATOR . $seed)); |
112 | if ((str_contains($fileContents, 'extends AbstractSeeder'))) { |
113 | $namespace = null; |
114 | if (str_contains($fileContents, 'namespace ')) { |
115 | $namespace = substr($fileContents, (strpos($fileContents, 'namespace ') + 10)); |
116 | $namespace = trim(substr($namespace, 0, strpos($namespace, ';'))) . '\\'; |
117 | } |
118 | $class = substr($fileContents, (strpos($fileContents, 'class ') + 6)); |
119 | $class = $namespace . substr($class, 0, strpos($class, ' extends')); |
120 | |
121 | include $path . DIRECTORY_SEPARATOR . $seed; |
122 | $dbSeed = new $class(); |
123 | if ($dbSeed instanceof Seeder\SeederInterface) { |
124 | $dbSeed->run($db); |
125 | } |
126 | } |
127 | } |
128 | $seedFilesRun[] = $seed; |
129 | } |
130 | |
131 | return $seedFilesRun; |
132 | } |
133 | |
134 | } |