Class luya\admin\proxy\ClientTransfer

Inheritanceluya\admin\proxy\ClientTransfer » yii\base\BaseObject
Implementsyii\base\Configurable
Uses Traitsluya\traits\CacheableTrait
Available since version1.0.0
Source Code https://github.com/luyadev/luya-module-admin/blob/master/src/proxy/ClientTransfer.php

Client Transfer Process

For admin/proxy usage see {{luya\admin\commands\ProxyController}}

Public Methods

Hide inherited methods

Method Description Defined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\base\BaseObject
__get() Returns the value of an object property. yii\base\BaseObject
__isset() Checks if a property is set, i.e. defined and not null. yii\base\BaseObject
__set() Sets value of an object property. yii\base\BaseObject
__unset() Sets an object property to null. yii\base\BaseObject
canGetProperty() Returns a value indicating whether a property can be read. yii\base\BaseObject
canSetProperty() Returns a value indicating whether a property can be set. yii\base\BaseObject
className() Returns the fully qualified name of this class. yii\base\BaseObject
deleteHasCache() Remove a value from the cache if caching is enabled. luya\traits\CacheableTrait
flushHasCache() Deletes all values from cache. luya\traits\CacheableTrait
getHasCache() Get the caching data if caching is allowed and there is any data stored for this key. luya\traits\CacheableTrait
getOrSetHasCache() Method combines both setHasCache() and getHasCache() methods to retrieve value identified by a $key, or to store the result of $closure execution if there is no cache available for the $key. luya\traits\CacheableTrait
hasMethod() Returns a value indicating whether a method is defined. yii\base\BaseObject
hasProperty() Returns a value indicating whether a property is defined. yii\base\BaseObject
init() Initializes the object. luya\admin\proxy\ClientTransfer
isCachable() Check if the current configuration of the application and the property allows a caching of the language container data. luya\traits\CacheableTrait
setHasCache() Store cache data for a specific key if caching is enabled in this application. luya\traits\CacheableTrait
start() luya\admin\proxy\ClientTransfer
storageUpload() Upload file to storage. luya\admin\proxy\ClientTransfer

Protected Methods

Hide inherited methods

Method Description Defined By
startDb() Start DB Sync luya\admin\proxy\ClientTransfer
startFiles() Start Storage Files Sync luya\admin\proxy\ClientTransfer
startImages() Start Storage Images Sync luya\admin\proxy\ClientTransfer

Constants

Hide inherited constants

Constant Value Description Defined By
ONLY_DB 'db' luya\admin\proxy\ClientTransfer
ONLY_STORAGE 'storage' luya\admin\proxy\ClientTransfer

Property Details

Hide inherited properties

$build public property
$only public property
public $only null

Method Details

Hide inherited methods

__call() public method

Defined in: yii\base\BaseObject::__call()

Calls the named method which is not a class method.

Do not call this method directly as it is a PHP magic method that will be implicitly called when an unknown method is being invoked.

public mixed __call ( $name, $params )
$name string

The method name

$params array

Method parameters

return mixed

The method return value

throws yii\base\UnknownMethodException

when calling unknown method

                public function __call($name, $params)
{
    throw new UnknownMethodException('Calling unknown method: ' . get_class($this) . "::$name()");
}

            
__construct() public method

Defined in: yii\base\BaseObject::__construct()

Constructor.

The default implementation does two things:

  • Initializes the object with the given configuration $config.
  • Call init().

If this method is overridden in a child class, it is recommended that

  • the last parameter of the constructor is a configuration array, like $config here.
  • call the parent implementation at the end of the constructor.
public void __construct ( $config = [] )
$config array

Name-value pairs that will be used to initialize the object properties

                public function __construct($config = [])
{
    if (!empty($config)) {
        Yii::configure($this, $config);
    }
    $this->init();
}

            
__get() public method

Defined in: yii\base\BaseObject::__get()

Returns the value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $value = $object->property;.

See also __set().

public mixed __get ( $name )
$name string

The property name

return mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is write-only

                public function __get($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter();
    } elseif (method_exists($this, 'set' . $name)) {
        throw new InvalidCallException('Getting write-only property: ' . get_class($this) . '::' . $name);
    }
    throw new UnknownPropertyException('Getting unknown property: ' . get_class($this) . '::' . $name);
}

            
__isset() public method

Defined in: yii\base\BaseObject::__isset()

Checks if a property is set, i.e. defined and not null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing isset($object->property).

Note that if the property is not defined, false will be returned.

See also https://www.php.net/manual/en/function.isset.php.

public boolean __isset ( $name )
$name string

The property name or the event name

return boolean

Whether the named property is set (not null).

                public function __isset($name)
{
    $getter = 'get' . $name;
    if (method_exists($this, $getter)) {
        return $this->$getter() !== null;
    }
    return false;
}

            
__set() public method

Defined in: yii\base\BaseObject::__set()

Sets value of an object property.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing $object->property = $value;.

See also __get().

public void __set ( $name, $value )
$name string

The property name or the event name

$value mixed

The property value

throws yii\base\UnknownPropertyException

if the property is not defined

throws yii\base\InvalidCallException

if the property is read-only

                public function __set($name, $value)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

            
__unset() public method

Defined in: yii\base\BaseObject::__unset()

Sets an object property to null.

Do not call this method directly as it is a PHP magic method that will be implicitly called when executing unset($object->property).

Note that if the property is not defined, this method will do nothing. If the property is read-only, it will throw an exception.

See also https://www.php.net/manual/en/function.unset.php.

public void __unset ( $name )
$name string

The property name

throws yii\base\InvalidCallException

if the property is read only.

                public function __unset($name)
{
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        $this->$setter(null);
    } elseif (method_exists($this, 'get' . $name)) {
        throw new InvalidCallException('Unsetting read-only property: ' . get_class($this) . '::' . $name);
    }
}

            
canGetProperty() public method

Defined in: yii\base\BaseObject::canGetProperty()

Returns a value indicating whether a property can be read.

A property is readable if:

  • the class has a getter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canSetProperty().

public boolean canGetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be read

                public function canGetProperty($name, $checkVars = true)
{
    return method_exists($this, 'get' . $name) || $checkVars && property_exists($this, $name);
}

            
canSetProperty() public method

Defined in: yii\base\BaseObject::canSetProperty()

Returns a value indicating whether a property can be set.

A property is writable if:

  • the class has a setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also canGetProperty().

public boolean canSetProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property can be written

                public function canSetProperty($name, $checkVars = true)
{
    return method_exists($this, 'set' . $name) || $checkVars && property_exists($this, $name);
}

            
className() public static method
Deprecated since 2.0.14. On PHP >=5.5, use ::class instead.

Defined in: yii\base\BaseObject::className()

Returns the fully qualified name of this class.

public static string className ( )
return string

The fully qualified name of this class.

                public static function className()
{
    return get_called_class();
}

            
deleteHasCache() public method

Defined in: luya\traits\CacheableTrait::deleteHasCache()

Remove a value from the cache if caching is enabled.

public boolean deleteHasCache ( $key )
$key string|array

The cache identifier

return boolean

Whether delete of key has been success or not

                public function deleteHasCache($key)
{
    if ($this->isCachable()) {
        return Yii::$app->cache->delete($key);
    }
    
    return false;
}

            
flushHasCache() public method

Defined in: luya\traits\CacheableTrait::flushHasCache()

Deletes all values from cache.

public boolean flushHasCache ( )
return boolean

Whether the flush operation was successful.

                public function flushHasCache()
{
    if ($this->isCachable()) {
        return Yii::$app->cache->flush();
    }
    
    return false;
}

            
getHasCache() public method

Defined in: luya\traits\CacheableTrait::getHasCache()

Get the caching data if caching is allowed and there is any data stored for this key.

public mixed|boolean getHasCache ( $key )
$key string|array

The identifiere key, can be a string or an array which will be calculated.

return mixed|boolean

Returns the data, if not found returns false.

                public function getHasCache($key)
{
    if ($this->isCachable()) {
        $data = Yii::$app->cache->get($key);
        
        $enumKey = (is_array($key)) ? implode(",", $key) : $key;
        
        if ($data) {
            Yii::debug("Cacheable trait key '$enumKey' successfully loaded from cache.", __METHOD__);
            return $data;
        }
        Yii::debug("Cacheable trait key '$enumKey' has not been found in cache.", __METHOD__);
    }

    return false;
}

            
getOrSetHasCache() public method

Defined in: luya\traits\CacheableTrait::getOrSetHasCache()

Method combines both setHasCache() and getHasCache() methods to retrieve value identified by a $key, or to store the result of $closure execution if there is no cache available for the $key.

Usage example:

use CacheableTrait;

public function getTopProducts($count = 10)
{
    return $this->getOrSetHasCache(['top-n-products', 'n' => $count], function ($cache) use ($count) {
        return Products::find()->mostPopular()->limit(10)->all();
    }, 1000);
}
public mixed getOrSetHasCache ( $key, Closure $closure, $duration null, $dependency null )
$key mixed

A key identifying the value to be cached. This can be a simple string or a complex data structure consisting of factors representing the key.

$closure Closure

The closure that will be used to generate a value to be cached. In case $closure returns false, the value will not be cached.

$duration integer

Default duration in seconds before the cache will expire. If not set, defaultDuration value will be used. 0 means never expire.

$dependency yii\caching\Dependency

Dependency of the cached item. If the dependency changes, the corresponding value in the cache will be invalidated when it is fetched via get(). This parameter is ignored if serializer is false.

return mixed

Result of $closure execution

                public function getOrSetHasCache($key, \Closure $closure, $duration = null, $dependency = null)
{
    if (($value = $this->getHasCache($key)) !== false) {
        return $value;
    }
    
    $value = call_user_func($closure, $this);
    
    $this->setHasCache($key, $value, $dependency, $duration);
    
    return $value;
}

            
hasMethod() public method

Defined in: yii\base\BaseObject::hasMethod()

Returns a value indicating whether a method is defined.

The default implementation is a call to php function method_exists(). You may override this method when you implemented the php magic method __call().

public boolean hasMethod ( $name )
$name string

The method name

return boolean

Whether the method is defined

                public function hasMethod($name)
{
    return method_exists($this, $name);
}

            
hasProperty() public method

Defined in: yii\base\BaseObject::hasProperty()

Returns a value indicating whether a property is defined.

A property is defined if:

  • the class has a getter or setter method associated with the specified name (in this case, property name is case-insensitive);
  • the class has a member variable with the specified name (when $checkVars is true);

See also:

public boolean hasProperty ( $name, $checkVars true )
$name string

The property name

$checkVars boolean

Whether to treat member variables as properties

return boolean

Whether the property is defined

                public function hasProperty($name, $checkVars = true)
{
    return $this->canGetProperty($name, $checkVars) || $this->canSetProperty($name, false);
}

            
init() public method

Initializes the object.

This method is invoked at the end of the constructor after the object is initialized with the given configuration.

public void init ( )

                public function init()
{
    parent::init();
    if ($this->build === null) {
        throw new InvalidConfigException("The build property can not be empty.");
    }
}

            
isCachable() public method

Defined in: luya\traits\CacheableTrait::isCachable()

Check if the current configuration of the application and the property allows a caching of the language container data.

public boolean isCachable ( )

                public function isCachable()
{
    if ($this->_cachable === null) {
        $this->_cachable = Yii::$app->has('cache') ? true : false;
    }

    return $this->_cachable;
}

            
setHasCache() public method

Defined in: luya\traits\CacheableTrait::setHasCache()

Store cache data for a specific key if caching is enabled in this application.

public boolean setHasCache ( $key, $value, $dependency null, $cacheExpiration null )
$key string|array

The identifier key or a array to store complex keys

$value mixed

The value to store in the cache component.

$dependency yii\caching\Dependency|array

Dependency of the cached item. If the dependency changes, the corresponding value in the cache will be invalidated when it is fetched via get(). This parameter is ignored if $serializer is false. You can also define an array with defintion which will generate the Object instead of object is provided.

$cacheExpiration

Integer The time in seconds before the cache data expires, 0 means never expire.

return boolean

Whether set has been success or not

                public function setHasCache($key, $value, $dependency = null, $cacheExpiration = null)
{
    if ($this->isCachable()) {
        if (is_array($dependency)) {
            $dependency = Yii::createObject($dependency);
        }
        
        return Yii::$app->cache->set($key, $value, is_null($cacheExpiration) ? $this->_cacheExpiration : $cacheExpiration, $dependency);
    }
    
    return false;
}

            
start() public method

public void start ( )

                public function start()
{
    $this->flushHasCache();
    if (empty($this->only) || $this->only == self::ONLY_DB) {
        $this->startDb();
    }
    if (empty($this->only) || $this->only == self::ONLY_STORAGE) {
        $this->startFiles();
        $this->startImages();
    }
    // close the build
    $curl = new Curl();
    $curl->get($this->build->requestCloseUrl, ['buildToken' => $this->build->buildToken]);
    return true;
}

            
startDb() protected method (available since version 4.0.0)

Start DB Sync

protected void startDb ( )

                protected function startDb()
{
    if ($this->build->db->schema instanceof \yii\db\mysql\Schema) {
        $this->build->command->outputInfo('Using local database ' . $this->build->db->createCommand('SELECT DATABASE()')->queryScalar());
    }
    foreach ($this->build->getTables() as $name => $table) {
        $table->syncData();
    }
}

            
startFiles() protected method (available since version 4.0.0)

Start Storage Files Sync

protected void startFiles ( )

                protected function startFiles()
{
    $fileCount = 0;
    // sync files
    foreach ((new Query())->where(['is_deleted' => false])->all() as $file) {
        /** @var \luya\admin\file\Item $file */
        if (!$file->fileExists) {
            $curl = new Curl();
            $curl->setOpt(CURLOPT_RETURNTRANSFER, true);
            $curl->get($this->build->fileProviderUrl, [
                'buildToken' => $this->build->buildToken,
                'machine' => $this->build->machineIdentifier,
                'fileId' => $file->id,
            ]);
            if (!$curl->error) {
                $md5 = $this->storageUpload($file->systemFileName, $curl->response);
                if ($md5) {
                    if ($md5 == $file->getFileHash()) {
                        $fileCount++;
                        $this->build->command->outputInfo('[+] File ' . $file->name . ' ('.$file->systemFileName.') downloaded.');
                    } else {
                        $this->build->command->outputError('[!] Downloaded file checksum "'.$md5.'" does not match server checksum "'.$file->getFileHash().'" for file ' . $file->systemFileName.'.');
                    }
                } else {
                    $this->build->command->outputError('[!] Unable to temporary store the file ' . $file->systemFileName.'.');
                }
            } else {
                $this->build->command->outputError('[!] File ' . $file->systemFileName. ' download request error: "'. $curl->error_message.'".');
            }
            $curl->close();
            unset($curl);
            gc_collect_cycles();
        }
    }
    $this->build->command->outputInfo("[=] {$fileCount} Files downloaded.");
}

            
startImages() protected method (available since version 4.0.0)

Start Storage Images Sync

protected void startImages ( )

                protected function startImages()
{
    $imageCount = 0;
    // sync images
    foreach ((new \luya\admin\image\Query())->all() as $image) {
        /** @var Item $image */
        if (!$image->fileExists) {
            $curl = new Curl();
            $curl->setOpt(CURLOPT_RETURNTRANSFER, true);
            $curl->get($this->build->imageProviderUrl, [
                'buildToken' => $this->build->buildToken,
                'machine' => $this->build->machineIdentifier,
                'imageId' => $image->id,
            ]);
            if (!$curl->error) {
                if ($this->storageUpload($image->systemFileName, $curl->response)) {
                    $imageCount++;
                    $this->build->command->outputInfo('[+] Image ' . $image->source.' downloaded.');
                }
            }
            $curl->close();
            unset($curl);
            gc_collect_cycles();
        }
    }
    $this->build->command->outputInfo("[=] {$imageCount} Images downloaded.");
}

            
storageUpload() public method (available since version 3.6.0)

Upload file to storage.

Upload the given filename with its content into the websites storage system and return the md5 checksum of the uploaded file.

public string|false storageUpload ( $fileName, $content )
$fileName string
$content string
return string|false

Either returns the md5 hash of the uploaded file or false if something went wrong

                public function storageUpload($fileName, $content)
{
    try {
        $fromTempFile = @tempnam(sys_get_temp_dir(), 'clientTransferUpload');
        FileHelper::writeFile($fromTempFile, $content);
        $result = Yii::$app->storage->fileSystemSaveFile($fromTempFile, $fileName);
        if (!$result) {
            return false;
        }
        $md5 = FileHelper::md5sum($fromTempFile);
        FileHelper::unlink($fromTempFile);
        return $md5;
    } catch (Exception $e) {
        return false;
    }
}