Source of file Seeder.php
Size: 6,155 Bytes - Last Modified: 2021-12-23T10:02:17+00:00
/var/www/docs.ssmods.com/process/src/code/Seeder.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223 | <?php namespace Seeder; use Seeder\Helpers\ConfigParser; use Seeder\Helpers\HeuristicParser; use Seeder\Helpers\OutputFormatter; use Seeder\Util\SeederState; /** * Class Seeder */ class Seeder extends \Object { /** * @var */ private $writer; /** * @var bool */ private $ignoreSeeds = false; /** * @var OutputFormatter */ private $outputFormatter; /** * @param $writer * @param OutputFormatter $outputFormatter */ public function __construct($writer, OutputFormatter $outputFormatter) { parent::__construct(); $this->writer = $writer; $this->outputFormatter = $outputFormatter; } /** * @param null $onlyClassName * @param null $key * @throws \Exception */ public function seed($onlyClassName = null, $key = null) { // seed random to get different results each run srand(); $this->outputFormatter->beginSeed(); $dataObjects = $this->config()->create; $configParser = new ConfigParser($this->writer); $heuristics = array(); $config = \Config::inst()->get('Seeder', 'heuristics'); if ($config) { $heuristicParser = new HeuristicParser(); $heuristics = $heuristicParser->parse($config); } if (is_array($dataObjects)) { foreach ($dataObjects as $index => $option) { $className = $index; if (isset($option['class'])) { $className = $option['class']; } if (empty($option['key'])) { $option['key'] = $className; } if (class_exists($className) && (!$onlyClassName || $onlyClassName === $className) && (!$key || $key === $option['key']) ) { $option['class'] = $className; $field = $configParser->objectConfig2Field($option); $field->name = $option['class']; // has_many will generate the number passed in count $field->fieldName = ''; $field->methodName = ''; $field->count = $this->getCount($field); if (!$field->count) { $this->outputFormatter->alreadySeeded($option['class'], $option['key']); } else { $this->outputFormatter->creatingDataObject($option['class'], $option['key']); $this->applyHeuristics($field, $heuristics); $state = new SeederState(); $objects = $field->provider->generate($field, $state); $this->writer->finish(); $this->outputFormatter->dataObjectsCreated($option['class'], count($objects)); } } else { error_log('Class not found ' . $className); } } } else { throw new \Exception('\'create\' must be an array'); } } /** * @param $field * @return int */ private function getCount($field) { $count = isset($field->options['count']) ? $field->options['count'] : 1; if ($this->ignoreSeeds) { return $count; } $currentCount = \SeedRecord::get()->filter(array( 'Root' => true, 'SeedClassName' => $field->dataType, 'Key' => $field->key, ))->Count(); $count -= $currentCount; return $count; } /** * @param $field * @param $heuristics */ public function applyHeuristics($field, $heuristics) { $matching = array(); foreach ($heuristics as $heuristic) { if ($heuristic->match($field)) { $matching[] = $heuristic; } } usort($matching, function ($h1, $h2) { return $h1->getSpecificity() - $h2->getSpecificity(); }); foreach ($matching as $heuristic) { $heuristic->apply($field, $this->writer); } foreach ($field->fields as $db) { $this->applyHeuristics($db, $heuristics); } foreach ($field->hasOne as $hasOneField) { $this->applyHeuristics($hasOneField, $heuristics); } foreach ($field->hasMany as $hasManyField) { $this->applyHeuristics($hasManyField, $heuristics); } foreach ($field->manyMany as $manyManyField) { $this->applyHeuristics($manyManyField, $heuristics); } } /** * @param null $key */ public function unseed($key = null) { $this->outputFormatter->beginUnseed(); // TODO delete many_many tables $deleted = array(); $seeds = \SeedRecord::get(); if ($key) { $seeds = $seeds->filter('Key', $key); } while ($seeds->Count()) { // sort by id desc to delete in reverse $seedBatch = $seeds->limit(1000)->sort('ID DESC'); $classes = array(); foreach ($seedBatch as $seed) { $className = $seed->SeedClassName; $classes[$className][] = $seed->SeedID; $this->writer->delete($seed); } foreach ($classes as $className => $ids) { $versioned = \DataObject::has_extension($className, 'Versioned'); if ($versioned) { $this->writer->deleteIDsFromStage($className, $ids, 'Stage', 'Live'); } else { $this->writer->deleteIDs($className, $ids); } $deleted[$className] = count($ids); } $this->writer->finish(); } $this->outputFormatter->reportDataObjectsDeleted($deleted); } /** * @param bool $ignoreSeeds */ public function setIgnoreSeeds($ignoreSeeds = false) { $this->ignoreSeeds = $ignoreSeeds; } } |