1
|
|
|
<?php |
2
|
|
|
namespace TildBJ\Seeder\Factory; |
3
|
|
|
|
4
|
|
|
/*************************************************************** |
5
|
|
|
* |
6
|
|
|
* Copyright notice |
7
|
|
|
* |
8
|
|
|
* (c) 2016 Dennis Römmich <[email protected]> |
9
|
|
|
* |
10
|
|
|
* All rights reserved |
11
|
|
|
* |
12
|
|
|
* This script is part of the TYPO3 project. The TYPO3 project is |
13
|
|
|
* free software; you can redistribute it and/or modify |
14
|
|
|
* it under the terms of the GNU General Public License as published by |
15
|
|
|
* the Free Software Foundation; either version 3 of the License, or |
16
|
|
|
* (at your option) any later version. |
17
|
|
|
* |
18
|
|
|
* The GNU General Public License can be found at |
19
|
|
|
* http://www.gnu.org/copyleft/gpl.html. |
20
|
|
|
* |
21
|
|
|
* This script is distributed in the hope that it will be useful, |
22
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
23
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
24
|
|
|
* GNU General Public License for more details. |
25
|
|
|
* |
26
|
|
|
* This copyright notice MUST APPEAR in all copies of the script! |
27
|
|
|
***************************************************************/ |
28
|
|
|
use TildBJ\Seeder; |
29
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Class SeederFactory |
33
|
|
|
* |
34
|
|
|
* @package TildBJ\Seeder\Factory\SeederFactory |
35
|
|
|
*/ |
36
|
|
|
class SeederFactory implements \TildBJ\Seeder\SeederFactory, \TYPO3\CMS\Core\SingletonInterface |
37
|
|
|
{ |
38
|
|
|
/** |
39
|
|
|
* @var Seeder\Faker |
40
|
|
|
*/ |
41
|
|
|
protected $faker; |
42
|
|
|
|
43
|
1 |
|
public function __construct(Seeder\Faker $faker) |
44
|
|
|
{ |
45
|
1 |
|
$this->faker = $faker; |
46
|
1 |
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Creates a new SeedCollection |
50
|
|
|
* |
51
|
|
|
* @param string $name |
52
|
|
|
* @param int $limit |
53
|
|
|
* @return Seeder\SeedCollection |
54
|
|
|
*/ |
55
|
7 |
|
public function create($name, $limit = 1) |
56
|
|
|
{ |
57
|
7 |
|
$calledClass = debug_backtrace(false, 2)[1]['class']; |
58
|
|
|
|
59
|
7 |
|
return $this->fillSeedCollection($calledClass, $name, $limit, true); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param $name |
64
|
|
|
* @param int $limit |
65
|
|
|
* @return Seeder\SeedCollection|mixed |
|
|
|
|
66
|
|
|
*/ |
67
|
6 |
|
public function make($name, $limit = 1) |
68
|
|
|
{ |
69
|
6 |
|
$calledClass = debug_backtrace(false, 2)[1]['class']; |
70
|
|
|
|
71
|
6 |
|
return $this->fillSeedCollection($calledClass, $name, $limit); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $calledClass |
76
|
|
|
* @param $name |
77
|
|
|
* @param int $limit |
78
|
|
|
* @return Seeder\SeedCollection |
79
|
|
|
*/ |
80
|
7 |
|
protected function fillSeedCollection($calledClass, $name, $limit = 1, $force = false) |
81
|
|
|
{ |
82
|
|
|
/** @var Seeder\Provider\TableConfiguration $tableConfiguration */ |
83
|
7 |
|
$tableConfiguration = GeneralUtility::makeInstance(Seeder\Provider\TableConfiguration::class, $name); |
84
|
|
|
|
85
|
|
|
/** @var Seeder\SeedCollection $seedCollection */ |
86
|
7 |
|
$seedCollection = GeneralUtility::makeInstance(Seeder\Collection\SeedCollection::class); |
87
|
|
|
|
88
|
7 |
|
$seeds = []; |
89
|
7 |
|
for ($i = 1; $i <= $limit; $i++) { |
90
|
|
|
/** @var Seeder\Seed $seed */ |
91
|
7 |
|
$seed = GeneralUtility::makeInstance(Seeder\Domain\Model\Seed::class); |
92
|
7 |
|
$seed->setTarget($name) |
93
|
7 |
|
->setTitle($calledClass) |
94
|
7 |
|
->setProperties($tableConfiguration->getColumns()); |
95
|
7 |
|
$seeds[$i] = $seed; |
96
|
|
|
} |
97
|
|
|
|
98
|
7 |
|
if ($force) { |
99
|
7 |
|
foreach ($seeds as $seed) { |
100
|
7 |
|
$seedCollection->attach($seed); |
101
|
|
|
} |
102
|
|
|
} else { |
103
|
6 |
|
$amount = count($seeds) - $seedCollection->countByName($calledClass); |
104
|
6 |
|
for ($i = 1; $i <= $amount; $i++) { |
105
|
6 |
|
$seedCollection->attach($seeds[$i]); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
7 |
|
return $seedCollection; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
This check looks for the generic type
array
as a return type and suggests a more specific type. This type is inferred from the actual code.