LippertComponents /
Blend
| 1 | <?php |
||||||||
| 2 | /** |
||||||||
| 3 | * Created by PhpStorm. |
||||||||
| 4 | * User: joshgulledge |
||||||||
| 5 | * Date: 2/15/18 |
||||||||
| 6 | * Time: 3:24 PM |
||||||||
| 7 | */ |
||||||||
| 8 | |||||||||
| 9 | namespace LCI\Blend\Console; |
||||||||
| 10 | |||||||||
| 11 | use Symfony\Component\Console\Input\InputInterface; |
||||||||
| 12 | use Symfony\Component\Console\Output\OutputInterface; |
||||||||
| 13 | use Symfony\Component\Console\Input\InputOption; |
||||||||
| 14 | |||||||||
| 15 | class Seed extends BaseCommand |
||||||||
| 16 | { |
||||||||
| 17 | |||||||||
| 18 | /** |
||||||||
| 19 | * @see https://symfony.com/doc/current/console.html |
||||||||
| 20 | * |
||||||||
| 21 | */ |
||||||||
| 22 | protected function configure() |
||||||||
| 23 | { |
||||||||
| 24 | $this |
||||||||
| 25 | ->setName('blend:seed') |
||||||||
| 26 | ->setDescription('Export Blend seeds, data migrations') |
||||||||
| 27 | ->addOption( |
||||||||
| 28 | 'name', |
||||||||
| 29 | 'N', |
||||||||
| 30 | InputOption::VALUE_OPTIONAL, |
||||||||
| 31 | 'Append this value to current timestamp for the generated migration file and seeds directory' |
||||||||
| 32 | ) |
||||||||
| 33 | ->addOption( |
||||||||
| 34 | 'object', |
||||||||
| 35 | 'o', |
||||||||
| 36 | InputOption::VALUE_OPTIONAL, |
||||||||
| 37 | 'Seed object, default is r, can be r(resource), c(chunk), p(plugin), s(snippet), x(systemSettings), t(template) or a(site)', |
||||||||
| 38 | 'r' |
||||||||
| 39 | ) |
||||||||
| 40 | ->addOption( |
||||||||
| 41 | 'id', |
||||||||
| 42 | 'i', |
||||||||
| 43 | InputOption::VALUE_OPTIONAL, |
||||||||
| 44 | 'ID of migration to run' |
||||||||
| 45 | ) |
||||||||
| 46 | ->addOption( |
||||||||
| 47 | 'date', |
||||||||
| 48 | 'd', |
||||||||
| 49 | InputOption::VALUE_OPTIONAL, |
||||||||
| 50 | 'Date since created or modified, ex: 2018-02-16' |
||||||||
| 51 | ) |
||||||||
| 52 | ->addOption( |
||||||||
| 53 | 'type', |
||||||||
| 54 | 't', |
||||||||
| 55 | InputOption::VALUE_OPTIONAL, |
||||||||
| 56 | 'Server type to run migrations as, default is master. Possible master, staging, dev and local', |
||||||||
| 57 | 'master' |
||||||||
| 58 | ); |
||||||||
| 59 | } |
||||||||
| 60 | |||||||||
| 61 | /** |
||||||||
| 62 | * @param InputInterface $input |
||||||||
| 63 | * @param OutputInterface $output |
||||||||
| 64 | * @return int|null|void |
||||||||
| 65 | */ |
||||||||
| 66 | protected function execute(InputInterface $input, OutputInterface $output) |
||||||||
| 67 | { |
||||||||
| 68 | // what seeds script to run? |
||||||||
| 69 | $name = (string)$input->getOption('name'); |
||||||||
| 70 | $type = $input->getOption('type'); |
||||||||
| 71 | $object = $input->getOption('object'); |
||||||||
| 72 | $id = $input->getOption('id'); |
||||||||
| 73 | $date = $input->getOption('date'); |
||||||||
| 74 | |||||||||
| 75 | if ($object == 'c' || $object == 'chunk') { |
||||||||
| 76 | $this->seedChunks($type, $name, $id); |
||||||||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||||||||
| 77 | |||||||||
| 78 | } elseif ($object == 'p' || $object == 'plugin') { |
||||||||
| 79 | $this->seedPlugins($type, $name, $id); |
||||||||
|
0 ignored issues
–
show
It seems like
$type can also be of type string[]; however, parameter $type of LCI\Blend\Console\Seed::seedPlugins() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 80 | |||||||||
| 81 | } elseif ($object == 'r' || $object == 'resource') { |
||||||||
| 82 | $this->seedResources($type, $name, $id, $date); |
||||||||
|
0 ignored issues
–
show
It seems like
$type can also be of type string[]; however, parameter $type of LCI\Blend\Console\Seed::seedResources() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 83 | |||||||||
| 84 | } elseif ($object == 's' || $object == 'snippet') { |
||||||||
| 85 | $this->seedSnippets($type, $name, $id); |
||||||||
|
0 ignored issues
–
show
It seems like
$type can also be of type string[]; however, parameter $type of LCI\Blend\Console\Seed::seedSnippets() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 86 | |||||||||
| 87 | } elseif ($object == 'x' || $object == 'systemSettings') { |
||||||||
| 88 | $this->seedSystemSettings($type, $name, $id, $date); |
||||||||
|
0 ignored issues
–
show
It seems like
$date can also be of type string[]; however, parameter $date of LCI\Blend\Console\Seed::seedSystemSettings() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$type can also be of type string[]; however, parameter $type of LCI\Blend\Console\Seed::seedSystemSettings() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
It seems like
$id can also be of type string[]; however, parameter $key of LCI\Blend\Console\Seed::seedSystemSettings() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 89 | |||||||||
| 90 | } elseif ($object == 't' || $object == 'template') { |
||||||||
| 91 | $this->seedTemplates($type, $name, $id); |
||||||||
|
0 ignored issues
–
show
It seems like
$type can also be of type string[]; however, parameter $type of LCI\Blend\Console\Seed::seedTemplates() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 92 | |||||||||
| 93 | } elseif ($object == 'a' || $object == 'site') { |
||||||||
| 94 | $this->blender->getSeedMaker()->makeSiteSeed($type, $name); |
||||||||
|
0 ignored issues
–
show
It seems like
$type can also be of type string[]; however, parameter $server_type of LCI\Blend\SeedMaker::makeSiteSeed() does only seem to accept string, maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||||
| 95 | |||||||||
| 96 | } |
||||||||
| 97 | } |
||||||||
| 98 | |||||||||
| 99 | /** |
||||||||
| 100 | * @param string $type |
||||||||
| 101 | * @param string $name |
||||||||
| 102 | * @param int $id |
||||||||
| 103 | */ |
||||||||
| 104 | protected function seedChunks($type, $name, $id) |
||||||||
| 105 | { |
||||||||
| 106 | /** @var \xPDOQuery $criteria */ |
||||||||
| 107 | $criteria = $this->modx->newQuery('modChunk'); |
||||||||
| 108 | |||||||||
| 109 | if (!empty($id) && is_numeric($id)) { |
||||||||
| 110 | $criteria->where([ |
||||||||
| 111 | 'id' => $id |
||||||||
| 112 | ]); |
||||||||
| 113 | $criteria->orCondition(array( |
||||||||
| 114 | 'name' => $id |
||||||||
| 115 | )); |
||||||||
| 116 | |||||||||
| 117 | } else { |
||||||||
| 118 | $ids = explode(',', $this->consoleUserInteractionHandler->promptInput('Enter in a comma separated list of chunk names or IDs: ', '')); |
||||||||
| 119 | |||||||||
| 120 | $criteria->where([ |
||||||||
| 121 | 'id:IN' => $ids |
||||||||
| 122 | ]); |
||||||||
| 123 | $criteria->orCondition(array( |
||||||||
| 124 | 'name:IN' => $ids |
||||||||
| 125 | )); |
||||||||
| 126 | } |
||||||||
| 127 | |||||||||
| 128 | $this->blender->getSeedMaker()->makeChunkSeeds($criteria, $type, $name); |
||||||||
| 129 | } |
||||||||
| 130 | |||||||||
| 131 | /** |
||||||||
| 132 | * @param string $type |
||||||||
| 133 | * @param string $name |
||||||||
| 134 | * @param int $id |
||||||||
| 135 | */ |
||||||||
| 136 | protected function seedPlugins($type, $name, $id) |
||||||||
| 137 | { |
||||||||
| 138 | /** @var \xPDOQuery $criteria */ |
||||||||
| 139 | $criteria = $this->modx->newQuery('modPlugin'); |
||||||||
| 140 | |||||||||
| 141 | if (!empty($id) && is_numeric($id)) { |
||||||||
| 142 | $criteria->where([ |
||||||||
| 143 | 'id' => $id |
||||||||
| 144 | ]); |
||||||||
| 145 | $criteria->orCondition(array( |
||||||||
| 146 | 'name' => $id |
||||||||
| 147 | )); |
||||||||
| 148 | |||||||||
| 149 | } else { |
||||||||
| 150 | $input = $this->consoleUserInteractionHandler->promptInput('Enter in a comma separated list of plugin names or IDs: ', ''); |
||||||||
| 151 | $ids = explode(',', $input); |
||||||||
| 152 | |||||||||
| 153 | $criteria->where([ |
||||||||
| 154 | 'id:IN' => $ids |
||||||||
| 155 | ]); |
||||||||
| 156 | $criteria->orCondition(array( |
||||||||
| 157 | 'name:IN' => $ids |
||||||||
| 158 | )); |
||||||||
| 159 | } |
||||||||
| 160 | |||||||||
| 161 | $this->blender->getSeedMaker()->makePluginSeeds($criteria, $type, $name); |
||||||||
| 162 | } |
||||||||
| 163 | |||||||||
| 164 | /** |
||||||||
| 165 | * @param string $type |
||||||||
| 166 | * @param string $name |
||||||||
| 167 | * @param int $id |
||||||||
| 168 | * @param string $date |
||||||||
| 169 | */ |
||||||||
| 170 | protected function seedResources($type, $name, $id, $date) |
||||||||
| 171 | { |
||||||||
| 172 | /** @var \xPDOQuery $criteria */ |
||||||||
| 173 | $criteria = $this->modx->newQuery('modResource'); |
||||||||
| 174 | |||||||||
| 175 | if (isset($date) && !empty($date)) { |
||||||||
| 176 | $date = strtotime($date); |
||||||||
| 177 | $criteria->where([ |
||||||||
| 178 | 'editedon:>=' => $date |
||||||||
| 179 | ]); |
||||||||
| 180 | $criteria->orCondition(array( |
||||||||
| 181 | 'createdon:>=' => $date |
||||||||
| 182 | )); |
||||||||
| 183 | |||||||||
| 184 | } elseif (!empty($id) && is_numeric($id)) { |
||||||||
| 185 | $criteria->where([ |
||||||||
| 186 | 'id' => $id |
||||||||
| 187 | ]); |
||||||||
| 188 | |||||||||
| 189 | } else { |
||||||||
| 190 | $input = $this->consoleUserInteractionHandler->promptInput('Enter in a comma separated list of resource IDs: ', '2'); |
||||||||
| 191 | $ids = explode(',', $input); |
||||||||
| 192 | |||||||||
| 193 | $criteria->where([ |
||||||||
| 194 | 'id:IN' => $ids |
||||||||
| 195 | ]); |
||||||||
| 196 | |||||||||
| 197 | if ($this->consoleUserInteractionHandler->promptConfirm('Would you like to include the parents?')) { |
||||||||
| 198 | // get parents: |
||||||||
| 199 | $query = $this->modx->newQuery('modResource', ['id:IN' => $ids]); |
||||||||
| 200 | $query->select(['modResource.parent']); |
||||||||
| 201 | $query->prepare(); |
||||||||
| 202 | $criteria->orCondition('`modResource`.`id` IN('.$query->toSQL().')'); |
||||||||
| 203 | } |
||||||||
| 204 | |||||||||
| 205 | if ($this->consoleUserInteractionHandler->promptConfirm('Would you like to include direct children?')) { |
||||||||
| 206 | // get direct children: |
||||||||
| 207 | $query = $this->modx->newQuery('modResource', ['parent:IN' => $ids]); |
||||||||
| 208 | $query->select(['modResource.id']); |
||||||||
| 209 | $query->prepare(); |
||||||||
| 210 | $children_sql = $query->toSQL(); |
||||||||
| 211 | $criteria->orCondition('`modResource`.`id` IN('.$children_sql.')'); |
||||||||
| 212 | |||||||||
| 213 | if ($this->consoleUserInteractionHandler->promptConfirm('Would you like to include direct grand children?')) { |
||||||||
| 214 | // get grand children |
||||||||
| 215 | $query = $this->modx->newQuery('modResource'); |
||||||||
| 216 | $query->select(['modResource.parent']); |
||||||||
| 217 | $query->where('`modResource`.`id` IN('.$children_sql.')'); |
||||||||
| 218 | $query->prepare(); |
||||||||
| 219 | $criteria->orCondition('`modResource`.`id` IN('.$query->toSQL().')'); |
||||||||
| 220 | } |
||||||||
| 221 | } |
||||||||
| 222 | } |
||||||||
| 223 | |||||||||
| 224 | $this->blender->getSeedMaker()->makeResourceSeeds($criteria, $type, $name); |
||||||||
| 225 | } |
||||||||
| 226 | |||||||||
| 227 | /** |
||||||||
| 228 | * @param string $type |
||||||||
| 229 | * @param string $name |
||||||||
| 230 | * @param int $id |
||||||||
| 231 | */ |
||||||||
| 232 | protected function seedSnippets($type, $name, $id) |
||||||||
| 233 | { |
||||||||
| 234 | /** @var \xPDOQuery $criteria */ |
||||||||
| 235 | $criteria = $this->modx->newQuery('modSnippet'); |
||||||||
| 236 | |||||||||
| 237 | if (!empty($id) && is_numeric($id)) { |
||||||||
| 238 | $criteria->where([ |
||||||||
| 239 | 'id' => $id |
||||||||
| 240 | ]); |
||||||||
| 241 | $criteria->orCondition(array( |
||||||||
| 242 | 'name' => $id |
||||||||
| 243 | )); |
||||||||
| 244 | |||||||||
| 245 | } else { |
||||||||
| 246 | $input = $this->consoleUserInteractionHandler->promptInput('Enter in a comma separated list of snippet names or IDs: '); |
||||||||
| 247 | $ids = explode(',', $input); |
||||||||
| 248 | |||||||||
| 249 | $criteria->where([ |
||||||||
| 250 | 'id:IN' => $ids |
||||||||
| 251 | ]); |
||||||||
| 252 | $criteria->orCondition(array( |
||||||||
| 253 | 'name:IN' => $ids |
||||||||
| 254 | )); |
||||||||
| 255 | } |
||||||||
| 256 | |||||||||
| 257 | $this->blender->getSeedMaker()->makeSnippetSeeds($criteria, $type, $name); |
||||||||
| 258 | } |
||||||||
| 259 | |||||||||
| 260 | /** |
||||||||
| 261 | * @param string $type |
||||||||
| 262 | * @param string $name |
||||||||
| 263 | * @param string $key |
||||||||
| 264 | * @param string $date |
||||||||
| 265 | */ |
||||||||
| 266 | protected function seedSystemSettings($type, $name, $key, $date) |
||||||||
| 267 | { |
||||||||
| 268 | /** @var \xPDOQuery $criteria */ |
||||||||
| 269 | $criteria = $this->modx->newQuery('modSystemSetting'); |
||||||||
| 270 | |||||||||
| 271 | if (isset($date) && !empty($date)) { |
||||||||
| 272 | $criteria->where([ |
||||||||
| 273 | 'editedon:>=' => $date |
||||||||
| 274 | ]); |
||||||||
| 275 | |||||||||
| 276 | } elseif (!empty($key) && strlen($key) > 1) { |
||||||||
| 277 | $criteria->where([ |
||||||||
| 278 | 'key' => $key |
||||||||
| 279 | ]); |
||||||||
| 280 | |||||||||
| 281 | } else { |
||||||||
| 282 | $names = $this->consoleUserInteractionHandler->promptInput('Enter in a comma separated list of system settings: '); |
||||||||
| 283 | |||||||||
| 284 | $criteria->where([ |
||||||||
| 285 | 'key:IN' => explode(',', $names) |
||||||||
| 286 | ]); |
||||||||
| 287 | } |
||||||||
| 288 | $this->blender->getSeedMaker()->makeSystemSettingSeeds($criteria, $type, $name); |
||||||||
| 289 | } |
||||||||
| 290 | |||||||||
| 291 | /** |
||||||||
| 292 | * @param string $type |
||||||||
| 293 | * @param string $name |
||||||||
| 294 | * @param int $id |
||||||||
| 295 | */ |
||||||||
| 296 | protected function seedTemplates($type, $name, $id) |
||||||||
| 297 | { |
||||||||
| 298 | /** @var \xPDOQuery $criteria */ |
||||||||
| 299 | $criteria = $this->modx->newQuery('modTemplate'); |
||||||||
| 300 | |||||||||
| 301 | if (is_numeric($id) && $id > 0) { |
||||||||
| 302 | $criteria->where([ |
||||||||
| 303 | 'id' => $id |
||||||||
| 304 | ]); |
||||||||
| 305 | |||||||||
| 306 | } else { |
||||||||
| 307 | $input = $this->consoleUserInteractionHandler->promptInput('Enter in a comma separated list of template names or IDs: '); |
||||||||
| 308 | |||||||||
| 309 | $ids = explode(',', $input); |
||||||||
| 310 | |||||||||
| 311 | $criteria->where([ |
||||||||
| 312 | 'id:IN' => $ids |
||||||||
| 313 | ]); |
||||||||
| 314 | $criteria->orCondition(array( |
||||||||
| 315 | 'templatename:IN' => $ids |
||||||||
| 316 | )); |
||||||||
| 317 | |||||||||
| 318 | } |
||||||||
| 319 | |||||||||
| 320 | $this->blender->getSeedMaker()->makeTemplateSeeds($criteria, $type, $name); |
||||||||
| 321 | } |
||||||||
| 322 | |||||||||
| 323 | } |