Skip to content

Commit 3416a0b

Browse files
committed
Release v4.1.3
1 parent 0bf6182 commit 3416a0b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

65 files changed

+478
-269
lines changed

app/Config/Cache.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,21 @@ class Cache extends BaseConfig
8282
*/
8383
public $prefix = '';
8484

85+
/**
86+
* --------------------------------------------------------------------------
87+
* Default TTL
88+
* --------------------------------------------------------------------------
89+
*
90+
* The default number of seconds to save items when none is specified.
91+
*
92+
* WARNING: This is not used by framework handlers where 60 seconds is
93+
* hard-coded, but may be useful to projects and modules. This will replace
94+
* the hard-coded value in a future release.
95+
*
96+
* @var integer
97+
*/
98+
public $ttl = 60;
99+
85100
/**
86101
* --------------------------------------------------------------------------
87102
* File settings

public/index.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717

1818
// Load our paths config file
1919
// This is the line that might need to be changed, depending on your folder structure.
20-
require realpath(FCPATH . '../app/Config/Paths.php') ?: FCPATH . '../app/Config/Paths.php';
20+
$pathsConfig = FCPATH . '../app/Config/Paths.php';
2121
// ^^^ Change this if you move your application folder
22+
require realpath($pathsConfig) ?: $pathsConfig;
2223

2324
$paths = new Config\Paths();
2425

spark

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ if (strpos(PHP_SAPI, 'cgi') === 0)
3333
define('FCPATH', __DIR__ . DIRECTORY_SEPARATOR . 'public' . DIRECTORY_SEPARATOR);
3434

3535
// Load our paths config file
36-
require realpath('app/Config/Paths.php') ?: 'app/Config/Paths.php';
36+
$pathsConfig = 'app/Config/Paths.php';
3737
// ^^^ Change this line if you move your application folder
38+
require realpath($pathsConfig) ?: $pathsConfig;
3839

3940
$paths = new Config\Paths();
4041

system/Autoloader/Autoloader.php

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@
2222
* An autoloader that uses both PSR4 autoloading, and traditional classmaps.
2323
*
2424
* Given a foo-bar package of classes in the file system at the following paths:
25-
*```
25+
* ```
2626
* /path/to/packages/foo-bar/
2727
* /src
2828
* Baz.php # Foo\Bar\Baz
2929
* Qux/
3030
* Quux.php # Foo\Bar\Qux\Quux
31-
*```
31+
* ```
3232
* you can add the path to the configuration array that is passed in the constructor.
3333
* The Config array consists of 2 primary keys, both of which are associative arrays:
3434
* 'psr4', and 'classmap'.
35-
*```
35+
* ```
3636
* $Config = [
3737
* 'psr4' => [
3838
* 'Foo\Bar' => '/path/to/packages/foo-bar'
@@ -41,17 +41,17 @@
4141
* 'MyClass' => '/path/to/class/file.php'
4242
* ]
4343
* ];
44-
*```
44+
* ```
4545
* Example:
46-
*```
46+
* ```
4747
* <?php
4848
* // our configuration array
4949
* $Config = [ ... ];
5050
* $loader = new \CodeIgniter\Autoloader\Autoloader($Config);
5151
*
5252
* // register the autoloader
5353
* $loader->register();
54-
*```
54+
* ```
5555
*/
5656
class Autoloader
5757
{
@@ -258,15 +258,6 @@ protected function loadInNamespace(string $class)
258258
{
259259
if (strpos($class, '\\') === false)
260260
{
261-
$class = 'Config\\' . $class;
262-
$filePath = APPPATH . str_replace('\\', DIRECTORY_SEPARATOR, $class) . '.php';
263-
$filename = $this->includeFile($filePath);
264-
265-
if ($filename)
266-
{
267-
return $filename;
268-
}
269-
270261
return false;
271262
}
272263

@@ -353,7 +344,9 @@ protected function discoverComposerNamespaces()
353344
return;
354345
}
355346

356-
/** @var ClassLoader $composer */
347+
/**
348+
* @var ClassLoader $composer
349+
*/
357350
$composer = include COMPOSER_PATH;
358351
$paths = $composer->getPrefixesPsr4();
359352
$classes = $composer->getClassMap();

system/CLI/CLI.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ public static function getOption(string $name)
983983

984984
// If the option didn't have a value, simply return TRUE
985985
// so they know it was set, otherwise return the actual value.
986-
$val = static::$options[$name] === null ? true : static::$options[$name];
986+
$val = static::$options[$name] ?? true;
987987

988988
return $val;
989989
}

system/Cache/Handlers/FileHandler.php

Lines changed: 9 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -285,35 +285,13 @@ public function getMetaData(string $key)
285285
{
286286
$key = static::validateKey($key, $this->prefix);
287287

288-
if (! is_file($this->path . $key))
288+
if (false === $data = $this->getItem($key))
289289
{
290290
return false; // This will return null in a future release
291291
}
292292

293-
$data = @unserialize(file_get_contents($this->path . $key));
294-
295-
if (! is_array($data) || ! isset($data['ttl']))
296-
{
297-
return false; // This will return null in a future release
298-
}
299-
300-
// Consider expired items as missing
301-
$expire = $data['time'] + $data['ttl'];
302-
303-
// @phpstan-ignore-next-line
304-
if ($data['ttl'] > 0 && time() > $expire)
305-
{
306-
// If the file is still there then remove it
307-
if (is_file($this->path . $key))
308-
{
309-
unlink($this->path . $key);
310-
}
311-
312-
return false; // This will return null in a future release
313-
}
314-
315293
return [
316-
'expire' => $expire,
294+
'expire' => $data['time'] + $data['ttl'],
317295
'mtime' => filemtime($this->path . $key),
318296
'data' => $data['data'],
319297
];
@@ -348,15 +326,19 @@ protected function getItem(string $filename)
348326
return false;
349327
}
350328

351-
$data = unserialize(file_get_contents($this->path . $filename));
329+
$data = @unserialize(file_get_contents($this->path . $filename));
330+
if (! is_array($data) || ! isset($data['ttl']))
331+
{
332+
return false;
333+
}
352334

353335
// @phpstan-ignore-next-line
354336
if ($data['ttl'] > 0 && time() > $data['time'] + $data['ttl'])
355337
{
356-
// If the file is still there then remove it
338+
// If the file is still there then try to remove it
357339
if (is_file($this->path . $filename))
358340
{
359-
unlink($this->path . $filename);
341+
@unlink($this->path . $filename);
360342
}
361343

362344
return false;

system/Cache/Handlers/MemcachedHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ public function getMetaData(string $key)
351351
return false; // This will return null in a future release
352352
}
353353

354-
list($data, $time, $limit) = $stored;
354+
[$data, $time, $limit] = $stored;
355355

356356
// Calculate the remaining time to live from the original limit
357357
$ttl = time() - $time - $limit;

system/CodeIgniter.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,8 @@ class CodeIgniter
4444
/**
4545
* The current version of CodeIgniter Framework
4646
*/
47-
const CI_VERSION = '4.1.2';
47+
const CI_VERSION = '4.1.3';
4848

49-
/**
50-
* @var string
51-
*/
5249
private const MIN_PHP_VERSION = '7.3';
5350

5451
/**
@@ -474,7 +471,7 @@ protected function handleRequest(?RouteCollectionInterface $routes, Cache $cache
474471

475472
// Save our current URI as the previous URI in the session
476473
// for safer, more accurate use with `previous_url()` helper function.
477-
$this->storePreviousURL((string) current_url(true));
474+
$this->storePreviousURL(current_url(true));
478475

479476
unset($uri);
480477

@@ -1080,7 +1077,7 @@ public function storePreviousURL($uri)
10801077

10811078
if (isset($_SESSION))
10821079
{
1083-
$_SESSION['_ci_previous_url'] = (string) $uri;
1080+
$_SESSION['_ci_previous_url'] = URI::createURIString($uri->getScheme(), $uri->getAuthority(), $uri->getPath(), $uri->getQuery(), $uri->getFragment());
10841081
}
10851082
}
10861083

system/Commands/Generators/ValidationGenerator.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
namespace CodeIgniter\Commands\Generators;
1313

1414
use CodeIgniter\CLI\BaseCommand;
15-
use CodeIgniter\CLI\CLI;
1615
use CodeIgniter\CLI\GeneratorTrait;
1716

1817
/**
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
<?php
2+
3+
namespace CodeIgniter\Commands\Utilities;
4+
5+
use CodeIgniter\CLI\BaseCommand;
6+
use CodeIgniter\CLI\CLI;
7+
use CodeIgniter\Config\DotEnv;
8+
9+
/**
10+
* Command to display the current environment,
11+
* or set a new one in the `.env` file.
12+
*/
13+
final class Environment extends BaseCommand
14+
{
15+
/**
16+
* The group the command is lumped under
17+
* when listing commands.
18+
*
19+
* @var string
20+
*/
21+
protected $group = 'CodeIgniter';
22+
23+
/**
24+
* The Command's name
25+
*
26+
* @var string
27+
*/
28+
protected $name = 'env';
29+
30+
/**
31+
* The Command's short description
32+
*
33+
* @var string
34+
*/
35+
protected $description = 'Retrieves the current environment, or set a new one.';
36+
37+
/**
38+
* The Command's usage
39+
*
40+
* @var string
41+
*/
42+
protected $usage = 'env [<environment>]';
43+
44+
/**
45+
* The Command's arguments
46+
*
47+
* @var array<string, string>
48+
*/
49+
protected $arguments = [
50+
'environment' => '[Optional] The new environment to set. If none is provided, this will print the current environment.',
51+
];
52+
53+
/**
54+
* The Command's options
55+
*
56+
* @var array
57+
*/
58+
protected $options = [];
59+
60+
/**
61+
* Allowed values for environment. `testing` is excluded
62+
* since spark won't work on it.
63+
*
64+
* @var array<int, string>
65+
*/
66+
private static $knownTypes = [
67+
'production',
68+
'development',
69+
];
70+
71+
/**
72+
* @inheritDoc
73+
*
74+
* @param array<string, mixed> $params
75+
*
76+
* @return void
77+
*/
78+
public function run(array $params)
79+
{
80+
if ($params === [])
81+
{
82+
CLI::write(sprintf('Your environment is currently set as %s.', CLI::color($_SERVER['CI_ENVIRONMENT'] ?? ENVIRONMENT, 'green')));
83+
CLI::newLine();
84+
85+
return;
86+
}
87+
88+
$env = strtolower(array_shift($params));
89+
90+
if ($env === 'testing')
91+
{
92+
CLI::error('The "testing" environment is reserved for PHPUnit testing.', 'light_gray', 'red');
93+
CLI::error('You will not be able to run spark under a "testing" environment.', 'light_gray', 'red');
94+
CLI::newLine();
95+
96+
return;
97+
}
98+
99+
if (! in_array($env, self::$knownTypes, true))
100+
{
101+
CLI::error(sprintf('Invalid environment type "%s". Expected one of "%s".', $env, implode('" and "', self::$knownTypes)), 'light_gray', 'red');
102+
CLI::newLine();
103+
104+
return;
105+
}
106+
107+
if (! $this->writeNewEnvironmentToEnvFile($env))
108+
{
109+
CLI::error('Error in writing new environment to .env file.', 'light_gray', 'red');
110+
CLI::newLine();
111+
112+
return;
113+
}
114+
115+
// force DotEnv to reload the new environment
116+
// however we cannot redefine the ENVIRONMENT constant
117+
putenv('CI_ENVIRONMENT');
118+
unset($_ENV['CI_ENVIRONMENT'], $_SERVER['CI_ENVIRONMENT']);
119+
(new DotEnv(ROOTPATH))->load();
120+
121+
CLI::write(sprintf('Environment is successfully changed to "%s".', $env), 'green');
122+
CLI::write('The ENVIRONMENT constant will be changed in the next script execution.');
123+
CLI::newLine();
124+
}
125+
126+
/**
127+
* @see https://regex101.com/r/4sSORp/1 for the regex in action
128+
*
129+
* @param string $newEnv
130+
*
131+
* @return boolean
132+
*/
133+
private function writeNewEnvironmentToEnvFile(string $newEnv): bool
134+
{
135+
$baseEnv = ROOTPATH . 'env';
136+
$envFile = ROOTPATH . '.env';
137+
138+
if (! is_file($envFile))
139+
{
140+
if (! is_file($baseEnv))
141+
{
142+
CLI::write('Both default shipped `env` file and custom `.env` are missing.', 'yellow');
143+
CLI::write('It is impossible to write the new environment type.', 'yellow');
144+
CLI::newLine();
145+
146+
return false;
147+
}
148+
149+
copy($baseEnv, $envFile);
150+
}
151+
152+
$pattern = preg_quote($_SERVER['CI_ENVIRONMENT'] ?? ENVIRONMENT, '/');
153+
$pattern = sprintf('/^[#\s]*CI_ENVIRONMENT[=\s]+%s$/m', $pattern);
154+
155+
return file_put_contents(
156+
$envFile,
157+
preg_replace($pattern, "\nCI_ENVIRONMENT = {$newEnv}", file_get_contents($envFile), -1, $count)
158+
) !== false && $count > 0;
159+
}
160+
}

0 commit comments

Comments
 (0)