Skip to content

Commit

Permalink
Merge pull request #2 from codeigniter4/develop
Browse files Browse the repository at this point in the history
updates
  • Loading branch information
NPWSamarasinghe authored Jan 17, 2021
2 parents 52a857e + eaaf227 commit 5d28010
Show file tree
Hide file tree
Showing 17 changed files with 191 additions and 78 deletions.
16 changes: 16 additions & 0 deletions app/Config/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class Cache extends BaseConfig
* system.
*
* @var string
*
* @deprecated Use the driver-specific variant under $file
*/
public $storePath = WRITEPATH . 'cache/';

Expand Down Expand Up @@ -80,6 +82,20 @@ class Cache extends BaseConfig
*/
public $prefix = '';

/**
* --------------------------------------------------------------------------
* File settings
* --------------------------------------------------------------------------
* Your file storage preferences can be specified below, if you are using
* the File driver.
*
* @var array<string, string|int|null>
*/
public $file = [
'storePath' => WRITEPATH . 'cache/',
'mode' => 0640,
];

/**
* -------------------------------------------------------------------------
* Memcached settings
Expand Down
1 change: 1 addition & 0 deletions app/Config/Mimes.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ class Mimes
'multipart/x-zip',
],
'rar' => [
'application/vnd.rar',
'application/x-rar',
'application/rar',
'application/x-rar-compressed',
Expand Down
32 changes: 26 additions & 6 deletions system/Cache/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ class FileHandler implements CacheInterface
*/
protected $path;

/**
* Mode for the stored files.
* Must be chmod-safe (octal).
*
* @var integer
*
* @see https://www.php.net/manual/en/function.chmod.php
*/
protected $mode;

//--------------------------------------------------------------------

/**
Expand All @@ -44,14 +54,24 @@ class FileHandler implements CacheInterface
*/
public function __construct(Cache $config)
{
$path = ! empty($config->storePath) ? $config->storePath : WRITEPATH . 'cache';
if (! is_really_writable($path))
if (! property_exists($config, 'file'))
{
$config->file = [
'storePath' => $config->storePath ?? WRITEPATH . 'cache',
'mode' => 0640,
];
}

$this->path = ! empty($config->file['storePath']) ? $config->file['storePath'] : WRITEPATH . 'cache';
$this->path = rtrim($this->path, '/') . '/';

if (! is_really_writable($this->path))
{
throw CacheException::forUnableToWrite($path);
throw CacheException::forUnableToWrite($this->path);
}

$this->prefix = $config->prefix ?: '';
$this->path = rtrim($path, '/') . '/';
$this->mode = $config->file['mode'] ?? 0640;
$this->prefix = (string) $config->prefix;
}

//--------------------------------------------------------------------
Expand Down Expand Up @@ -105,7 +125,7 @@ public function save(string $key, $value, int $ttl = 60)

if ($this->writeFile($this->path . $key, serialize($contents)))
{
chmod($this->path . $key, 0640);
chmod($this->path . $key, $this->mode);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/MemcachedHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class MemcachedHandler implements CacheInterface
*/
public function __construct(Cache $config)
{
$this->prefix = $config->prefix ?: '';
$this->prefix = (string) $config->prefix;

if (! empty($config))
{
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/PredisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class PredisHandler implements CacheInterface
*/
public function __construct(Cache $config)
{
$this->prefix = $config->prefix ?: '';
$this->prefix = (string) $config->prefix;

if (isset($config->redis))
{
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/RedisHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class RedisHandler implements CacheInterface
*/
public function __construct(Cache $config)
{
$this->prefix = $config->prefix ?: '';
$this->prefix = (string) $config->prefix;

if (! empty($config))
{
Expand Down
2 changes: 1 addition & 1 deletion system/Cache/Handlers/WincacheHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class WincacheHandler implements CacheInterface
*/
public function __construct(Cache $config)
{
$this->prefix = $config->prefix ?: '';
$this->prefix = (string) $config->prefix;
}

//--------------------------------------------------------------------
Expand Down
8 changes: 8 additions & 0 deletions system/Config/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
*/
class View extends BaseConfig
{
/**
* When false, the view method will clear the data between each
* call.
*
* @var boolean
*/
public $saveData = true;

/**
* Parser Filters map a filter name with any PHP callable. When the
* Parser prepares a variable for display, it will chain it
Expand Down
18 changes: 5 additions & 13 deletions system/HTTP/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ class Request extends Message implements MessageInterface, RequestInterface
*/
protected $uri;

//--------------------------------------------------------------------

/**
* Constructor.
*
Expand All @@ -68,8 +66,6 @@ public function __construct($config = null)
}
}

//--------------------------------------------------------------------

/**
* Validate an IP address
*
Expand All @@ -85,8 +81,6 @@ public function isValidIP(string $ip = null, string $which = null): bool
return (new FormatRules())->valid_ip($ip, $which);
}

//--------------------------------------------------------------------

/**
* Get the request method.
*
Expand All @@ -101,8 +95,6 @@ public function getMethod(bool $upper = false): string
return ($upper) ? strtoupper($this->method) : strtolower($this->method);
}

//--------------------------------------------------------------------

/**
* Sets the request method. Used when spoofing the request.
*
Expand All @@ -124,16 +116,16 @@ public function setMethod(string $method)
*
* @param string $method
*
* @return self
* @return static
*/
public function withMethod($method)
{
$clone = clone $this;
$request = clone $this;

return $clone->setMethod($method);
}
$request->method = $method;

//--------------------------------------------------------------------
return $request;
}

/**
* Retrieves the URI instance.
Expand Down
21 changes: 14 additions & 7 deletions system/Validation/FormatRules.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ public function alpha_space(?string $value = null): bool
return true;
}

return (bool) preg_match('/^[A-Z ]+$/i', $value);
// @see https://regex101.com/r/LhqHPO/1
return (bool) preg_match('/\A[A-Z ]+\z/i', $value);
}

/**
Expand All @@ -56,7 +57,8 @@ public function alpha_space(?string $value = null): bool
*/
public function alpha_dash(?string $str = null): bool
{
return (bool) preg_match('/^[a-z0-9_-]+$/i', $str);
// @see https://regex101.com/r/XfVY3d/1
return (bool) preg_match('/\A[a-z0-9_-]+\z/i', $str);
}

/**
Expand All @@ -72,7 +74,8 @@ public function alpha_dash(?string $str = null): bool
*/
public function alpha_numeric_punct($str)
{
return (bool) preg_match('/^[A-Z0-9 ~!#$%\&\*\-_+=|:.]+$/i', $str);
// @see https://regex101.com/r/6N8dDY/1
return (bool) preg_match('/\A[A-Z0-9 ~!#$%\&\*\-_+=|:.]+\z/i', $str);
}

/**
Expand All @@ -96,7 +99,8 @@ public function alpha_numeric(?string $str = null): bool
*/
public function alpha_numeric_space(?string $str = null): bool
{
return (bool) preg_match('/^[A-Z0-9 ]+$/i', $str);
// @see https://regex101.com/r/0AZDME/1
return (bool) preg_match('/\A[A-Z0-9 ]+\z/i', $str);
}

/**
Expand All @@ -123,7 +127,8 @@ public function string($str = null): bool
*/
public function decimal(?string $str = null): bool
{
return (bool) preg_match('/^[-+]?[0-9]{0,}\.?[0-9]+$/', $str);
// @see https://regex101.com/r/HULifl/1/
return (bool) preg_match('/\A[-+]?[0-9]{0,}\.?[0-9]+\z/', $str);
}

/**
Expand All @@ -147,7 +152,7 @@ public function hex(?string $str = null): bool
*/
public function integer(?string $str = null): bool
{
return (bool) preg_match('/^[\-+]?[0-9]+$/', $str);
return (bool) preg_match('/\A[\-+]?[0-9]+\z/', $str);
}

/**
Expand Down Expand Up @@ -181,7 +186,8 @@ public function is_natural_no_zero(?string $str = null): bool
*/
public function numeric(?string $str = null): bool
{
return (bool) preg_match('/^[\-+]?[0-9]*\.?[0-9]+$/', $str);
// @see https://regex101.com/r/bb9wtr/1
return (bool) preg_match('/\A[\-+]?[0-9]*\.?[0-9]+\z/', $str);
}

/**
Expand Down Expand Up @@ -253,6 +259,7 @@ public function valid_json(string $str = null): bool
*/
public function valid_email(string $str = null): bool
{
// @see https://regex101.com/r/wlJG1t/1/
if (function_exists('idn_to_ascii') && defined('INTL_IDNA_VARIANT_UTS46') && preg_match('#\A([^@]+)@(.+)\z#', $str, $matches))
{
$str = $matches[1] . '@' . idn_to_ascii($matches[2], 0, INTL_IDNA_VARIANT_UTS46);
Expand Down
8 changes: 2 additions & 6 deletions system/View/RendererInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,7 @@ interface RendererInterface
* @param array $options Reserved for 3rd-party uses since
* it might be needed to pass additional info
* to other template engines.
* @param boolean $saveData If true, will save data for use with any other calls,
* if false, will clean the data after displaying the view,
* if not specified, use the config setting.
* @param boolean $saveData Whether to save data for subsequent calls
*
* @return string
*/
Expand All @@ -44,9 +42,7 @@ public function render(string $view, array $options = null, bool $saveData = fal
* @param array $options Reserved for 3rd-party uses since
* it might be needed to pass additional info
* to other template engines.
* @param boolean $saveData If true, will save data for use with any other calls,
* if false, will clean the data after displaying the view,
* if not specified, use the config setting.
* @param boolean $saveData Whether to save data for subsequent calls
*
* @return string
*/
Expand Down
32 changes: 18 additions & 14 deletions system/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function __construct(ViewConfig $config, string $viewPath = null, FileLoc
$this->loader = $loader ?? Services::locator();
$this->logger = $logger ?? Services::logger();
$this->debug = $debug ?? CI_DEBUG;
$this->saveData = $config->saveData ?? null;
$this->saveData = (bool) $config->saveData;
}

//--------------------------------------------------------------------
Expand All @@ -152,12 +152,16 @@ public function __construct(ViewConfig $config, string $viewPath = null, FileLoc
* data that has already been set.
*
* Valid $options:
* - cache number of seconds to cache for
* - cache_name Name to use for cache
* - cache Number of seconds to cache for
* - cache_name Name to use for cache
*
* @param string $view
* @param array|null $options
* @param boolean|null $saveData
* @param string $view File name of the view source
* @param array|null $options Reserved for 3rd-party uses since
* it might be needed to pass additional info
* to other template engines.
* @param boolean|null $saveData If true, saves data for subsequent calls,
* if false, cleans the data after displaying,
* if null, uses the config setting.
*
* @return string
*/
Expand All @@ -172,7 +176,7 @@ public function render(string $view, array $options = null, bool $saveData = nul
$fileExt = pathinfo($view, PATHINFO_EXTENSION);
$realPath = empty($fileExt) ? $view . '.php' : $view; // allow Views as .html, .tpl, etc (from CI3)
$this->renderVars['view'] = $realPath;
$this->renderVars['options'] = $options;
$this->renderVars['options'] = $options ?? [];

// Was it cached?
if (isset($this->renderVars['options']['cache']))
Expand Down Expand Up @@ -272,13 +276,13 @@ public function render(string $view, array $options = null, bool $saveData = nul
* data that has already been set.
* Cache does not apply, because there is no "key".
*
* @param string $view The view contents
* @param array $options Reserved for 3rd-party uses since
* it might be needed to pass additional info
* to other template engines.
* @param boolean $saveData If true, will save data for use with any other calls,
* if false, will clean the data after displaying the view,
* if not specified, use the config setting.
* @param string $view The view contents
* @param array|null $options Reserved for 3rd-party uses since
* it might be needed to pass additional info
* to other template engines.
* @param boolean|null $saveData If true, saves data for subsequent calls,
* if false, cleans the data after displaying,
* if null, uses the config setting.
*
* @return string
*/
Expand Down
Loading

0 comments on commit 5d28010

Please sign in to comment.