|
1
|
|
|
<?php |
|
2
|
|
|
namespace Dennis\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 Dennis\Seeder; |
|
29
|
|
|
use TYPO3\CMS\Core\Utility\GeneralUtility; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Class SeederFactory |
|
33
|
|
|
* |
|
34
|
|
|
* @package Dennis\Seeder\Factory\SeederFactory |
|
35
|
|
|
*/ |
|
36
|
|
|
class SeederFactory implements \Dennis\Seeder\SeederFactory, \TYPO3\CMS\Core\SingletonInterface |
|
37
|
|
|
{ |
|
38
|
|
|
/** |
|
39
|
|
|
* @var Seeder\Faker |
|
40
|
|
|
*/ |
|
41
|
|
|
protected $faker; |
|
42
|
|
|
|
|
43
|
|
|
public function __construct(Seeder\Faker $faker) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->faker = $faker; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Creates a new SeedCollection |
|
50
|
|
|
* |
|
51
|
|
|
* @param string $name |
|
52
|
|
|
* @param int $limit |
|
53
|
|
|
* @return Seeder\SeedCollection |
|
54
|
|
|
*/ |
|
55
|
|
|
public function create($name, $limit = 1) |
|
56
|
|
|
{ |
|
57
|
|
|
$calledClass = debug_backtrace(false, 2)[1]['class']; |
|
58
|
|
|
|
|
59
|
|
|
return $this->fillSeedCollection($calledClass, $name, $limit, true); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @param $name |
|
64
|
|
|
* @param int $limit |
|
65
|
|
|
* @return Seeder\SeedCollection|mixed |
|
|
|
|
|
|
66
|
|
|
*/ |
|
67
|
|
|
public function make($name, $limit = 1) |
|
68
|
|
|
{ |
|
69
|
|
|
$calledClass = debug_backtrace(false, 2)[1]['class']; |
|
70
|
|
|
|
|
71
|
|
|
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
|
|
|
protected function fillSeedCollection($calledClass, $name, $limit = 1, $force = false) |
|
81
|
|
|
{ |
|
82
|
|
|
/** @var Seeder\Provider\TableConfiguration $tableConfiguration */ |
|
83
|
|
|
$tableConfiguration = GeneralUtility::makeInstance(Seeder\Provider\TableConfiguration::class, $name); |
|
84
|
|
|
|
|
85
|
|
|
/** @var Seeder\SeedCollection $seedCollection */ |
|
86
|
|
|
$seedCollection = GeneralUtility::makeInstance(Seeder\Collection\SeedCollection::class); |
|
87
|
|
|
|
|
88
|
|
|
$seeds = []; |
|
89
|
|
|
for ($i = 1; $i <= $limit; $i++) { |
|
90
|
|
|
/** @var Seeder\Seed $seed */ |
|
91
|
|
|
$seed = GeneralUtility::makeInstance(Seeder\Domain\Model\Seed::class); |
|
92
|
|
|
$seed->setTarget($name) |
|
93
|
|
|
->setTitle($calledClass) |
|
94
|
|
|
->setProperties($tableConfiguration->getColumns()); |
|
95
|
|
|
$seeds[$i] = $seed; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
if ($force) { |
|
99
|
|
|
foreach ($seeds as $seed) { |
|
100
|
|
|
$seedCollection->attach($seed); |
|
101
|
|
|
} |
|
102
|
|
|
} else { |
|
103
|
|
|
$amount = count($seeds) - $seedCollection->countByName($calledClass); |
|
104
|
|
|
for ($i = 1; $i <= $amount; $i++) { |
|
105
|
|
|
$seedCollection->attach($seeds[$i]); |
|
106
|
|
|
} |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
return $seedCollection; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
|
This check looks for the generic type
arrayas a return type and suggests a more specific type. This type is inferred from the actual code.