0 follower

Final Class Yiisoft\Proxy\ClassCache

InheritanceYiisoft\Proxy\ClassCache

Public Methods

Hide inherited methods

Method Description Defined By
__construct() Yiisoft\Proxy\ClassCache
get() Reads proxy class contents. Yiisoft\Proxy\ClassCache
getClassPath() Gets full path to a class. For example: /tmp/Yiisoft/Tests/Stub/GraphInterface.MyProxy.php or /tmp/Yiisoft/Tests/Stub/Graph.MyProxy.php. Additionally, checks and prepares (if needed) {@see $cachePath} for usage (@see FileHelper::ensureDirectory()}. Yiisoft\Proxy\ClassCache
set() Writes proxy class contents to a file in {@see getClassPath()}. Yiisoft\Proxy\ClassCache

Method Details

Hide inherited methods

__construct() public method

public mixed __construct ( string $cachePath )
$cachePath string

                public function __construct(
    /**
     * @var string Base directory for working with cache. It will be created automatically if it does not exist
     * ({@see getClassPath()}).
     */
    private string $cachePath
) {
}

            
get() public method

Reads proxy class contents.

public string|null get ( string $className, string $baseProxyClassName )
$className string

The full name of user class or interface (with namespace). For example: GraphInterface or Graph. You can use ::class instead of manually specifying a string.

$baseProxyClassName string

The full name of {@see \Yiisoft\Proxy\ObjectProxy} implementation (with namespace) which will be the base class for proxy. For example: MyProxy.

return string|null

In case of presence data in cache the whole class contents (including PHP opening tag) returned as a string. In case of its absence or other errors - null is returned.

throws Exception

When unable to write to a file in {@see \Yiisoft\Proxy\getClassPath()}.

                public function get(string $className, string $baseProxyClassName): ?string
{
    $classPath = $this->getClassPath($className, $baseProxyClassName);
    if (!file_exists($classPath)) {
        return null;
    }
    $content = file_get_contents($classPath);
    return $content === false ? null : $content;
}

            
getClassPath() public method

Gets full path to a class. For example: /tmp/Yiisoft/Tests/Stub/GraphInterface.MyProxy.php or /tmp/Yiisoft/Tests/Stub/Graph.MyProxy.php. Additionally, checks and prepares (if needed) {@see $cachePath} for usage (@see FileHelper::ensureDirectory()}.

public string getClassPath ( string $className, string $baseProxyClassName )
$className string

The full name of user class or interface (with namespace). For example: GraphInterface or Graph. You can use ::class instead of manually specifying a string.

$baseProxyClassName string

The full name of {@see \Yiisoft\Proxy\ObjectProxy} implementation (with namespace) which will be the base class for proxy. For example: MyProxy.

throws RuntimeException

In case when it's impossible to use or create {@see $cachePath}.

                public function getClassPath(string $className, string $baseProxyClassName): string
{
    [$classFileName, $classFilePath] = $this->getClassFileNameAndPath($className, $baseProxyClassName);
    try {
        FileHelper::ensureDirectory($classFilePath, 0777);
    } catch (RuntimeException) {
        throw new RuntimeException("Directory \"$classFilePath\" was not created.");
    }
    return $classFilePath . DIRECTORY_SEPARATOR . $classFileName;
}

            
set() public method

Writes proxy class contents to a file in {@see getClassPath()}.

public void set ( string $className, string $baseProxyClassName, string $classContents )
$className string

The full name of user class or interface (with namespace). For example: Yiisoft\Proxy\Tests\Stub\GraphInterface or Yiisoft\Proxy\Tests\Stub\Graph. You can use ::class instead of manually specifying a string.

$baseProxyClassName string

The full name of {@see \Yiisoft\Proxy\ObjectProxy} implementation (with namespace) which will be the base class for proxy. For example: MyProxy.

$classContents string

The whole class contents without opening PHP tag (it's prepended automatically).

                public function set(string $className, string $baseProxyClassName, string $classContents): void
{
    file_put_contents($this->getClassPath($className, $baseProxyClassName), "<?php\n\n" . $classContents, LOCK_EX);
}