PHPSocialNetwork / phpfastcache

A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.
https://www.phpfastcache.com
MIT License
2.36k stars 452 forks source link

PHP Arrays Driver #811

Closed Awilum closed 3 years ago

Awilum commented 3 years ago

I want to create PHP Arrays Driver that will store data as PHP Arrays in the php files. The issue I have, that I can't save php files with php file extension because of this:

const SAFE_FILE_EXTENSIONS = 'txt|cache|db|pfc';

and I don't understand why this method is not allowing to set custom extension. it is says Set Cache File Extension but I'm still can't do this, because it is anyway compare it to SAFE_FILE_EXTENSIONS

public function setCacheFileExtension(string $cacheFileExtension): self
    {
        /**
         * Feel free to propose your own one
         * by opening a pull request :)
         */
        $safeFileExtensions = \explode('|', SAFE_FILE_EXTENSIONS);

        if (\strpos($cacheFileExtension, '.') !== false) {
            throw new PhpfastcacheInvalidConfigurationException('cacheFileExtension cannot contain a dot "."');
        }
        if (!\in_array($cacheFileExtension, $safeFileExtensions, true)) {
            throw new PhpfastcacheInvalidConfigurationException(
                "Extension \"{$cacheFileExtension}\" is not safe, currently allowed extension names: " . \implode(', ', $safeFileExtensions)
            );
        }

        $this->cacheFileExtension = $cacheFileExtension;
        return $this;
    }

How to set php file extension ?

Awilum commented 3 years ago

Okey, issue solved

for my personal driver Config instead of:

class Config extends ConfigurationOption
{
    use IOConfigurationOptionTrait;
}

I do this:

class Config extends ConfigurationOption
{
    /**
     * @var boolean
     */
    protected $secureFileManipulation = false;

    /**
     * @var bool
     */
    protected $htaccess = true;

    /**
     * @var string
     */
    protected $securityKey = '';

    /**
     * @var string
     */
    protected $cacheFileExtension = 'php';

    /**
     * @return string
     */
    public function getSecurityKey(): string
    {
        return $this->securityKey;
    }

    /**
     * @param string $securityKey
     * @return Config
     */
    public function setSecurityKey(string $securityKey): self
    {
        $this->securityKey = $securityKey;

        return $this;
    }

    /**
     * @return bool
     */
    public function getHtaccess(): bool
    {
        return $this->htaccess;
    }

    /**
     * @param bool $htaccess
     * @return Config
     */
    public function setHtaccess(bool $htaccess): self
    {
        $this->htaccess = $htaccess;

        return $this;
    }

    /**
     * @return bool
     */
    public function isSecureFileManipulation(): bool
    {
        return $this->secureFileManipulation;
    }

    /**
     * @param bool $secureFileManipulation
     * @return self
     */
    public function setSecureFileManipulation(bool $secureFileManipulation): self
    {
        $this->secureFileManipulation = $secureFileManipulation;
        return $this;
    }

    /**
     * @return string
     */
    public function getCacheFileExtension(): string
    {
        return $this->cacheFileExtension;
    }

    /**
     * @param string $cacheFileExtension
     * @return self
     * @throws PhpfastcacheInvalidConfigurationException
     */
    public function setCacheFileExtension(string $cacheFileExtension): self
    {
        $this->cacheFileExtension = 'php';
        return $this;
    }
}
Geolim4 commented 3 years ago

Hello

Its a bit unconventional but instead of rewriting the whole trait, just rewrite the method by rewriting IOConfigurationOptionTrait::setCacheFileExtension().

Create a trait:

trait YourIOConfigurationOptionTrait
{
    use  IOConfigurationOptionTrait;
     public function setCacheFileExtension(string $cacheFileExtension): self
    {
        $this->cacheFileExtension =  $cacheFileExtension;
        return $this;
    }
}

and in your driver:

class YourDriverConfig
{
    use  YourIOConfigurationOptionTrait;
}

You can even rewrite the setCacheFileExtension method directly in your config class without extending the trait.

Otherwise your setter will be completely useless, do it properly :)

Awilum commented 3 years ago

@Geolim4 Thanks for your reply!