Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3.x] Fix typed properties issues #879

Merged
merged 1 commit into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 27 additions & 28 deletions src/ExtractTags.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,11 @@
namespace Laravel\Telescope;

use Illuminate\Broadcasting\BroadcastEvent;
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Events\CallQueuedListener;
use Illuminate\Mail\SendQueuedMailable;
use Illuminate\Notifications\SendQueuedNotifications;
use Illuminate\Support\Arr;
use ReflectionClass;
use stdClass;

Expand Down Expand Up @@ -48,6 +47,22 @@ public static function fromJob($job)
})->all();
}

/**
* Resolve the value.
*
* @param mixed $value
* @return \Illuminate\Support\Collection|null
*/
protected static function resolveValue($value)
{
switch (true) {
case $value instanceof Model:
return collect([$value]);
case $value instanceof Collection:
return $value->flatten();
}
}

/**
* Determine the tags for the given array.
*
Expand All @@ -56,15 +71,9 @@ public static function fromJob($job)
*/
public static function fromArray(array $data)
{
$models = collect($data)->map(function ($value) {
if ($value instanceof Model) {
return [$value];
} elseif ($value instanceof EloquentCollection) {
return $value->flatten();
}
})->collapse()->filter();

return $models->map(function ($model) {
return collect($data)->map(function ($value) {
return static::resolveValue($value);
})->collapse()->filter()->map(function ($model) {
return FormatModel::given($model);
})->all();
}
Expand Down Expand Up @@ -101,7 +110,7 @@ protected static function tagsForListener($job)
* Determine tags for the given job.
*
* @param array $targets
* @return mixed
* @return array
*/
protected static function explicitTags(array $targets)
{
Expand Down Expand Up @@ -140,25 +149,15 @@ protected static function targetsFor($job)
*/
protected static function modelsFor(array $targets)
{
$models = [];

foreach ($targets as $target) {
$models[] = collect(
(new ReflectionClass($target))->getProperties()
)->map(function ($property) use ($target) {
return collect($targets)->map(function ($target) {
return collect((new ReflectionClass($target))->getProperties())->map(function ($property) use ($target) {
$property->setAccessible(true);

$value = $property->getValue($target);

if ($value instanceof Model) {
return [$value];
} elseif ($value instanceof EloquentCollection) {
return $value->flatten();
if (PHP_VERSION_ID < 70400 || ! is_object($target) || $property->isInitialized($target)) {
return static::resolveValue($property->getValue($target));
}
})->collapse()->filter()->all();
}

return collect(Arr::collapse($models))->unique();
})->collapse()->filter();
})->collapse()->unique();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/FormatModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class FormatModel
* Format the given model to a readable string.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return array
* @return string
*/
public static function given($model)
{
Expand Down