LippertComponents /
Blend
| 1 | <?php |
||||
| 2 | |||||
| 3 | namespace LCI\Blend; |
||||
| 4 | |||||
| 5 | use Exception; |
||||
| 6 | use LCI\Blend\Blendable\Chunk; |
||||
| 7 | use LCI\Blend\Blendable\Context; |
||||
| 8 | use LCI\Blend\Blendable\MediaSource; |
||||
| 9 | use LCI\Blend\Blendable\Plugin; |
||||
| 10 | use LCI\Blend\Blendable\Resource; |
||||
| 11 | use LCI\Blend\Blendable\Snippet; |
||||
| 12 | use LCI\Blend\Blendable\Template; |
||||
| 13 | use LCI\Blend\Helpers\Format; |
||||
| 14 | use LCI\Blend\Migrations\MigrationsCreator; |
||||
| 15 | use LCI\Blend\Model\xPDO\BlendMigrations; |
||||
| 16 | |||||
| 17 | /** |
||||
| 18 | * Class SeedMaker |
||||
| 19 | * @package LCI\Blend |
||||
| 20 | */ |
||||
| 21 | class SeedMaker |
||||
| 22 | { |
||||
| 23 | /** @var \modx */ |
||||
|
0 ignored issues
–
show
|
|||||
| 24 | protected $modx; |
||||
| 25 | |||||
| 26 | /** @var Blender */ |
||||
| 27 | protected $blender; |
||||
| 28 | |||||
| 29 | protected $format; |
||||
| 30 | |||||
| 31 | /** |
||||
| 32 | * SeedMaker constructor. |
||||
| 33 | * @param \modx $modx |
||||
| 34 | * @param Blender $blender |
||||
| 35 | */ |
||||
| 36 | public function __construct(\modx $modx, Blender $blender) |
||||
| 37 | { |
||||
| 38 | $this->modx = $modx; |
||||
| 39 | $this->blender = $blender; |
||||
| 40 | |||||
| 41 | $this->format = new Format($this->blender->getSeedsDir()); |
||||
| 42 | } |
||||
| 43 | |||||
| 44 | // Find: ->makeChunkSeeds( Replace: ->getSeedMaker()->makeChunkSeeds( |
||||
| 45 | /** |
||||
| 46 | * @param \xPDOQuery|array|null $criteria |
||||
|
0 ignored issues
–
show
The type
xPDOQuery was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths Loading history...
|
|||||
| 47 | * @param string $server_type |
||||
| 48 | * @param string $name |
||||
| 49 | * @param bool $create_migration_file |
||||
| 50 | * |
||||
| 51 | * @return array |
||||
| 52 | */ |
||||
| 53 | public function makeChunkSeeds($criteria, $server_type = 'master', $name = null, $create_migration_file = true) |
||||
| 54 | { |
||||
| 55 | $keys = []; |
||||
| 56 | $collection = $this->modx->getCollection('modChunk', $criteria); |
||||
| 57 | |||||
| 58 | foreach ($collection as $chunk) { |
||||
| 59 | /** @var \LCI\Blend\Blendable\Chunk $blendChunk */ |
||||
| 60 | $blendChunk = new Chunk($this->modx, $this->blender, $chunk->get('name')); |
||||
| 61 | $seed_key = $blendChunk |
||||
| 62 | ->setSeedsDir($this->format->getMigrationName('chunk', $name)) |
||||
| 63 | ->seed(); |
||||
| 64 | $this->blender->out("Chunk: ".$chunk->get('name').' Key: '.$seed_key); |
||||
| 65 | $keys[] = $seed_key; |
||||
| 66 | } |
||||
| 67 | |||||
| 68 | if ($create_migration_file) { |
||||
| 69 | /** @var MigrationsCreator $migrationCreator */ |
||||
| 70 | $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys); |
||||
| 71 | |||||
| 72 | $migrationCreator |
||||
| 73 | ->setPathTimeStamp($this->format->getPathTimeStamp()) |
||||
| 74 | ->setVerbose($this->blender->getVerbose()) |
||||
| 75 | ->setName($name) |
||||
| 76 | ->setDescription('') |
||||
| 77 | ->setServerType($server_type) |
||||
| 78 | ->setMigrationsPath($this->blender->getMigrationPath()) |
||||
| 79 | ->createChunkMigrationClassFile(); |
||||
| 80 | |||||
| 81 | $this->logCreatedSeedMigration($migrationCreator->getLogData()); |
||||
| 82 | } |
||||
| 83 | return $keys; |
||||
| 84 | } |
||||
| 85 | |||||
| 86 | /** |
||||
| 87 | * @param \xPDOQuery|array|null $criteria |
||||
| 88 | * @param string $server_type |
||||
| 89 | * @param string $name |
||||
| 90 | * @param bool $create_migration_file |
||||
| 91 | * |
||||
| 92 | * @return array |
||||
| 93 | */ |
||||
| 94 | public function makeContextSeeds($criteria, $server_type = 'master', $name = null, $create_migration_file = true) |
||||
| 95 | { |
||||
| 96 | $keys = []; |
||||
| 97 | $collection = $this->modx->getCollection('modContext', $criteria); |
||||
| 98 | |||||
| 99 | foreach ($collection as $context) { |
||||
| 100 | /** @var Context $blendContext */ |
||||
| 101 | $blendContext = new Context($this->modx, $this->blender, $context->get('key')); |
||||
| 102 | $seed_key = $blendContext |
||||
| 103 | ->setSeedsDir($this->format->getMigrationName('context', $name)) |
||||
| 104 | ->seed(); |
||||
| 105 | $this->blender->out("Context: ".$context->get('name').' Key: '.$seed_key); |
||||
| 106 | $keys[] = $seed_key; |
||||
| 107 | } |
||||
| 108 | |||||
| 109 | if ($create_migration_file) { |
||||
| 110 | /** @var MigrationsCreator $migrationCreator */ |
||||
| 111 | $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys); |
||||
| 112 | |||||
| 113 | $migrationCreator |
||||
| 114 | ->setPathTimeStamp($this->format->getPathTimeStamp()) |
||||
| 115 | ->setVerbose($this->blender->getVerbose()) |
||||
| 116 | ->setName($name) |
||||
| 117 | ->setDescription('') |
||||
| 118 | ->setServerType($server_type) |
||||
| 119 | ->setMigrationsPath($this->blender->getMigrationPath()) |
||||
| 120 | ->createContextMigrationClassFile(); |
||||
| 121 | |||||
| 122 | $this->logCreatedSeedMigration($migrationCreator->getLogData()); |
||||
| 123 | } |
||||
| 124 | return $keys; |
||||
| 125 | } |
||||
| 126 | |||||
| 127 | /** |
||||
| 128 | * @param \xPDOQuery|array|null $criteria |
||||
| 129 | * @param string $server_type |
||||
| 130 | * @param string $name |
||||
| 131 | * @param bool $create_migration_file |
||||
| 132 | * |
||||
| 133 | * @return array |
||||
| 134 | */ |
||||
| 135 | public function makeMediaSourceSeeds($criteria, $server_type = 'master', $name = null, $create_migration_file = true) |
||||
| 136 | { |
||||
| 137 | $keys = []; |
||||
| 138 | $collection = $this->modx->getCollection('modMediaSource', $criteria); |
||||
| 139 | |||||
| 140 | foreach ($collection as $mediaSource) { |
||||
| 141 | /** @var MediaSource $blendMediaSource */ |
||||
| 142 | $blendMediaSource = new MediaSource($this->modx, $this->blender, $mediaSource->get('name')); |
||||
| 143 | $seed_key = $blendMediaSource |
||||
| 144 | ->setSeedsDir($this->format->getMigrationName('mediaSource', $name)) |
||||
| 145 | ->seed(); |
||||
| 146 | $this->blender->out("Media Source: ".$mediaSource->get('name').' Key: '.$seed_key); |
||||
| 147 | $keys[] = $seed_key; |
||||
| 148 | } |
||||
| 149 | |||||
| 150 | if ($create_migration_file) { |
||||
| 151 | /** @var MigrationsCreator $migrationCreator */ |
||||
| 152 | $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys); |
||||
| 153 | |||||
| 154 | $migrationCreator |
||||
| 155 | ->setPathTimeStamp($this->format->getPathTimeStamp()) |
||||
| 156 | ->setVerbose($this->blender->getVerbose()) |
||||
| 157 | ->setName($name) |
||||
| 158 | ->setDescription('') |
||||
| 159 | ->setServerType($server_type) |
||||
| 160 | ->setMigrationsPath($this->blender->getMigrationPath()) |
||||
| 161 | ->createMediaSourceMigrationClassFile(); |
||||
| 162 | |||||
| 163 | $this->logCreatedSeedMigration($migrationCreator->getLogData()); |
||||
| 164 | } |
||||
| 165 | return $keys; |
||||
| 166 | } |
||||
| 167 | |||||
| 168 | /** |
||||
| 169 | * @param \xPDOQuery|array|null $criteria |
||||
| 170 | * @param string $server_type |
||||
| 171 | * @param string $name |
||||
| 172 | * @param bool $create_migration_file |
||||
| 173 | * |
||||
| 174 | * @return array |
||||
| 175 | */ |
||||
| 176 | public function makePluginSeeds($criteria, $server_type = 'master', $name = null, $create_migration_file = true) |
||||
| 177 | { |
||||
| 178 | $keys = []; |
||||
| 179 | $collection = $this->modx->getCollection('modPlugin', $criteria); |
||||
| 180 | |||||
| 181 | foreach ($collection as $plugin) { |
||||
| 182 | /** @var \LCI\Blend\Blendable\Plugin $blendPlugin */ |
||||
| 183 | $blendPlugin = new Plugin($this->modx, $this->blender, $plugin->get('name')); |
||||
| 184 | $seed_key = $blendPlugin |
||||
| 185 | ->setSeedsDir($this->format->getMigrationName('plugin', $name)) |
||||
| 186 | ->seed(); |
||||
| 187 | $this->blender->out("Plugin: ".$plugin->get('name').' Key: '.$seed_key); |
||||
| 188 | $keys[] = $seed_key; |
||||
| 189 | } |
||||
| 190 | |||||
| 191 | if ($create_migration_file) { |
||||
| 192 | /** @var MigrationsCreator $migrationCreator */ |
||||
| 193 | $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys); |
||||
| 194 | |||||
| 195 | $migrationCreator |
||||
| 196 | ->setPathTimeStamp($this->format->getPathTimeStamp()) |
||||
| 197 | ->setVerbose($this->blender->getVerbose()) |
||||
| 198 | ->setName($name) |
||||
| 199 | ->setDescription('') |
||||
| 200 | ->setServerType($server_type) |
||||
| 201 | ->setMigrationsPath($this->blender->getMigrationPath()) |
||||
| 202 | ->createPluginMigrationClassFile(); |
||||
| 203 | |||||
| 204 | $this->logCreatedSeedMigration($migrationCreator->getLogData()); |
||||
| 205 | } |
||||
| 206 | return $keys; |
||||
| 207 | } |
||||
| 208 | |||||
| 209 | /** |
||||
| 210 | * @param \xPDOQuery|array|null $criteria |
||||
| 211 | * @param string $server_type |
||||
| 212 | * @param string $name |
||||
| 213 | * @param bool $create_migration_file |
||||
| 214 | * |
||||
| 215 | * @return array |
||||
| 216 | */ |
||||
| 217 | public function makeResourceSeeds($criteria, $server_type = 'master', $name = null, $create_migration_file = true) |
||||
| 218 | { |
||||
| 219 | $keys = [ |
||||
| 220 | 'web' => [] |
||||
| 221 | ]; |
||||
| 222 | |||||
| 223 | $collection = $this->modx->getCollection('modResource', $criteria); |
||||
| 224 | foreach ($collection as $resource) { |
||||
| 225 | $blendResource = new Resource($this->modx, $this->blender, $resource->get('alias'), $resource->get('context_key')); |
||||
| 226 | $seed_key = $blendResource |
||||
| 227 | ->setSeedsDir($this->format->getMigrationName('resource', $name)) |
||||
| 228 | ->seed(); |
||||
| 229 | $this->blender->out("ID: ".$resource->get('id').' Key: '.$seed_key); |
||||
| 230 | |||||
| 231 | if (!isset($keys[$resource->get('context_key')])) { |
||||
| 232 | $keys[$resource->get('context_key')] = []; |
||||
| 233 | } |
||||
| 234 | |||||
| 235 | $keys[$resource->get('context_key')][] = $seed_key; |
||||
| 236 | } |
||||
| 237 | |||||
| 238 | if ($create_migration_file) { |
||||
| 239 | /** @var MigrationsCreator $migrationCreator */ |
||||
| 240 | $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys); |
||||
| 241 | |||||
| 242 | $migrationCreator |
||||
| 243 | ->setPathTimeStamp($this->format->getPathTimeStamp()) |
||||
| 244 | ->setVerbose($this->blender->getVerbose()) |
||||
| 245 | ->setName($name) |
||||
| 246 | ->setDescription('') |
||||
| 247 | ->setServerType($server_type) |
||||
| 248 | ->setMigrationsPath($this->blender->getMigrationPath()) |
||||
| 249 | ->createResourceMigrationClassFile(); |
||||
| 250 | |||||
| 251 | $this->logCreatedSeedMigration($migrationCreator->getLogData()); |
||||
| 252 | } |
||||
| 253 | return $keys; |
||||
| 254 | } |
||||
| 255 | |||||
| 256 | /** |
||||
| 257 | * @param \xPDOQuery|array|null $criteria |
||||
| 258 | * @param string $server_type |
||||
| 259 | * @param string $name |
||||
| 260 | * @param bool $create_migration_file |
||||
| 261 | * |
||||
| 262 | * @return array |
||||
| 263 | */ |
||||
| 264 | public function makeSnippetSeeds($criteria, $server_type = 'master', $name = null, $create_migration_file = true) |
||||
| 265 | { |
||||
| 266 | $keys = []; |
||||
| 267 | $collection = $this->modx->getCollection('modSnippet', $criteria); |
||||
| 268 | |||||
| 269 | foreach ($collection as $snippet) { |
||||
| 270 | /** @var \LCI\Blend\Blendable\Snippet $blendSnippet */ |
||||
| 271 | $blendSnippet = new Snippet($this->modx, $this->blender, $snippet->get('name')); |
||||
| 272 | $seed_key = $blendSnippet |
||||
| 273 | ->setSeedsDir($this->format->getMigrationName('snippet', $name)) |
||||
| 274 | ->seed(); |
||||
| 275 | $this->blender->out("Snippet: ".$snippet->get('name').' Key: '.$seed_key); |
||||
| 276 | $keys[] = $seed_key; |
||||
| 277 | } |
||||
| 278 | |||||
| 279 | if ($create_migration_file) { |
||||
| 280 | /** @var MigrationsCreator $migrationCreator */ |
||||
| 281 | $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys); |
||||
| 282 | |||||
| 283 | $migrationCreator |
||||
| 284 | ->setPathTimeStamp($this->format->getPathTimeStamp()) |
||||
| 285 | ->setVerbose($this->blender->getVerbose()) |
||||
| 286 | ->setName($name) |
||||
| 287 | ->setDescription('') |
||||
| 288 | ->setServerType($server_type) |
||||
| 289 | ->setMigrationsPath($this->blender->getMigrationPath()) |
||||
| 290 | ->createSnippetMigrationClassFile(); |
||||
| 291 | |||||
| 292 | $this->logCreatedSeedMigration($migrationCreator->getLogData()); |
||||
| 293 | } |
||||
| 294 | return $keys; |
||||
| 295 | } |
||||
| 296 | |||||
| 297 | /** |
||||
| 298 | * @param \xPDOQuery|array|null $criteria |
||||
| 299 | * @param string $server_type |
||||
| 300 | * @param string $name |
||||
| 301 | * @param bool $create_migration_file |
||||
| 302 | * |
||||
| 303 | * @return array |
||||
| 304 | */ |
||||
| 305 | public function makeSystemSettingSeeds($criteria, $server_type = 'master', $name = null, $create_migration_file = true) |
||||
| 306 | { |
||||
| 307 | $collection = $this->modx->getCollection('modSystemSetting', $criteria); |
||||
| 308 | |||||
| 309 | $setting_data = []; |
||||
| 310 | /** @var \modSystemSetting $setting */ |
||||
| 311 | foreach ($collection as $setting) { |
||||
| 312 | /** @var \LCI\Blend\Blendable\SystemSetting $blendableSetting */ |
||||
| 313 | $blendableSetting = $this->blender->getBlendableLoader()->getBlendableSystemSetting($setting->get('key')); |
||||
| 314 | $setting_data[] = $blendableSetting->seedToArray(); |
||||
| 315 | } |
||||
| 316 | |||||
| 317 | // https://docs.modx.com/revolution/2.x/developing-in-modx/other-development-resources/class-reference/modx/modx.invokeevent |
||||
| 318 | $this->modx->invokeEvent( |
||||
| 319 | 'OnBlendSeedSystemSettings', |
||||
| 320 | [ |
||||
| 321 | 'blender' => $this->blender, |
||||
| 322 | 'data' => &$setting_data |
||||
| 323 | ] |
||||
| 324 | ); |
||||
| 325 | |||||
| 326 | if ($create_migration_file) { |
||||
| 327 | /** @var MigrationsCreator $migrationCreator */ |
||||
| 328 | $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $setting_data); |
||||
| 329 | |||||
| 330 | $migrationCreator |
||||
| 331 | ->setPathTimeStamp($this->format->getPathTimeStamp()) |
||||
| 332 | ->setVerbose($this->blender->getVerbose()) |
||||
| 333 | ->setName($name) |
||||
| 334 | ->setDescription('') |
||||
| 335 | ->setServerType($server_type) |
||||
| 336 | ->setMigrationsPath($this->blender->getMigrationPath()) |
||||
| 337 | ->createSystemSettingsMigrationClassFile(); |
||||
| 338 | |||||
| 339 | $this->logCreatedSeedMigration($migrationCreator->getLogData()); |
||||
| 340 | } |
||||
| 341 | return $setting_data; |
||||
| 342 | } |
||||
| 343 | |||||
| 344 | /** |
||||
| 345 | * @param \xPDOQuery|array|null $criteria |
||||
| 346 | * @param string $server_type |
||||
| 347 | * @param string $name |
||||
| 348 | * @param bool $create_migration_file |
||||
| 349 | * |
||||
| 350 | * @return array |
||||
| 351 | */ |
||||
| 352 | public function makeTemplateSeeds($criteria, $server_type = 'master', $name = null, $create_migration_file = true) |
||||
| 353 | { |
||||
| 354 | $keys = []; |
||||
| 355 | $collection = $this->modx->getCollection('modTemplate', $criteria); |
||||
| 356 | |||||
| 357 | foreach ($collection as $template) { |
||||
| 358 | //exit(); |
||||
| 359 | /** @var \LCI\Blend\Blendable\Template $blendTemplate */ |
||||
| 360 | $blendTemplate = new Template($this->modx, $this->blender, $template->get('templatename')); |
||||
| 361 | $seed_key = $blendTemplate |
||||
| 362 | ->setSeedsDir($this->format->getMigrationName('template', $name)) |
||||
| 363 | ->seed(); |
||||
| 364 | $this->blender->out("Template ID: ".$template->get('id').' Key: '.$seed_key); |
||||
| 365 | $keys[] = $seed_key; |
||||
| 366 | } |
||||
| 367 | |||||
| 368 | if ($create_migration_file) { |
||||
| 369 | /** @var MigrationsCreator $migrationCreator */ |
||||
| 370 | $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $keys); |
||||
| 371 | |||||
| 372 | $migrationCreator |
||||
| 373 | ->setPathTimeStamp($this->format->getPathTimeStamp()) |
||||
| 374 | ->setVerbose($this->blender->getVerbose()) |
||||
| 375 | ->setName($name) |
||||
| 376 | ->setDescription('') |
||||
| 377 | ->setServerType($server_type) |
||||
| 378 | ->setMigrationsPath($this->blender->getMigrationPath()) |
||||
| 379 | ->createTemplateMigrationClassFile(); |
||||
| 380 | |||||
| 381 | $this->logCreatedSeedMigration($migrationCreator->getLogData()); |
||||
| 382 | } |
||||
| 383 | return $keys; |
||||
| 384 | } |
||||
| 385 | |||||
| 386 | /** |
||||
| 387 | * @param string $server_type |
||||
| 388 | * @param null|string $name |
||||
| 389 | */ |
||||
| 390 | public function makeSiteSeed($server_type = 'master', $name = null) |
||||
| 391 | { |
||||
| 392 | $site_data = [ |
||||
| 393 | 'mediaSources' => $this->makeMediaSourceSeeds(null, $server_type, $name, false), |
||||
| 394 | 'contexts' => $this->makeContextSeeds(null, $server_type, $name, false), |
||||
| 395 | 'chunks' => $this->makeChunkSeeds(null, $server_type, $name, false), |
||||
| 396 | 'plugins' => $this->makePluginSeeds(null, $server_type, $name, false), |
||||
| 397 | 'resources' => $this->makeResourceSeeds(null, $server_type, $name, false), |
||||
| 398 | 'snippets' => $this->makeSnippetSeeds(null, $server_type, $name, false), |
||||
| 399 | 'systemSettings' => $this->makeSystemSettingSeeds(null, $server_type, $name, false), |
||||
| 400 | 'templates' => $this->makeTemplateSeeds(null, $server_type, $name, false) |
||||
| 401 | ]; |
||||
| 402 | |||||
| 403 | /** @var MigrationsCreator $migrationCreator */ |
||||
| 404 | $migrationCreator = new MigrationsCreator($this->blender->getUserInteractionHandler(), $site_data); |
||||
| 405 | |||||
| 406 | $migrationCreator |
||||
| 407 | ->setPathTimeStamp($this->format->getPathTimeStamp()) |
||||
| 408 | ->setVerbose($this->blender->getVerbose()) |
||||
| 409 | ->setName($name) |
||||
| 410 | ->setDescription('') |
||||
| 411 | ->setServerType($server_type) |
||||
| 412 | ->setMigrationsPath($this->blender->getMigrationPath()) |
||||
| 413 | ->createSiteMigrationClassFile(); |
||||
| 414 | |||||
| 415 | $this->logCreatedSeedMigration($migrationCreator->getLogData()); |
||||
| 416 | } |
||||
| 417 | |||||
| 418 | /** |
||||
| 419 | * @param array $data |
||||
| 420 | */ |
||||
| 421 | protected function logCreatedSeedMigration($data = []) |
||||
| 422 | { |
||||
| 423 | try { |
||||
| 424 | /** @var BlendMigrations $migration */ |
||||
| 425 | $migration = $this->modx->newObject($this->blender->getBlendClassObject()); |
||||
| 426 | if ($migration) { |
||||
| 427 | $migration->fromArray($data); |
||||
| 428 | $migration->save(); |
||||
| 429 | } |
||||
| 430 | } catch (Exception $exception) { |
||||
| 431 | $this->blender->out($exception->getMessage(), true); |
||||
|
0 ignored issues
–
show
true of type true is incompatible with the type integer expected by parameter $verbose of LCI\Blend\Blender::out().
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||
| 432 | } |
||||
| 433 | } |
||||
| 434 | } |
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths