Вход Регистрация
Файл: vendor/laravel/framework/src/Illuminate/Queue/SerializesAndRestoresModelIdentifiers.php
Строк: 154
<?php

namespace IlluminateQueue;

use 
IlluminateContractsDatabaseModelIdentifier;
use 
IlluminateContractsQueueQueueableCollection;
use 
IlluminateContractsQueueQueueableEntity;
use 
IlluminateDatabaseEloquentCollection as EloquentCollection;
use 
IlluminateDatabaseEloquentRelationsConcernsAsPivot;
use 
IlluminateDatabaseEloquentRelationsPivot;

trait 
SerializesAndRestoresModelIdentifiers
{
    
/**
     * Get the property value prepared for serialization.
     *
     * @param  mixed  $value
     * @param  bool  $withRelations
     * @return mixed
     */
    
protected function getSerializedPropertyValue($value$withRelations true)
    {
        if (
$value instanceof QueueableCollection) {
            return (new 
ModelIdentifier(
                
$value->getQueueableClass(),
                
$value->getQueueableIds(),
                
$withRelations $value->getQueueableRelations() : [],
                
$value->getQueueableConnection()
            ))->
useCollectionClass(
                (
$collectionClass get_class($value)) !== EloquentCollection::class
                    ? 
$collectionClass
                    
null
            
);
        }

        if (
$value instanceof QueueableEntity) {
            return new 
ModelIdentifier(
                
get_class($value),
                
$value->getQueueableId(),
                
$withRelations $value->getQueueableRelations() : [],
                
$value->getQueueableConnection()
            );
        }

        return 
$value;
    }

    
/**
     * Get the restored property value after deserialization.
     *
     * @param  mixed  $value
     * @return mixed
     */
    
protected function getRestoredPropertyValue($value)
    {
        if (! 
$value instanceof ModelIdentifier) {
            return 
$value;
        }

        return 
is_array($value->id)
                ? 
$this->restoreCollection($value)
                : 
$this->restoreModel($value);
    }

    
/**
     * Restore a queueable collection instance.
     *
     * @param  IlluminateContractsDatabaseModelIdentifier  $value
     * @return IlluminateDatabaseEloquentCollection
     */
    
protected function restoreCollection($value)
    {
        if (! 
$value->class || count($value->id) === 0) {
            return ! 
is_null($value->collectionClass ?? null)
                ? new 
$value->collectionClass
                
: new EloquentCollection;
        }

        
$collection $this->getQueryForModelRestoration(
            (new 
$value->class)->setConnection($value->connection), $value->id
        
)->useWritePdo()->get();

        if (
is_a($value->classPivot::class, true) ||
            
in_array(AsPivot::class, class_uses($value->class))) {
            return 
$collection;
        }

        
$collection $collection->keyBy->getKey();

        
$collectionClass get_class($collection);

        return new 
$collectionClass(
            
collect($value->id)->map(function ($id) use ($collection) {
                return 
$collection[$id] ?? null;
            })->
filter()
        );
    }

    
/**
     * Restore the model from the model identifier instance.
     *
     * @param  IlluminateContractsDatabaseModelIdentifier  $value
     * @return IlluminateDatabaseEloquentModel
     */
    
public function restoreModel($value)
    {
        return 
$this->getQueryForModelRestoration(
            (new 
$value->class)->setConnection($value->connection), $value->id
        
)->useWritePdo()->firstOrFail()->load($value->relations ?? []);
    }

    
/**
     * Get the query for model restoration.
     *
     * @param  IlluminateDatabaseEloquentModel  $model
     * @param  array|int  $ids
     * @return IlluminateDatabaseEloquentBuilder
     */
    
protected function getQueryForModelRestoration($model$ids)
    {
        return 
$model->newQueryForRestoration($ids);
    }
}
Онлайн: 0
Реклама