Qoraiche /
laravel-mail-editor
| 1 | <?php |
||||||
| 2 | |||||||
| 3 | namespace Qoraiche\MailEclipse; |
||||||
| 4 | |||||||
| 5 | use Illuminate\Support\Facades\View; |
||||||
| 6 | use Qoraiche\MailEclipse\Facades\MailEclipse; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 7 | use RecursiveDirectoryIterator; |
||||||
| 8 | use RecursiveIteratorIterator; |
||||||
| 9 | use ReflectionClass; |
||||||
| 10 | use RegexIterator; |
||||||
| 11 | |||||||
| 12 | class MailEclipseMailables |
||||||
| 13 | { |
||||||
| 14 | public function __construct() |
||||||
| 15 | { |
||||||
| 16 | // |
||||||
| 17 | } |
||||||
| 18 | |||||||
| 19 | public static function get(string $name, string $key = 'name') |
||||||
| 20 | { |
||||||
| 21 | return collect(self::all())->where($key, $name); |
||||||
| 22 | } |
||||||
| 23 | |||||||
| 24 | /** |
||||||
| 25 | * Get mailables list. |
||||||
| 26 | * |
||||||
| 27 | * @return array |
||||||
| 28 | */ |
||||||
| 29 | public static function all() |
||||||
| 30 | { |
||||||
| 31 | $fqcns = []; |
||||||
| 32 | |||||||
| 33 | if (! file_exists(config('maileclipse.mailables_dir'))) { |
||||||
| 34 | return; |
||||||
| 35 | } else { |
||||||
| 36 | $allFiles = new RecursiveIteratorIterator(new RecursiveDirectoryIterator(config('maileclipse.mailables_dir'))); |
||||||
| 37 | $phpFiles = new RegexIterator($allFiles, '/\.php$/'); |
||||||
| 38 | $i = 0; |
||||||
| 39 | |||||||
| 40 | foreach ($phpFiles as $phpFile) { |
||||||
| 41 | $i++; |
||||||
| 42 | $content = file_get_contents($phpFile->getRealPath()); |
||||||
| 43 | $tokens = token_get_all($content); |
||||||
| 44 | $namespace = ''; |
||||||
| 45 | for ($index = 0; isset($tokens[$index]); $index++) { |
||||||
| 46 | if (! isset($tokens[$index][0])) { |
||||||
| 47 | continue; |
||||||
| 48 | } |
||||||
| 49 | if (T_NAMESPACE === $tokens[$index][0]) { |
||||||
| 50 | $index += 2; // Skip namespace keyword and whitespace |
||||||
| 51 | while (isset($tokens[$index]) && is_array($tokens[$index])) { |
||||||
| 52 | $namespace .= $tokens[$index++][1]; |
||||||
| 53 | } |
||||||
| 54 | } |
||||||
| 55 | if (T_CLASS === $tokens[$index][0] && T_WHITESPACE === $tokens[$index + 1][0] && T_STRING === $tokens[$index + 2][0]) { |
||||||
| 56 | $index += 2; // Skip class keyword and whitespace |
||||||
| 57 | |||||||
| 58 | [$name, $extension] = explode('.', $phpFile->getFilename()); |
||||||
| 59 | |||||||
| 60 | $mailableClass = $namespace.'\\'.$tokens[$index][1]; |
||||||
| 61 | |||||||
| 62 | if (! MailEclipse::mailable_exists($mailableClass)) { |
||||||
|
0 ignored issues
–
show
The method
mailable_exists() does not exist on Qoraiche\MailEclipse\Facades\MailEclipse. Since you implemented __callStatic, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 63 | continue; |
||||||
| 64 | } |
||||||
| 65 | |||||||
| 66 | $reflector = new ReflectionClass($mailableClass); |
||||||
| 67 | |||||||
| 68 | if ($reflector->isAbstract()) { |
||||||
| 69 | continue; |
||||||
| 70 | } |
||||||
| 71 | |||||||
| 72 | $mailable_data = MailEclipse::buildMailable($mailableClass); |
||||||
| 73 | |||||||
| 74 | if (! is_null(MailEclipse::handleMailableViewDataArgs($mailableClass))) { |
||||||
| 75 | $mailable_view_data = MailEclipse::getMailableViewData(MailEclipse::handleMailableViewDataArgs($mailableClass), $mailable_data); |
||||||
|
0 ignored issues
–
show
The method
getMailableViewData() does not exist on Qoraiche\MailEclipse\Facades\MailEclipse. Since you implemented __callStatic, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 76 | } else { |
||||||
| 77 | $mailable_view_data = MailEclipse::getMailableViewData(new $mailableClass, $mailable_data); |
||||||
| 78 | } |
||||||
| 79 | |||||||
| 80 | $fqcns[$i]['data'] = $mailable_data; |
||||||
| 81 | $fqcns[$i]['markdown'] = MailEclipse::getMarkdownViewName($mailable_data); |
||||||
|
0 ignored issues
–
show
The method
getMarkdownViewName() does not exist on Qoraiche\MailEclipse\Facades\MailEclipse. Since you implemented __callStatic, consider adding a @method annotation.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
Loading history...
|
|||||||
| 82 | $fqcns[$i]['name'] = $name; |
||||||
| 83 | $fqcns[$i]['namespace'] = $mailableClass; |
||||||
| 84 | $fqcns[$i]['filename'] = $phpFile->getFilename(); |
||||||
| 85 | $fqcns[$i]['modified'] = $phpFile->getCTime(); |
||||||
| 86 | $fqcns[$i]['viewed'] = $phpFile->getATime(); |
||||||
| 87 | $fqcns[$i]['view_data'] = $mailable_view_data; |
||||||
| 88 | // $fqcns[$i]['view_data'] = []; |
||||||
| 89 | $fqcns[$i]['path_name'] = $phpFile->getPathname(); |
||||||
| 90 | $fqcns[$i]['readable'] = $phpFile->isReadable(); |
||||||
| 91 | $fqcns[$i]['writable'] = $phpFile->isWritable(); |
||||||
| 92 | $fqcns[$i]['view_path'] = null; |
||||||
| 93 | $fqcns[$i]['text_view_path'] = null; |
||||||
| 94 | |||||||
| 95 | if (! is_null($fqcns[$i]['markdown']) && View::exists($fqcns[$i]['markdown'])) { |
||||||
| 96 | $fqcns[$i]['view_path'] = View($fqcns[$i]['markdown'])->getPath(); |
||||||
| 97 | } |
||||||
| 98 | |||||||
| 99 | if (! is_null($fqcns[$i]['data'])) { |
||||||
| 100 | if (! is_null($fqcns[$i]['data']->view) && View::exists($fqcns[$i]['data']->view)) { |
||||||
| 101 | $fqcns[$i]['view_path'] = View($fqcns[$i]['data']->view)->getPath(); |
||||||
| 102 | } |
||||||
| 103 | |||||||
| 104 | if (! is_null($fqcns[$i]['data']->textView) && View::exists($fqcns[$i]['data']->textView)) { |
||||||
| 105 | $fqcns[$i]['text_view_path'] = View($fqcns[$i]['data']->textView)->getPath(); |
||||||
| 106 | $fqcns[$i]['text_view'] = $fqcns[$i]['data']->textView; |
||||||
| 107 | } |
||||||
| 108 | } |
||||||
| 109 | |||||||
| 110 | // break if you have one class per file (psr-4 compliant) |
||||||
| 111 | // otherwise you'll need to handle class constants (Foo::class) |
||||||
| 112 | break; |
||||||
| 113 | } |
||||||
| 114 | } |
||||||
| 115 | } |
||||||
| 116 | |||||||
| 117 | $collection = collect($fqcns)->map(function ($mailable) { |
||||||
| 118 | return $mailable; |
||||||
| 119 | })->reject(function ($object) { |
||||||
| 120 | return ! method_exists($object['namespace'], 'build'); |
||||||
| 121 | }); |
||||||
| 122 | |||||||
| 123 | return $collection; |
||||||
|
0 ignored issues
–
show
|
|||||||
| 124 | } |
||||||
| 125 | } |
||||||
| 126 | } |
||||||
| 127 |
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: