Merge branch 'develop' into testing2/helpers

This commit is contained in:
Master Yoda 2018-06-28 11:17:09 -07:00
commit 8aa28377e1
No known key found for this signature in database
GPG Key ID: CED549230775AD5B
15 changed files with 752 additions and 387 deletions

View File

@ -151,6 +151,56 @@ class FileLocator
//--------------------------------------------------------------------
/**
* Examines a file and returns the fully qualified domain name.
*
* @param string $file
*
* @return string
*/
public function getClassname(string $file) : string
{
$php = file_get_contents($file);
$tokens = token_get_all($php);
$count = count($tokens);
$dlm = false;
$namespace = '';
$class_name = '';
for ($i = 2; $i < $count; $i++)
{
if ((isset($tokens[$i-2][1]) && ($tokens[$i-2][1] == "phpnamespace" || $tokens[$i-2][1] == "namespace")) || ($dlm && $tokens[$i-1][0] == T_NS_SEPARATOR && $tokens[$i][0] == T_STRING))
{
if (! $dlm)
{
$namespace = 0;
}
if (isset($tokens[$i][1]))
{
$namespace = $namespace ? $namespace."\\".$tokens[$i][1] : $tokens[$i][1];
$dlm = true;
}
}
elseif ($dlm && ($tokens[$i][0] != T_NS_SEPARATOR) && ($tokens[$i][0] != T_STRING))
{
$dlm = false;
}
if (($tokens[$i-2][0] == T_CLASS || (isset($tokens[$i-2][1]) && $tokens[$i-2][1] == "phpclass"))
&& $tokens[$i-1][0] == T_WHITESPACE
&& $tokens[$i][0] == T_STRING)
{
$class_name = $tokens[$i][1];
break;
}
}
if( empty( $class_name ) ) return "";
return $namespace .'\\'. $class_name;
}
//--------------------------------------------------------------------
/**
* Searches through all of the defined namespaces looking for a file.
* Returns an array of all found locations for the defined file.

View File

@ -85,6 +85,24 @@ if ( ! function_exists('cache'))
//--------------------------------------------------------------------
if ( ! function_exists('config'))
{
/**
* More simple way of getting config instances
*
* @param string $name
* @param bool $getShared
*
* @return mixed
*/
function config(string $name, bool $getShared = true)
{
return \CodeIgniter\Config\Config::get($name, $getShared);
}
}
//--------------------------------------------------------------------
if ( ! function_exists('view'))
{

View File

@ -36,6 +36,8 @@
* @filesource
*/
use CodeIgniter\Autoloader\FileLocator;
/**
* Services Configuration file.
*
@ -191,7 +193,7 @@ class BaseService
// Get instances of all service classes and cache them locally.
foreach ($files as $file)
{
$classname = static::getClassName($file);
$classname = $locator->getClassname($file);
if (! in_array($classname, ['Config\\Services', 'CodeIgniter\\Config\\Services']))
{
@ -214,50 +216,4 @@ class BaseService
}
}
}
/**
* Examines a file and returns the fully qualified domain name.
*
* @param string $file
*
* @return string
*/
private static function getClassname(string $file)
{
$php = file_get_contents($file);
$tokens = token_get_all($php);
$count = count($tokens);
$dlm = false;
$namespace = '';
$class_name = '';
for ($i = 2; $i < $count; $i++)
{
if ((isset($tokens[$i-2][1]) && ($tokens[$i-2][1] == "phpnamespace" || $tokens[$i-2][1] == "namespace")) || ($dlm && $tokens[$i-1][0] == T_NS_SEPARATOR && $tokens[$i][0] == T_STRING))
{
if (! $dlm)
{
$namespace = 0;
}
if (isset($tokens[$i][1]))
{
$namespace = $namespace ? $namespace."\\".$tokens[$i][1] : $tokens[$i][1];
$dlm = true;
}
}
elseif ($dlm && ($tokens[$i][0] != T_NS_SEPARATOR) && ($tokens[$i][0] != T_STRING))
{
$dlm = false;
}
if (($tokens[$i-2][0] == T_CLASS || (isset($tokens[$i-2][1]) && $tokens[$i-2][1] == "phpclass"))
&& $tokens[$i-1][0] == T_WHITESPACE
&& $tokens[$i][0] == T_STRING)
{
$class_name = $tokens[$i][1];
break;
}
}
return $namespace .'\\'. $class_name;
}
}

122
system/Config/Config.php Normal file
View File

@ -0,0 +1,122 @@
<?php namespace CodeIgniter\Config;
/**
* CodeIgniter
*
* An open source application development framework for PHP
*
* This content is released under the MIT License (MIT)
*
* Copyright (c) 2014-2018 British Columbia Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
* @package CodeIgniter
* @author CodeIgniter Dev Team
* @copyright 2014-2018 British Columbia Institute of Technology (https://bcit.ca/)
* @license https://opensource.org/licenses/MIT MIT License
* @link https://codeigniter.com
* @since Version 3.0.0
* @filesource
*/
/**
* Class Config
*
* @package CodeIgniter\Config
*/
class Config
{
/**
* Cache for instance of any configurations that
* have been requested as "shared" instance.
*
* @var array
*/
static private $instances = [];
//--------------------------------------------------------------------
/**
* Create new configuration instances or return
* a shared instance
*
* @param string $name Configuration name
* @param boolean $getShared Use shared instance
*
* @return mixed|null
*/
public static function get(string $name, bool $getShared = true)
{
$class = $name;
if (($pos = strrpos($name, '\\')) !== false)
{
$class = substr($name, $pos + 1);
}
$class = strtolower($class);
if (! $getShared)
{
return self::createClass($name);
}
if (! isset( self::$instances[$class] ))
{
self::$instances[$class] = self::createClass($name);
}
return self::$instances[$class];
}
//--------------------------------------------------------------------
/**
* Find configuration class and create instance
*
* @param string $name Classname
*
* @return mixed|null
*/
private static function createClass(string $name)
{
if (class_exists($name))
{
return new $name();
}
$locator = Services::locator();
$file = $locator->locateFile($name, 'Config');
if (empty($file))
{
return null;
}
$name = $locator->getClassname($file);
if (empty($name))
{
return null;
}
return new $name();
}
//--------------------------------------------------------------------
}

View File

@ -1,4 +1,5 @@
<?php
/**
* CodeIgniter
*
@ -35,7 +36,6 @@
* @since Version 1.0.0
* @filesource
*/
/**
* CodeIgniter Directory Helpers
*
@ -66,8 +66,10 @@ if ( ! function_exists('directory_map'))
*/
function directory_map(string $source_dir, int $directory_depth = 0, bool $hidden = false): array
{
if ($fp = @opendir($source_dir))
try
{
$fp = opendir($source_dir);
$filedata = [];
$new_depth = $directory_depth - 1;
$source_dir = rtrim($source_dir, DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
@ -95,8 +97,10 @@ if ( ! function_exists('directory_map'))
closedir($fp);
return $filedata;
}
return [];
catch (\Exception $fe)
{
return [];
}
}
}
@ -120,25 +124,29 @@ if ( ! function_exists('write_file'))
*/
function write_file(string $path, string $data, string $mode = 'wb'): bool
{
if ( ! $fp = @fopen($path, $mode))
try
{
$fp = fopen($path, $mode);
flock($fp, LOCK_EX);
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
{
if (($result = fwrite($fp, substr($data, $written))) === false)
{
break;
}
}
flock($fp, LOCK_UN);
fclose($fp);
return is_int($result);
}
catch (\Exception $fe)
{
return false;
}
flock($fp, LOCK_EX);
for ($result = $written = 0, $length = strlen($data); $written < $length; $written += $result)
{
if (($result = fwrite($fp, substr($data, $written))) === false)
{
break;
}
}
flock($fp, LOCK_UN);
fclose($fp);
return is_int($result);
}
}
@ -168,29 +176,33 @@ if ( ! function_exists('delete_files'))
// Trim the trailing slash
$path = rtrim($path, '/\\');
if ( ! $current_dir = @opendir($path))
try
{
$current_dir = opendir($path);
while (false !== ($filename = @readdir($current_dir)))
{
if ($filename !== '.' && $filename !== '..')
{
if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.')
{
delete_files($path . DIRECTORY_SEPARATOR . $filename, $delDir, $htdocs, $_level + 1);
}
elseif ($htdocs !== true || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
{
@unlink($path . DIRECTORY_SEPARATOR . $filename);
}
}
}
closedir($current_dir);
return ($delDir === true && $_level > 0) ? @rmdir($path) : true;
}
catch (\Exception $fe)
{
return false;
}
while (false !== ($filename = @readdir($current_dir)))
{
if ($filename !== '.' && $filename !== '..')
{
if (is_dir($path . DIRECTORY_SEPARATOR . $filename) && $filename[0] !== '.')
{
delete_files($path . DIRECTORY_SEPARATOR . $filename, $delDir, $htdocs, $_level + 1);
}
elseif ($htdocs !== true || ! preg_match('/^(\.htaccess|index\.(html|htm|php)|web\.config)$/i', $filename))
{
@unlink($path . DIRECTORY_SEPARATOR . $filename);
}
}
}
closedir($current_dir);
return ($delDir === true && $_level > 0) ? @rmdir($path) : true;
}
}
@ -216,8 +228,9 @@ if ( ! function_exists('get_filenames'))
{
static $filedata = [];
if ($fp = @opendir($source_dir))
try
{
$fp = opendir($source_dir);
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($recursion === false)
{
@ -240,8 +253,10 @@ if ( ! function_exists('get_filenames'))
closedir($fp);
return $filedata;
}
return [];
catch (\Exception $fe)
{
return [];
}
}
}
@ -270,34 +285,38 @@ if ( ! function_exists('get_dir_file_info'))
static $filedata = [];
$relative_path = $source_dir;
if ($fp = @opendir($source_dir))
try
{
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($recursion === false)
{
$filedata = [];
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
// Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
while (false !== ($file = readdir($fp)))
{
if (is_dir($source_dir . $file) && $file[0] !== '.' && $top_level_only === false)
$fp = @opendir($source_dir); {
// reset the array and make sure $source_dir has a trailing slash on the initial call
if ($recursion === false)
{
get_dir_file_info($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, true);
$filedata = [];
$source_dir = rtrim(realpath($source_dir), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
}
elseif ($file[0] !== '.')
{
$filedata[$file] = get_file_info($source_dir . $file);
$filedata[$file]['relative_path'] = $relative_path;
}
}
closedir($fp);
return $filedata;
// Used to be foreach (scandir($source_dir, 1) as $file), but scandir() is simply not as fast
while (false !== ($file = readdir($fp)))
{
if (is_dir($source_dir . $file) && $file[0] !== '.' && $top_level_only === false)
{
get_dir_file_info($source_dir . $file . DIRECTORY_SEPARATOR, $top_level_only, true);
}
elseif ($file[0] !== '.')
{
$filedata[$file] = get_file_info($source_dir . $file);
$filedata[$file]['relative_path'] = $relative_path;
}
}
closedir($fp);
return $filedata;
}
}
catch (\Exception $fe)
{
return [];
}
return [];
}
}
@ -318,9 +337,9 @@ if ( ! function_exists('get_file_info'))
* @param string $file Path to file
* @param mixed $returned_values Array or comma separated string of information returned
*
* @return array
* @return array|null
*/
function get_file_info(string $file, $returned_values = ['name', 'server_path', 'size', 'date']): array
function get_file_info(string $file, $returned_values = ['name', 'server_path', 'size', 'date'])
{
if ( ! file_exists($file))
{
@ -334,8 +353,7 @@ if ( ! function_exists('get_file_info'))
foreach ($returned_values as $key)
{
switch ($key)
{
switch ($key) {
case 'name':
$fileinfo['name'] = basename($file);
break;

View File

@ -0,0 +1,40 @@
<?php namespace CodeIgniter\Config;
use Config\Email;
class ConfigTest extends \CIUnitTestCase
{
public function testCreateSingleInstance()
{
$Config = Config::get('email', false);
$UpperConfig = Config::get('Email', false);
$NamespaceConfig = Config::get('Config\\Email', false);
$this->assertInstanceOf(Email::class, $Config);
$this->assertInstanceOf(Email::class, $UpperConfig);
$this->assertInstanceOf(Email::class, $NamespaceConfig);
}
public function testCreateInvalidInstance()
{
$Config = Config::get('gfnusvjai', false);
$this->assertNull($Config);
}
public function testCreateSharedInstance()
{
$Config = Config::get('email' );
$Config2 = Config::get('Config\\Email');
$this->assertTrue($Config === $Config2);
}
public function testCreateNonConfig()
{
$Config = Config::get('constants', false);
$this->assertNull($Config);
}
}

View File

@ -57,7 +57,7 @@ class ResponseTest extends \CIUnitTestCase
$response = new Response(new App());
$this->expectException(HTTPException::class);
$this->expectExceptionMessage('Unknown HTTP status code provided with no message');
$this->expectExceptionMessage(lang('HTTP.unknownStatusCode', [115]));
$response->setStatusCode(115);
}

View File

@ -90,6 +90,22 @@ class ArrayHelperTest extends \CIUnitTestCase
$this->assertEquals(23, dot_array_search('foo.*.baz', $data));
}
public function testArrayDotNestedNotFound()
{
$data = [
'foo' => [
'buzz' => [
'fizz' => 11
],
'bar' => [
'baz' => 23
]
]
];
$this->assertNull(dot_array_search('foo.*.notthere', $data));
}
public function testArrayDotIgnoresLastWildcard()
{
$data = [

View File

@ -4,7 +4,7 @@ use Config\App;
use CodeIgniter\Config\Services;
use Tests\Support\HTTP\MockResponse;
final class cookieHelperTest extends \CIUnitTestCase
final class CookieHelperTest extends \CIUnitTestCase
{
private $name;
@ -22,6 +22,8 @@ final class cookieHelperTest extends \CIUnitTestCase
Services::injectMock('response', new MockResponse(new App()));
$this->response = service('response');
$this->request = new IncomingRequest(new App(), new URI(), null, new UserAgent());
Services::injectMock('request', $this->request);
helper('cookie');
}
@ -30,14 +32,11 @@ final class cookieHelperTest extends \CIUnitTestCase
public function testSetCookie()
{
$this->response->setCookie($this->name, $this->value, $this->expire);
//TODO: Find a way for set_cookie() to use the MockResponse object.
//set_cookie($this->name, $this->value, $this->expire);
set_cookie($this->name, $this->value, $this->expire);
$this->assertTrue($this->response->hasCookie($this->name));
$this->response->deleteCookie($this->name);
delete_cookie($this->name);
}
//--------------------------------------------------------------------
@ -49,17 +48,16 @@ final class cookieHelperTest extends \CIUnitTestCase
'value' => $this->value,
'expire' => $this->expire
];
//set_cookie($cookieAttr);
$this->response->setCookie($cookieAttr);
set_cookie($cookieAttr);
$this->assertTrue($this->response->hasCookie($this->name, $this->value));
$this->response->deleteCookie($this->name);
delete_cookie($this->name);
}
//--------------------------------------------------------------------
public function testGetCookie()
public function testSetCookieSecured()
{
$pre = 'Hello, I try to';
$pst = 'your site';
@ -68,29 +66,35 @@ final class cookieHelperTest extends \CIUnitTestCase
$unsecured = 'unsecured';
$secured = 'secured';
//set_cookie($unsecured, $unsec, $this->expire);
//set_cookie($secured, $sec, $this->expire);
$this->response->setCookie($unsecured, $unsec, $this->expire);
$this->response->setCookie($secured, $sec, $this->expire);
set_cookie($unsecured, $unsec, $this->expire);
set_cookie($secured, $sec, $this->expire);
$this->assertTrue($this->response->hasCookie($unsecured, $unsec));
$this->assertTrue($this->response->hasCookie($secured, $sec));
$this->response->deleteCookie($unsecured);
$this->response->deleteCookie($secured);
delete_cookie($unsecured);
delete_cookie($secured);
}
//--------------------------------------------------------------------
public function testDeleteCookie()
{
//set_cookie($this->name, $this->value, $this->expire);
$this->response->setCookie($this->name, $this->value, $this->expire);
set_cookie($this->name, $this->value, $this->expire);
//$this->response->setCookie($this->name, $this->value, $this->expire);
$this->response->deleteCookie($this->name);
delete_cookie($this->name);
//$this->assertEquals(get_cookie($this->name), '');
$this->assertTrue($this->response->hasCookie($this->name));
$this->assertEmpty($this->response->getCookie($this->name));
}
//--------------------------------------------------------------------
public function testGetCookie()
{
$_COOKIE['TEST'] = 5;
$this->assertEquals(5, get_cookie('TEST'));
}
}

View File

@ -0,0 +1,37 @@
<?php namespace CodeIgniter\HTTP;
use Config\App;
use CodeIgniter\Config\Services;
use Tests\Support\HTTP\MockResponse;
final class DateHelperTest extends \CIUnitTestCase
{
private $name;
private $value;
private $expire;
private $response;
public function setUp()
{
parent::setUp();
helper('date');
}
//--------------------------------------------------------------------
public function testNowDefault()
{
$time = new \DateTime();
$this->assertLessThan(1, abs(now() - time())); // close enough
}
//--------------------------------------------------------------------
public function testNowSpecific()
{
// Chicago should be two hours ahead of Vancouver
$this->assertEquals(7200,now('America/Chicago')-now('America/Vancouver'));
}
}

View File

@ -1,298 +1,341 @@
<?php namespace CodeIgniter\Helpers;
<?php
namespace CodeIgniter\Helpers;
use org\bovigo\vfs\vfsStream;
class FilesystemHelperTest extends \CIUnitTestCase
{
public function testDirectoryMapDefaults()
{
helper('filesystem');
$this->assertTrue(function_exists('directory_map'));
public function setUp()
{
parent::setUp();
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
$this->structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
}
$expected = [
'foo' . DIRECTORY_SEPARATOR => [
'bar',
'baz'
],
'boo' . DIRECTORY_SEPARATOR => [
'far',
'faz'
],
'AnEmptyFolder' . DIRECTORY_SEPARATOR => [],
'simpleFile'
];
//--------------------------------------------------------------------
$root = vfsStream::setup('root', null, $structure);
$this->assertTrue($root->hasChild('foo'));
public function testDirectoryMapDefaults()
{
helper('filesystem');
$this->assertTrue(function_exists('directory_map'));
$this->assertEquals($expected, directory_map(vfsStream::url('root')));
}
$expected = [
'foo' . DIRECTORY_SEPARATOR => [
'bar',
'baz'
],
'boo' . DIRECTORY_SEPARATOR => [
'far',
'faz'
],
'AnEmptyFolder' . DIRECTORY_SEPARATOR => [],
'simpleFile'
];
//--------------------------------------------------------------------
$root = vfsStream::setup('root', null, $this->structure);
$this->assertTrue($root->hasChild('foo'));
public function testDirectoryMapShowsHiddenFiles()
{
helper('filesystem');
$this->assertTrue(function_exists('directory_map'));
$this->assertEquals($expected, directory_map(vfsStream::url('root')));
}
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
public function testDirectoryMapShowsHiddenFiles()
{
helper('filesystem');
$this->assertTrue(function_exists('directory_map'));
$expected = [
'foo' . DIRECTORY_SEPARATOR => [
'bar',
'baz'
],
'boo' . DIRECTORY_SEPARATOR => [
'far',
'faz'
],
'AnEmptyFolder' . DIRECTORY_SEPARATOR => [],
'simpleFile',
'.hidden'
];
$expected = [
'foo' . DIRECTORY_SEPARATOR => [
'bar',
'baz'
],
'boo' . DIRECTORY_SEPARATOR => [
'far',
'faz'
],
'AnEmptyFolder' . DIRECTORY_SEPARATOR => [],
'simpleFile',
'.hidden'
];
$root = vfsStream::setup('root', null, $structure);
$this->assertTrue($root->hasChild('foo'));
$root = vfsStream::setup('root', null, $this->structure);
$this->assertTrue($root->hasChild('foo'));
$this->assertEquals($expected, directory_map(vfsStream::url('root'), false, true));
}
$this->assertEquals($expected, directory_map(vfsStream::url('root'), false, true));
}
//--------------------------------------------------------------------
public function testDirectoryMapLimitsRecursion()
{
$this->assertTrue(function_exists('directory_map'));
public function testDirectoryMapLimitsRecursion()
{
$this->assertTrue(function_exists('directory_map'));
$expected = [
'foo' . DIRECTORY_SEPARATOR,
'boo' . DIRECTORY_SEPARATOR,
'AnEmptyFolder' . DIRECTORY_SEPARATOR,
'simpleFile',
'.hidden'
];
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
$root = vfsStream::setup('root', null, $this->structure);
$this->assertTrue($root->hasChild('foo'));
$expected = [
'foo' . DIRECTORY_SEPARATOR,
'boo' . DIRECTORY_SEPARATOR,
'AnEmptyFolder' . DIRECTORY_SEPARATOR,
'simpleFile',
'.hidden'
];
$this->assertEquals($expected, directory_map(vfsStream::url('root'), 1, true));
}
$root = vfsStream::setup('root', null, $structure);
$this->assertTrue($root->hasChild('foo'));
public function testDirectoryMapHandlesNotfound()
{
$this->assertEquals([], directory_map(SUPPORTPATH . 'Files/shaker/'));
}
$this->assertEquals($expected, directory_map(vfsStream::url('root'), 1, true));
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
public function testWriteFileSuccess()
{
$vfs = vfsStream::setup('root');
public function testWriteFileSuccess()
{
$vfs = vfsStream::setup('root');
$this->assertTrue(write_file(vfsStream::url('root/test.php'), 'Simple'));
$this->assertFileExists($vfs->getChild('test.php')->url());
}
$this->assertTrue(write_file(vfsStream::url('root/test.php'), 'Simple'));
$this->assertFileExists($vfs->getChild('test.php')->url());
}
public function testWriteFileFailure()
{
$vfs = vfsStream::setup('root');
//--------------------------------------------------------------------
$this->assertFalse(write_file(vfsStream::url('apple#test.php'), 'Simple'));
}
public function testDeleteFilesDefaultsToOneLevelDeep()
{
$this->assertTrue(function_exists('delete_files'));
//--------------------------------------------------------------------
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
public function testDeleteFilesDefaultsToOneLevelDeep()
{
$this->assertTrue(function_exists('delete_files'));
$vfs = vfsStream::setup('root', null, $structure);
$vfs = vfsStream::setup('root', null, $this->structure);
delete_files(vfsStream::url('root'));
delete_files(vfsStream::url('root'));
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('.hidden'));
$this->assertTrue($vfs->hasChild('foo'));
$this->assertTrue($vfs->hasChild('boo'));
$this->assertTrue($vfs->hasChild('AnEmptyFolder'));
}
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('.hidden'));
$this->assertTrue($vfs->hasChild('foo'));
$this->assertTrue($vfs->hasChild('boo'));
$this->assertTrue($vfs->hasChild('AnEmptyFolder'));
}
//--------------------------------------------------------------------
public function testDeleteFilesHandlesRecursion()
{
$this->assertTrue(function_exists('delete_files'));
public function testDeleteFilesHandlesRecursion()
{
$this->assertTrue(function_exists('delete_files'));
$vfs = vfsStream::setup('root', null, $this->structure);
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
delete_files(vfsStream::url('root'), true);
$vfs = vfsStream::setup('root', null, $structure);
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('.hidden'));
$this->assertFalse($vfs->hasChild('foo'));
$this->assertFalse($vfs->hasChild('boo'));
$this->assertFalse($vfs->hasChild('AnEmptyFolder'));
}
delete_files(vfsStream::url('root'), true);
public function testDeleteFilesLeavesHTFiles()
{
$structure = array_merge($this->structure, [
'.htaccess' => 'Deny All',
'index.html' => 'foo',
'index.php' => 'blah'
]);
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('.hidden'));
$this->assertFalse($vfs->hasChild('foo'));
$this->assertFalse($vfs->hasChild('boo'));
$this->assertFalse($vfs->hasChild('AnEmptyFolder'));
}
$vfs = vfsStream::setup('root', null, $structure);
//--------------------------------------------------------------------
delete_files(vfsStream::url('root'), true, true);
public function testDeleteFilesLeavesHTFiles()
{
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon',
'.htaccess' => 'Deny All',
'index.html' => 'foo',
'index.php' => 'blah'
];
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('foo'));
$this->assertFalse($vfs->hasChild('boo'));
$this->assertFalse($vfs->hasChild('AnEmptyFolder'));
$this->assertTrue($vfs->hasChild('.htaccess'));
$this->assertTrue($vfs->hasChild('index.html'));
$this->assertTrue($vfs->hasChild('index.php'));
}
$vfs = vfsStream::setup('root', null, $structure);
public function testDeleteFilesFailure()
{
$this->assertFalse(delete_files(SUPPORTPATH . 'Files/shaker/'));
}
delete_files(vfsStream::url('root'), true, true);
//--------------------------------------------------------------------
$this->assertFalse($vfs->hasChild('simpleFile'));
$this->assertFalse($vfs->hasChild('foo'));
$this->assertFalse($vfs->hasChild('boo'));
$this->assertFalse($vfs->hasChild('AnEmptyFolder'));
$this->assertTrue($vfs->hasChild('.htaccess'));
$this->assertTrue($vfs->hasChild('index.html'));
$this->assertTrue($vfs->hasChild('index.php'));
}
public function testGetFilenames()
{
$this->assertTrue(function_exists('delete_files'));
//--------------------------------------------------------------------
// Not sure the directory names should actually show up
// here but this matches v3.x results.
$expected = [
'foo',
'boo',
'AnEmptyFolder',
'simpleFile'
];
public function testGetFilenames()
{
$this->assertTrue(function_exists('delete_files'));
$vfs = vfsStream::setup('root', null, $this->structure);
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
$this->assertEquals($expected, get_filenames($vfs->url(), false));
}
// Not sure the directory names should actually show up
// here but this matches v3.x results.
$expected = [
'foo',
'boo',
'AnEmptyFolder',
'simpleFile'
];
public function testGetFilenamesWithSource()
{
$this->assertTrue(function_exists('delete_files'));
$vfs = vfsStream::setup('root', null, $structure);
// Not sure the directory names should actually show up
// here but this matches v3.x results.
$expected = [
DIRECTORY_SEPARATOR . 'foo',
DIRECTORY_SEPARATOR . 'boo',
DIRECTORY_SEPARATOR . 'AnEmptyFolder',
DIRECTORY_SEPARATOR . 'simpleFile'
];
$this->assertEquals($expected, get_filenames($vfs->url(), false));
}
$vfs = vfsStream::setup('root', null, $this->structure);
//--------------------------------------------------------------------
$this->assertEquals($expected, get_filenames($vfs->url(), true));
}
public function testGetFilenamesWithSource()
{
$this->assertTrue(function_exists('delete_files'));
public function testGetFilenamesFailure()
{
$this->assertEquals([], get_filenames(SUPPORTPATH . 'Files/shaker/'));
}
$structure = [
'foo' => [
'bar' => 'Once upon a midnight dreary',
'baz' => 'While I pondered weak and weary'
],
'boo' => [
'far' => 'Upon a tome of long-forgotten lore',
'faz' => 'There came a tapping up on the door'
],
'AnEmptyFolder' => [],
'simpleFile' => 'A tap-tap-tapping upon my door',
'.hidden' => 'There is no spoon'
];
//--------------------------------------------------------------------
// Not sure the directory names should actually show up
// here but this matches v3.x results.
$expected = [
DIRECTORY_SEPARATOR . 'foo',
DIRECTORY_SEPARATOR . 'boo',
DIRECTORY_SEPARATOR . 'AnEmptyFolder',
DIRECTORY_SEPARATOR . 'simpleFile'
];
public function testGetDirFileInfo()
{
$expected = [
'banana.php' => [
'name' => 'banana.php',
'server_path' => '/pub7/htdocs/CodeIgniter4/tests/_support/Files/baker/banana.php',
'size' => 193,
'date' => 1529305930,
'relative_path' => '/pub7/htdocs/CodeIgniter4/tests/_support/Files/baker',
]
];
$vfs = vfsStream::setup('root', null, $structure);
$this->assertEquals($expected, get_filenames($vfs->url(), true));
}
$this->assertEquals($expected, get_dir_file_info(SUPPORTPATH . 'Files/baker'));
}
//--------------------------------------------------------------------
public function testGetDirFileInfoNested()
{
$expected = ['banana.php', 'prune_ripe.php', 'fig_3.php', 'apple.php'];
$results = get_dir_file_info(SUPPORTPATH . 'Files', false);
$this->assertEmpty(array_diff($expected, array_keys($results)));
}
public function testGetDirFileInfoFailure()
{
$expected = [];
$this->assertEquals($expected, get_dir_file_info(SUPPORTPATH . 'Files#baker'));
}
//--------------------------------------------------------------------
public function testGetFileInfo()
{
$expected = [
'name' => 'banana.php',
'server_path' => '/pub7/htdocs/CodeIgniter4/tests/_support/Files/baker/banana.php',
'size' => 193,
'date' => 1529305930,
];
$this->assertEquals($expected, get_file_info(SUPPORTPATH . 'Files/baker/banana.php'));
}
public function testGetFileInfoCustom()
{
$expected = [
'readable' => true,
'writable' => true,
'executable' => false,
];
$this->assertEquals($expected, get_file_info(SUPPORTPATH . 'Files/baker/banana.php', 'readable,writable,executable'));
}
public function testGetFileInfoPerms()
{
$expected = 0664;
$stuff = get_file_info(SUPPORTPATH . 'Files/baker/banana.php', 'fileperms');
$this->assertEquals($expected, $stuff['fileperms'] & 0777);
}
public function testGetFileNotThereInfo()
{
$expected = null;
$this->assertEquals($expected, get_file_info(SUPPORTPATH . 'Files/icer'));
}
//--------------------------------------------------------------------
public function testOctalPermissions()
{
$this->assertEquals('777', octal_permissions(0777));
$this->assertEquals('655', octal_permissions(0655));
$this->assertEquals('123', octal_permissions(0123));
}
public function testSymbolicPermissions()
{
$expected = [
0777 => 'urwxrwxrwx',
0655 => 'urw-r-xr-x',
0123 => 'u--x-w--wx',
010655 => 'prw-r-xr-x',
020655 => 'crw-r-xr-x',
040655 => 'drw-r-xr-x',
060655 => 'brw-r-xr-x',
0100655 => '-rw-r-xr-x',
0120655 => 'lrw-r-xr-x',
0140655 => 'srw-r-xr-x',
];
foreach ($expected as $perm => $value)
$this->assertEquals($value, symbolic_permissions($perm));
}
//--------------------------------------------------------------------
public function testRealPathURL()
{
$this->expectException(\InvalidArgumentException::class);
set_realpath('http://somewhere.com/overtherainbow');
}
public function testRealPathInvalid()
{
$this->expectException(\InvalidArgumentException::class);
set_realpath(SUPPORTPATH . 'root/../', true);
}
public function testRealPathResolved()
{
$this->assertEquals(SUPPORTPATH . 'Helpers/', set_realpath(SUPPORTPATH . 'Files/../Helpers', true));
}
}

View File

@ -11,11 +11,21 @@ create an instance of the class and all your settings are there for you.
Accessing Config Files
======================
You can access config files within your classes by creating a new instance. All of the properties
You can access config files within your classes by creating a new instance or using the config function. All of the properties
are public, so you access the settings like any other property::
// Creating new class by hand
$config = new \Config\EmailConfig();
// Creating new class with config function
$config = config( 'EmailConfig', false );
// Get shared instance with config function
$config = config( 'EmailConfig' );
// Access config class with namespace
$config = config( 'Config\\EmailConfig' );
// Access settings as class properties
$protocol = $config->protocol;
$mailpath = $config->mailpath;

View File

@ -0,0 +1,47 @@
###########
Date Helper
###########
The Date Helper file contains functions that assist in working with
dates.
.. contents::
:local:
.. raw:: html
<div class="custom-index container"></div>
Loading this Helper
===================
This helper is loaded using the following code::
helper('date');
Available Functions
===================
The following functions are available:
.. php:function:: now([$timezone = NULL])
:param string $timezone: Timezone
:returns: UNIX timestamp
:rtype: int
Returns the current time as a UNIX timestamp, referenced either to your server's
local time or any PHP supported timezone, based on the "time reference" setting
in your config file. If you do not intend to set your master time reference to
any other PHP supported timezone (which you'll typically do if you run a site
that lets each user set their own timezone settings) there is no benefit to using
this function over PHP's ``time()`` function.
::
echo now('Australia/Victoria');
If a timezone is not provided, it will return ``time()`` based on the
**time_reference** setting.
Many functions previously found in the CodeIgniter 3 ``date_helper`` have been moved to the ``I18n``
module in CodeIgniter 4.

View File

@ -44,9 +44,9 @@ Tutorial
CodeIgniter4 Overview
*********************
.. toctree::
:titlesonly:
:titlesonly:
concepts/index
concepts/index
**************
General Topics
@ -61,7 +61,10 @@ General Topics
Library Reference
*****************
* ``Honeypot``
.. toctree::
:titlesonly:
libraries/index
******************
Database Reference
@ -86,9 +89,9 @@ Testing
*******
.. toctree::
:titlesonly:
:titlesonly:
testing.index
testing/index
***************************
Contributing to CodeIgniter
@ -97,4 +100,4 @@ Contributing to CodeIgniter
.. toctree::
:titlesonly:
contributing/index
contributing/index

View File

@ -13,6 +13,7 @@ Library Reference
curlrequest
email
files
honeypot
images
incomingrequest
localization