| Total Complexity | 133 |
| Total Lines | 877 |
| Duplicated Lines | 0 % |
| Changes | 2 | ||
| Bugs | 0 | Features | 0 |
Complex classes like MailEclipse often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MailEclipse, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class MailEclipse |
||
| 22 | { |
||
| 23 | public static $view_namespace = 'maileclipse'; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Default type examples for being passed to reflected classes. |
||
| 27 | * |
||
| 28 | * @var array TYPES |
||
| 29 | */ |
||
| 30 | public const TYPES = [ |
||
| 31 | 'int' => 31, |
||
| 32 | // 'string' => 'test_string', // not needed as it can be cast __toString() |
||
| 33 | 'bool' => false, |
||
| 34 | 'float' => 3.14159, |
||
| 35 | ]; |
||
| 36 | |||
| 37 | public static function getMailables() |
||
| 38 | { |
||
| 39 | return self::mailablesList(); |
||
| 40 | } |
||
| 41 | |||
| 42 | public static function getMailable($key, $name) |
||
| 43 | { |
||
| 44 | $filtered = collect(self::getMailables())->where($key, $name); |
||
| 45 | |||
| 46 | return $filtered; |
||
| 47 | } |
||
| 48 | |||
| 49 | public static function deleteTemplate($templateSlug) |
||
| 50 | { |
||
| 51 | $template = self::getTemplates() |
||
| 52 | ->where('template_slug', $templateSlug)->first(); |
||
| 53 | |||
| 54 | if (! is_null($template)) { |
||
| 55 | self::saveTemplates(self::getTemplates()->reject(function ($value, $key) use ($template) { |
||
|
|
|||
| 56 | return $value->template_slug == $template->template_slug; |
||
| 57 | })); |
||
| 58 | |||
| 59 | $template_view = self::$view_namespace.'::templates.'.$templateSlug; |
||
| 60 | $template_plaintext_view = $template_view.'_plain_text'; |
||
| 61 | |||
| 62 | if (View::exists($template_view)) { |
||
| 63 | unlink(View($template_view)->getPath()); |
||
| 64 | |||
| 65 | if (View::exists($template_plaintext_view)) { |
||
| 66 | unlink(View($template_plaintext_view)->getPath()); |
||
| 67 | } |
||
| 68 | |||
| 69 | return true; |
||
| 70 | } |
||
| 71 | } |
||
| 72 | |||
| 73 | return false; |
||
| 74 | } |
||
| 75 | |||
| 76 | public static function getTemplatesFile() |
||
| 77 | { |
||
| 78 | $file = config('maileclipse.mailables_dir').'templates.json'; |
||
| 79 | if (! file_exists($file)) { |
||
| 80 | if (! file_exists(config('maileclipse.mailables_dir'))) { |
||
| 81 | mkdir(config('maileclipse.mailables_dir')); |
||
| 82 | } |
||
| 83 | file_put_contents($file, '[]'); |
||
| 84 | } |
||
| 85 | |||
| 86 | return $file; |
||
| 87 | } |
||
| 88 | |||
| 89 | public static function saveTemplates(Collection $templates) |
||
| 90 | { |
||
| 91 | file_put_contents(self::getTemplatesFile(), $templates->toJson()); |
||
| 92 | } |
||
| 93 | |||
| 94 | public static function updateTemplate($request) |
||
| 95 | { |
||
| 96 | $template = self::getTemplates() |
||
| 97 | ->where('template_slug', $request->templateslug)->first(); |
||
| 98 | |||
| 99 | if (! is_null($template)) { |
||
| 100 | if (! preg_match("/^[a-zA-Z0-9-_\s]+$/", $request->title)) { |
||
| 101 | return response()->json([ |
||
| 102 | 'status' => 'failed', |
||
| 103 | 'message' => 'Template name not valid', |
||
| 104 | ]); |
||
| 105 | } |
||
| 106 | |||
| 107 | $templatename = Str::camel(preg_replace('/\s+/', '_', $request->title)); |
||
| 108 | |||
| 109 | // check if not already exists on db |
||
| 110 | // |
||
| 111 | // |
||
| 112 | |||
| 113 | if (self::getTemplates()->contains('template_slug', '=', $templatename)) { |
||
| 114 | return response()->json([ |
||
| 115 | |||
| 116 | 'status' => 'failed', |
||
| 117 | 'message' => 'Template name already exists', |
||
| 118 | |||
| 119 | ]); |
||
| 120 | } |
||
| 121 | |||
| 122 | // Update |
||
| 123 | // |
||
| 124 | $oldForm = self::getTemplates()->reject(function ($value, $key) use ($template) { |
||
| 125 | return $value->template_slug == $template->template_slug; |
||
| 126 | }); |
||
| 127 | $newForm = array_merge($oldForm->toArray(), [array_merge((array) $template, [ |
||
| 128 | 'template_slug' => $templatename, |
||
| 129 | 'template_name' => $request->title, |
||
| 130 | 'template_description' => $request->description, |
||
| 131 | ])]); |
||
| 132 | |||
| 133 | self::saveTemplates(collect($newForm)); |
||
| 134 | |||
| 135 | $template_view = self::$view_namespace.'::templates.'.$request->templateslug; |
||
| 136 | $template_plaintext_view = $template_view.'_plain_text'; |
||
| 137 | |||
| 138 | if (View::exists($template_view)) { |
||
| 139 | $viewPath = View($template_view)->getPath(); |
||
| 140 | |||
| 141 | rename($viewPath, dirname($viewPath)."/{$templatename}.blade.php"); |
||
| 142 | |||
| 143 | if (View::exists($template_plaintext_view)) { |
||
| 144 | $textViewPath = View($template_plaintext_view)->getPath(); |
||
| 145 | |||
| 146 | rename($textViewPath, dirname($viewPath)."/{$templatename}_plain_text.blade.php"); |
||
| 147 | } |
||
| 148 | } |
||
| 149 | |||
| 150 | return response()->json([ |
||
| 151 | |||
| 152 | 'status' => 'ok', |
||
| 153 | 'message' => 'Updated Successfully', |
||
| 154 | 'template_url' => route('viewTemplate', ['templatename' => $templatename]), |
||
| 155 | |||
| 156 | ]); |
||
| 157 | } |
||
| 158 | } |
||
| 159 | |||
| 160 | public static function getTemplate($templateSlug) |
||
| 161 | { |
||
| 162 | $template = self::getTemplates() |
||
| 163 | ->where('template_slug', $templateSlug)->first(); |
||
| 164 | |||
| 165 | if (! is_null($template)) { |
||
| 166 | $template_view = self::$view_namespace.'::templates.'.$template->template_slug; |
||
| 167 | $template_plaintext_view = $template_view.'_plain_text'; |
||
| 168 | |||
| 169 | // return $template_plaintext_view; |
||
| 170 | |||
| 171 | if (View::exists($template_view)) { |
||
| 172 | $viewPath = View($template_view)->getPath(); |
||
| 173 | $textViewPath = View($template_plaintext_view)->getPath(); |
||
| 174 | |||
| 175 | $templateData = collect([ |
||
| 176 | 'template' => self::templateComponentReplace(file_get_contents($viewPath), true), |
||
| 177 | 'plain_text' => View::exists($template_plaintext_view) ? file_get_contents($textViewPath) : '', |
||
| 178 | 'slug' => $template->template_slug, |
||
| 179 | 'name' => $template->template_name, |
||
| 180 | 'description' => $template->template_description, |
||
| 181 | 'template_type' => $template->template_type, |
||
| 182 | 'template_view_name' => $template->template_view_name, |
||
| 183 | 'template_skeleton' => $template->template_skeleton, |
||
| 184 | ]); |
||
| 185 | |||
| 186 | return $templateData; |
||
| 187 | } |
||
| 188 | } |
||
| 189 | |||
| 190 | // return; |
||
| 191 | } |
||
| 192 | |||
| 193 | public static function getTemplates() |
||
| 198 | } |
||
| 199 | |||
| 200 | public static function createTemplate($request) |
||
| 201 | { |
||
| 202 | if (! preg_match("/^[a-zA-Z0-9-_\s]+$/", $request->template_name)) { |
||
| 203 | return response()->json([ |
||
| 204 | |||
| 205 | 'status' => 'error', |
||
| 206 | 'message' => 'Template name not valid', |
||
| 207 | |||
| 208 | ]); |
||
| 209 | } |
||
| 210 | |||
| 211 | $view = self::$view_namespace.'::templates.'.$request->template_name; |
||
| 212 | |||
| 213 | $templatename = Str::camel(preg_replace('/\s+/', '_', $request->template_name)); |
||
| 214 | |||
| 215 | if (! view()->exists($view) && ! self::getTemplates()->contains('template_slug', '=', $templatename)) { |
||
| 216 | self::saveTemplates(self::getTemplates() |
||
| 217 | ->push([ |
||
| 218 | 'template_name' => $request->template_name, |
||
| 219 | 'template_slug' => $templatename, |
||
| 220 | 'template_description' => $request->template_description, |
||
| 221 | 'template_type' => $request->template_type, |
||
| 222 | 'template_view_name' => $request->template_view_name, |
||
| 223 | 'template_skeleton' => $request->template_skeleton, |
||
| 224 | ])); |
||
| 225 | |||
| 226 | $dir = resource_path('views/vendor/'.self::$view_namespace.'/templates'); |
||
| 227 | |||
| 228 | if (! \File::isDirectory($dir)) { |
||
| 229 | \File::makeDirectory($dir, 0755, true); |
||
| 230 | } |
||
| 231 | |||
| 232 | file_put_contents($dir."/{$templatename}.blade.php", self::templateComponentReplace($request->content)); |
||
| 233 | |||
| 234 | file_put_contents($dir."/{$templatename}_plain_text.blade.php", $request->plain_text); |
||
| 235 | |||
| 236 | return response()->json([ |
||
| 237 | |||
| 238 | 'status' => 'ok', |
||
| 239 | 'message' => 'Template created<br> <small>Reloading...<small>', |
||
| 240 | 'template_url' => route('viewTemplate', ['templatename' => $templatename]), |
||
| 241 | |||
| 242 | ]); |
||
| 243 | } |
||
| 244 | |||
| 245 | return response()->json([ |
||
| 246 | |||
| 247 | 'status' => 'error', |
||
| 248 | 'message' => 'Template not created', |
||
| 249 | |||
| 250 | ]); |
||
| 251 | } |
||
| 252 | |||
| 253 | public static function getTemplateSkeletons() |
||
| 254 | { |
||
| 255 | return collect(config('maileclipse.skeletons')); |
||
| 256 | } |
||
| 257 | |||
| 258 | public static function getTemplateSkeleton($type, $name, $skeleton) |
||
| 259 | { |
||
| 260 | $skeletonView = self::$view_namespace."::skeletons.{$type}.{$name}.{$skeleton}"; |
||
| 261 | |||
| 262 | if (view()->exists($skeletonView)) { |
||
| 263 | $skeletonViewPath = View($skeletonView)->getPath(); |
||
| 264 | $templateContent = file_get_contents($skeletonViewPath); |
||
| 265 | |||
| 266 | return [ |
||
| 267 | 'type' => $type, |
||
| 268 | 'name' => $name, |
||
| 269 | 'skeleton' => $skeleton, |
||
| 270 | 'template' => self::templateComponentReplace($templateContent, true), |
||
| 271 | 'view' => $skeletonView, |
||
| 272 | 'view_path' => $skeletonViewPath, |
||
| 273 | ]; |
||
| 274 | } |
||
| 275 | } |
||
| 276 | |||
| 277 | protected static function templateComponentReplace($content, $reverse = false) |
||
| 278 | { |
||
| 279 | if ($reverse) { |
||
| 280 | $patterns = [ |
||
| 281 | '/@component/i', |
||
| 282 | '/@endcomponent/i', |
||
| 283 | '/@yield/', |
||
| 284 | '/@section/', |
||
| 285 | '/@endsection/', |
||
| 286 | '/@extends/', |
||
| 287 | '/@parent/', |
||
| 288 | '/@slot/', |
||
| 289 | '/@endslot/', |
||
| 290 | ]; |
||
| 291 | |||
| 292 | $replacements = [ |
||
| 293 | '[component]: # ', |
||
| 294 | '[endcomponent]: # ', |
||
| 295 | '[yield]: # ', |
||
| 296 | '[section]: # ', |
||
| 297 | '[endsection]: # ', |
||
| 298 | '[extends]: # ', |
||
| 299 | '[parent]: # ', |
||
| 300 | '[slot]: # ', |
||
| 301 | '[endslot]: # ', |
||
| 302 | ]; |
||
| 303 | } else { |
||
| 304 | $patterns = [ |
||
| 305 | '/\[component]:\s?#\s?/i', |
||
| 306 | '/\[endcomponent]:\s?#\s?/i', |
||
| 307 | '/\[yield]:\s?#\s?/i', |
||
| 308 | '/\[section]:\s?#\s?/i', |
||
| 309 | '/\[endsection]:\s?#\s?/i', |
||
| 310 | '/\[extends]:\s?#\s?/i', |
||
| 311 | '/\[parent]:\s?#\s?/i', |
||
| 312 | '/\[slot]:\s?#\s?/i', |
||
| 313 | '/\[endslot]:\s?#\s?/i', |
||
| 314 | ]; |
||
| 315 | |||
| 316 | $replacements = [ |
||
| 317 | '@component', |
||
| 318 | '@endcomponent', |
||
| 319 | '@yield', |
||
| 320 | '@section', |
||
| 321 | '@endsection', |
||
| 322 | '@extends', |
||
| 323 | '@parent', |
||
| 324 | '@slot', |
||
| 325 | '@endslot', |
||
| 326 | ]; |
||
| 327 | } |
||
| 328 | |||
| 329 | return preg_replace($patterns, $replacements, $content); |
||
| 330 | } |
||
| 331 | |||
| 332 | protected static function markdownedTemplate($viewPath) |
||
| 333 | { |
||
| 334 | $viewContent = file_get_contents($viewPath); |
||
| 335 | |||
| 336 | return self::templateComponentReplace($viewContent, true); |
||
| 337 | |||
| 338 | // return preg_replace($patterns, $replacements, $viewContent); |
||
| 339 | } |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Markdowned template view. |
||
| 343 | */ |
||
| 344 | public static function markdownedTemplateToView($save = true, $content = '', $viewPath = '', $template = false) |
||
| 345 | { |
||
| 346 | if ($template && View::exists(self::$view_namespace.'::templates.'.$viewPath)) { |
||
| 347 | $viewPath = View(self::$view_namespace.'::templates.'.$viewPath)->getPath(); |
||
| 348 | } |
||
| 349 | |||
| 350 | $replaced = self::templateComponentReplace($content); |
||
| 351 | |||
| 352 | if (! $save) { |
||
| 353 | return $replaced; |
||
| 354 | } |
||
| 355 | |||
| 356 | return file_put_contents($viewPath, $replaced) === false ? false : true; |
||
| 357 | } |
||
| 358 | |||
| 359 | public static function previewMarkdownViewContent($simpleview, $content, $viewName, $template = false, $namespace = null) |
||
| 360 | { |
||
| 361 | $previewtoset = self::markdownedTemplateToView(false, $content); |
||
| 362 | $dir = dirname(__FILE__, 2).'/resources/views/draft'; |
||
| 363 | $viewName = $template ? $viewName.'_template' : $viewName; |
||
| 364 | |||
| 365 | if (file_exists($dir)) { |
||
| 366 | file_put_contents($dir."/{$viewName}.blade.php", $previewtoset); |
||
| 367 | |||
| 368 | if ($template) { |
||
| 369 | $instance = null; |
||
| 370 | } else { |
||
| 371 | if (! is_null(self::handleMailableViewDataArgs($namespace))) { |
||
| 372 | $instance = self::handleMailableViewDataArgs($namespace); |
||
| 373 | } else { |
||
| 374 | $instance = new $namespace; |
||
| 375 | } |
||
| 376 | } |
||
| 377 | |||
| 378 | return self::renderPreview($simpleview, self::$view_namespace.'::draft.'.$viewName, $template, $instance); |
||
| 379 | } |
||
| 380 | |||
| 381 | return false; |
||
| 382 | } |
||
| 383 | |||
| 384 | public static function previewMarkdownHtml($instance, $view) |
||
| 387 | } |
||
| 388 | |||
| 389 | public static function getMailableTemplateData($mailableName) |
||
| 390 | { |
||
| 391 | $mailable = self::getMailable('name', $mailableName); |
||
| 392 | |||
| 393 | if ($mailable->isEmpty()) { |
||
| 394 | return false; |
||
| 395 | } |
||
| 418 | } |
||
| 419 | |||
| 420 | public static function generateMailable($request = null) |
||
| 421 | { |
||
| 422 | $name = self::generateClassName($request->input('name')); |
||
| 423 | |||
| 424 | if ($name === false) { |
||
| 425 | return response()->json([ |
||
| 426 | 'status' => 'error', |
||
| 427 | 'message' => 'Wrong name format.', |
||
| 428 | ]); |
||
| 429 | } |
||
| 430 | |||
| 431 | if (! self::getMailable('name', $name)->isEmpty() && ! $request->has('force')) { |
||
| 432 | // return redirect()->route('createMailable')->with('error', 'mailable already exists! to overide it enable force option.'); |
||
| 433 | // |
||
| 434 | return response()->json([ |
||
| 435 | 'status' => 'error', |
||
| 436 | 'message' => 'This mailable name already exists. names should be unique! to override it, enable "force" option.', |
||
| 437 | ]); |
||
| 438 | } |
||
| 439 | |||
| 440 | if (strtolower($name) === 'mailable') { |
||
| 441 | return response()->json([ |
||
| 442 | 'status' => 'error', |
||
| 443 | 'message' => 'You cannot use this name', |
||
| 444 | ]); |
||
| 445 | } |
||
| 446 | |||
| 447 | $params = collect([ |
||
| 448 | 'name' => $name, |
||
| 449 | ]); |
||
| 450 | |||
| 451 | if ($request->input('markdown')) { |
||
| 452 | $params->put('--markdown', $request->markdown); |
||
| 453 | } |
||
| 454 | |||
| 455 | if ($request->has('force')) { |
||
| 456 | $params->put('--force', true); |
||
| 457 | } |
||
| 458 | |||
| 459 | $exitCode = Artisan::call('make:mail', $params->all()); |
||
| 460 | |||
| 461 | if ($exitCode > -1) { |
||
| 462 | |||
| 463 | // return redirect()->route('mailableList'); |
||
| 464 | // |
||
| 465 | return response()->json([ |
||
| 466 | |||
| 467 | 'status' => 'ok', |
||
| 468 | 'message' => 'Mailable Created<br> <small>Reloading...<small>', |
||
| 469 | |||
| 470 | ]); |
||
| 471 | } |
||
| 472 | } |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Get mailables list. |
||
| 476 | * |
||
| 477 | * @return array |
||
| 478 | */ |
||
| 479 | protected static function mailablesList() |
||
| 574 | } |
||
| 575 | } |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Handle Mailable Constructor arguments and pass the fake ones. |
||
| 579 | */ |
||
| 580 | public static function handleMailableViewDataArgs($mailable) |
||
| 581 | { |
||
| 582 | if (method_exists($mailable, '__construct')) { |
||
| 583 | $reflection = new ReflectionClass($mailable); |
||
| 584 | |||
| 585 | $params = $reflection->getConstructor()->getParameters(); |
||
| 586 | |||
| 587 | DB::beginTransaction(); |
||
| 588 | |||
| 589 | $eloquentFactory = app(EloquentFactory::class); |
||
| 590 | |||
| 591 | $args = collect($params)->map(function ($param) { |
||
| 592 | if ($param->getType() !== null) { |
||
| 593 | if (class_exists($param->getType()->getName())) { |
||
| 594 | $parameters = [ |
||
| 595 | 'is_instance' => true, |
||
| 596 | 'instance' => $param->getType()->getName(), |
||
| 597 | ]; |
||
| 598 | } elseif ($param->getType()->getName() == 'array') { |
||
| 599 | $parameters = [ |
||
| 600 | 'is_array' => true, |
||
| 601 | 'arg' => $param->getName(), |
||
| 602 | ]; |
||
| 603 | } else { |
||
| 604 | $parameters = $param->name; |
||
| 605 | } |
||
| 606 | |||
| 607 | return $parameters; |
||
| 608 | } |
||
| 609 | |||
| 610 | return $param->name; |
||
| 611 | }); |
||
| 612 | |||
| 613 | $resolvedTypeHints = []; |
||
| 614 | |||
| 615 | foreach ($args->all() as $arg) { |
||
| 616 | if (is_array($arg)) { |
||
| 617 | if (isset($arg['is_instance'])) { |
||
| 618 | |||
| 619 | $model = $arg['instance']; |
||
| 620 | |||
| 621 | $resolvedTypeHints = self::resolveFactory($eloquentFactory, $model, $resolvedTypeHints); |
||
| 622 | |||
| 623 | } elseif (isset($arg['is_array'])) { |
||
| 624 | $resolvedTypeHints[] = []; |
||
| 625 | } else { |
||
| 626 | return; |
||
| 627 | } |
||
| 628 | } else { |
||
| 629 | $resolvedTypeHints[] = self::getMissingParams($arg, $params); |
||
| 630 | } |
||
| 631 | } |
||
| 632 | |||
| 633 | $reflector = new ReflectionClass($mailable); |
||
| 634 | |||
| 635 | if (!$args->isEmpty()) { |
||
| 636 | $foo = $reflector->newInstanceArgs($resolvedTypeHints); |
||
| 637 | |||
| 638 | return $foo; |
||
| 639 | } |
||
| 640 | |||
| 641 | DB::rollBack(); |
||
| 642 | } |
||
| 643 | } |
||
| 644 | |||
| 645 | /** |
||
| 646 | * Gets any missing params that may not be collectable in the reflection. |
||
| 647 | * |
||
| 648 | * @param string $arg the argument string|array |
||
| 649 | * @param array $params the reflection param list |
||
| 650 | * |
||
| 651 | * @return array|string|\ReeceM\Mocker\Mocked |
||
| 652 | */ |
||
| 653 | private static function getMissingParams($arg, $params) |
||
| 654 | { |
||
| 655 | /** |
||
| 656 | * Determine if a builtin type can be found. |
||
| 657 | * Not a string or object as a Mocked::class can work there. |
||
| 658 | * |
||
| 659 | * getName() is undocumented alternative to casting to string. |
||
| 660 | * https://www.php.net/manual/en/class.reflectiontype.php#124658 |
||
| 661 | * |
||
| 662 | * @var \ReflectionType $reflection |
||
| 663 | */ |
||
| 664 | $reflection = collect($params)->where('name', $arg)->first()->getType(); |
||
| 665 | |||
| 666 | if (version_compare(phpversion(), '7.1', '>=')) { |
||
| 667 | $type = ! is_null($reflection) |
||
| 668 | ? self::TYPES[$reflection->getName()] |
||
| 669 | : null; |
||
| 670 | } else { |
||
| 671 | $type = ! is_null($reflection) |
||
| 672 | ? self::TYPES[/** @scrutinizer ignore-deprecated */ $reflection->__toString()] |
||
| 673 | : null; |
||
| 674 | } |
||
| 675 | |||
| 676 | try { |
||
| 677 | return ! is_null($type) |
||
| 678 | ? $type |
||
| 679 | : new Mocked($arg, \ReeceM\Mocker\Utils\VarStore::singleton()); |
||
| 680 | } catch (\Exception $e) { |
||
| 681 | return $arg; |
||
| 682 | } |
||
| 683 | } |
||
| 684 | |||
| 685 | private static function getMailableViewData($mailable, $mailable_data) |
||
| 686 | { |
||
| 687 | $traitProperties = []; |
||
| 688 | |||
| 689 | $data = new ReflectionClass($mailable); |
||
| 690 | |||
| 691 | foreach ($data->getTraits() as $trait) { |
||
| 692 | foreach ($trait->getProperties(ReflectionProperty::IS_PUBLIC) as $traitProperty) { |
||
| 693 | $traitProperties[] = $traitProperty->name; |
||
| 694 | } |
||
| 695 | } |
||
| 696 | |||
| 697 | $properties = $data->getProperties(ReflectionProperty::IS_PUBLIC); |
||
| 698 | $allProps = []; |
||
| 699 | |||
| 700 | foreach ($properties as $prop) { |
||
| 701 | if ($prop->class == $data->getName() || $prop->class == get_parent_class($data->getName()) && |
||
| 702 | get_parent_class($data->getName()) != 'Illuminate\Mail\Mailable' && ! $prop->isStatic()) { |
||
| 703 | $allProps[] = $prop->name; |
||
| 704 | } |
||
| 705 | } |
||
| 706 | |||
| 707 | $obj = self::buildMailable($mailable); |
||
| 708 | |||
| 709 | if (is_null($obj)) { |
||
| 710 | $obj = []; |
||
| 711 | |||
| 712 | return collect($obj); |
||
| 713 | } |
||
| 714 | |||
| 715 | $classProps = array_diff($allProps, $traitProperties); |
||
| 716 | |||
| 717 | $withFuncData = collect($obj->viewData)->keys(); |
||
| 718 | |||
| 719 | $mailableData = collect($classProps)->merge($withFuncData); |
||
| 720 | |||
| 721 | $data = $mailableData->map(function ($parameter) use ($mailable_data) { |
||
| 722 | return [ |
||
| 723 | 'key' => $parameter, |
||
| 724 | 'value' => property_exists($mailable_data, $parameter) ? $mailable_data->$parameter : null, |
||
| 725 | 'data' => property_exists($mailable_data, $parameter) ? self::viewDataInspect($mailable_data->$parameter) : null, |
||
| 726 | ]; |
||
| 727 | }); |
||
| 728 | |||
| 729 | return $data->all(); |
||
| 730 | } |
||
| 731 | |||
| 732 | protected static function viewDataInspect($param) |
||
| 748 | ]; |
||
| 749 | } |
||
| 750 | } |
||
| 751 | |||
| 752 | protected static function mailable_exists($mailable) |
||
| 753 | { |
||
| 754 | if (! class_exists($mailable)) { |
||
| 755 | return false; |
||
| 756 | } |
||
| 757 | |||
| 758 | return true; |
||
| 759 | } |
||
| 760 | |||
| 761 | protected static function getMarkdownViewName($mailable) |
||
| 762 | { |
||
| 763 | if ($mailable === null) { |
||
| 764 | return; |
||
| 765 | } |
||
| 766 | |||
| 767 | $reflection = new ReflectionClass($mailable); |
||
| 768 | |||
| 769 | $property = $reflection->getProperty('markdown'); |
||
| 770 | |||
| 771 | $property->setAccessible(true); |
||
| 772 | |||
| 773 | return $property->getValue($mailable); |
||
| 774 | } |
||
| 775 | |||
| 776 | public static function buildMailable($instance, $type = 'call') |
||
| 777 | { |
||
| 778 | if ($type == 'call') { |
||
| 779 | if (! is_null(self::handleMailableViewDataArgs($instance))) { |
||
| 780 | return Container::getInstance()->call([self::handleMailableViewDataArgs($instance), 'build']); |
||
| 781 | } |
||
| 782 | |||
| 783 | return Container::getInstance()->call([new $instance, 'build']); |
||
| 784 | } |
||
| 785 | |||
| 786 | return Container::getInstance()->make($instance); |
||
| 787 | } |
||
| 788 | |||
| 789 | public static function renderPreview($simpleview, $view, $template = false, $instance = null) |
||
| 790 | { |
||
| 791 | if (! View::exists($view)) { |
||
| 792 | return; |
||
| 793 | } |
||
| 794 | |||
| 795 | if (! $template) { |
||
| 796 | $obj = self::buildMailable($instance); |
||
| 797 | $viewData = $obj->viewData; |
||
| 798 | $_data = array_merge($instance->buildViewData(), $viewData); |
||
| 799 | |||
| 800 | foreach ($_data as $key => $value) { |
||
| 801 | if (! is_object($value)) { |
||
| 802 | $_data[$key] = '<span class="maileclipse-key" title="Variable">'.$key.'</span>'; |
||
| 803 | } |
||
| 804 | } |
||
| 805 | } else { |
||
| 806 | $_data = []; |
||
| 807 | } |
||
| 808 | |||
| 809 | $_view = $view; |
||
| 810 | |||
| 811 | try { |
||
| 812 | if ($simpleview) { |
||
| 813 | return htmlspecialchars_decode(view($_view, $_data)->render()); |
||
| 814 | } |
||
| 815 | |||
| 816 | $_md = self::buildMailable(Markdown::class, 'make'); |
||
| 817 | |||
| 818 | return htmlspecialchars_decode($_md->render($_view, $_data)); |
||
| 819 | } catch (ErrorException $e) { |
||
| 820 | $error = '<div class="alert alert-warning"> |
||
| 821 | <h5 class="alert-heading">Error:</h5> |
||
| 822 | <p>'.$e->getMessage().'</p> |
||
| 823 | </div>'; |
||
| 824 | |||
| 825 | if ($template) { |
||
| 826 | $error .= '<div class="alert alert-info"> |
||
| 827 | <h5 class="alert-heading">Notice:</h5> |
||
| 828 | <p>You can\'t add variables within a template editor because they are undefined until you bind the template with a mailable that actually has data.</p> |
||
| 829 | </div>'; |
||
| 830 | } |
||
| 831 | |||
| 832 | return $error; |
||
| 833 | } |
||
| 834 | } |
||
| 835 | |||
| 836 | /** |
||
| 837 | * Class name has to satisfy those rules. |
||
| 838 | * |
||
| 839 | * https://www.php.net/manual/en/language.oop5.basic.php#language.oop5.basic.class |
||
| 840 | * https://www.php.net/manual/en/reserved.keywords.php |
||
| 841 | * |
||
| 842 | * @param $input |
||
| 843 | * @return string|false class name or false on failure |
||
| 844 | */ |
||
| 845 | public static function generateClassName($input) |
||
| 871 | } |
||
| 872 | |||
| 873 | /** |
||
| 874 | * @param $eloquentFactory |
||
| 875 | * @param $model |
||
| 876 | * @param array $resolvedTypeHints |
||
| 877 | * @return array |
||
| 878 | */ |
||
| 879 | private static function resolveFactory($eloquentFactory, $model, array $resolvedTypeHints): array |
||
| 898 | } |
||
| 899 | } |
||
| 900 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.