Skip to content

Commit b3a43e3

Browse files
committed
Merge branch 'develop' into user_guide
2 parents f26e637 + b4bc1b4 commit b3a43e3

20 files changed

Lines changed: 399 additions & 71 deletions

File tree

system/Common.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
*/
3939

4040
use Config\App;
41+
use Config\View;
4142
use Config\Logger;
4243
use Config\Database;
4344
use Config\Services;
@@ -1069,8 +1070,9 @@ function view(string $name, array $data = [], array $options = []): string
10691070
*/
10701071
$renderer = Services::renderer();
10711072

1072-
$saveData = true;
1073-
if (array_key_exists('saveData', $options) && $options['saveData'] === true)
1073+
$saveData = config(View::class)->saveData;
1074+
1075+
if (array_key_exists('saveData', $options))
10741076
{
10751077
$saveData = (bool) $options['saveData'];
10761078
unset($options['saveData']);

system/Database/Seeder.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141

4242
use CodeIgniter\CLI\CLI;
4343
use CodeIgniter\Config\BaseConfig;
44+
use CodeIgniter\Database\Forge;
4445

4546
/**
4647
* Class Seeder
@@ -122,6 +123,8 @@ public function __construct(BaseConfig $config, BaseConnection $db = null)
122123
}
123124

124125
$this->db = & $db;
126+
127+
$this->forge = \Config\Database::forge($this->DBGroup);
125128
}
126129

127130
//--------------------------------------------------------------------

system/HTTP/Negotiate.php

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function encoding(array $supported = []): string
179179
*/
180180
public function language(array $supported): string
181181
{
182-
return $this->getBestMatch($supported, $this->request->getHeaderLine('accept-language'));
182+
return $this->getBestMatch($supported, $this->request->getHeaderLine('accept-language'), false, false, true);
183183
}
184184

185185
//--------------------------------------------------------------------
@@ -198,10 +198,11 @@ public function language(array $supported): string
198198
* @param boolean $enforceTypes If TRUE, will compare media types and sub-types.
199199
* @param boolean $strictMatch If TRUE, will return empty string on no match.
200200
* If FALSE, will return the first supported element.
201+
* @param boolean $matchLocales If TRUE, will match locale sub-types to a broad type (fr-FR = fr)
201202
*
202203
* @return string Best match
203204
*/
204-
protected function getBestMatch(array $supported, string $header = null, bool $enforceTypes = false, bool $strictMatch = false): string
205+
protected function getBestMatch(array $supported, string $header = null, bool $enforceTypes = false, bool $strictMatch = false, bool $matchLocales = false): string
205206
{
206207
if (empty($supported))
207208
{
@@ -232,7 +233,7 @@ protected function getBestMatch(array $supported, string $header = null, bool $e
232233
// If an acceptable value is supported, return it
233234
foreach ($supported as $available)
234235
{
235-
if ($this->match($accept, $available, $enforceTypes))
236+
if ($this->match($accept, $available, $enforceTypes, $matchLocales))
236237
{
237238
return $available;
238239
}
@@ -337,12 +338,14 @@ public function parseHeader(string $header): array
337338
/**
338339
* Match-maker
339340
*
340-
* @param array $acceptable
341-
* @param string $supported
342-
* @param boolean $enforceTypes
341+
* @param array $acceptable
342+
* @param string $supported
343+
* @param boolean $enforceTypes
344+
* @param boolean $matchLocales
345+
*
343346
* @return boolean
344347
*/
345-
protected function match(array $acceptable, string $supported, bool $enforceTypes = false): bool
348+
protected function match(array $acceptable, string $supported, bool $enforceTypes = false, $matchLocales = false): bool
346349
{
347350
$supported = $this->parseHeader($supported);
348351
if (is_array($supported) && count($supported) === 1)
@@ -363,6 +366,12 @@ protected function match(array $acceptable, string $supported, bool $enforceType
363366
return $this->matchTypes($acceptable, $supported);
364367
}
365368

369+
// Do we need to match locales against broader locales?
370+
if ($matchLocales)
371+
{
372+
return $this->matchLocales($acceptable, $supported);
373+
}
374+
366375
return false;
367376
}
368377

@@ -409,8 +418,14 @@ protected function matchParameters(array $acceptable, array $supported): bool
409418
*/
410419
public function matchTypes(array $acceptable, array $supported): bool
411420
{
412-
list($aType, $aSubType) = explode('/', $acceptable['value']);
413-
list($sType, $sSubType) = explode('/', $supported['value']);
421+
[
422+
$aType,
423+
$aSubType,
424+
] = explode('/', $acceptable['value']);
425+
[
426+
$sType,
427+
$sSubType,
428+
] = explode('/', $supported['value']);
414429

415430
// If the types don't match, we're done.
416431
if ($aType !== $sType)
@@ -429,4 +444,25 @@ public function matchTypes(array $acceptable, array $supported): bool
429444
}
430445

431446
//--------------------------------------------------------------------
447+
448+
/**
449+
* Will match locales against their broader pairs, so that fr-FR would
450+
* match a supported localed of fr
451+
*
452+
* @param array $acceptable
453+
* @param array $supported
454+
*
455+
* @return boolean
456+
*/
457+
public function matchLocales(array $acceptable, array $supported): bool
458+
{
459+
$aBroad = mb_strpos($acceptable['value'], '-') > 0
460+
? mb_substr($acceptable['value'], 0, mb_strpos($acceptable['value'], '-'))
461+
: $acceptable['value'];
462+
$sBroad = mb_strpos($supported['value'], '-') > 0
463+
? mb_substr($supported['value'], 0, mb_strpos($supported['value'], '-'))
464+
: $supported['value'];
465+
466+
return strtolower($aBroad) === strtolower($sBroad);
467+
}
432468
}

system/Model.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,10 +1126,11 @@ public function chunk(int $size, Closure $userFunc)
11261126
* @param string $group Will be used by the pagination library
11271127
* to identify a unique pagination set.
11281128
* @param integer $page Optional page number (useful when the page number is provided in different way)
1129+
* @param integer $segment Optional URI segment number (if page number is provided by URI segment)
11291130
*
11301131
* @return array|null
11311132
*/
1132-
public function paginate(int $perPage = null, string $group = 'default', int $page = 0)
1133+
public function paginate(int $perPage = null, string $group = 'default', int $page = 0, int $segment = 0)
11331134
{
11341135
$pager = \Config\Services::pager(null, null, false);
11351136
$page = $page >= 1 ? $page : $pager->getCurrentPage($group);
@@ -1138,7 +1139,7 @@ public function paginate(int $perPage = null, string $group = 'default', int $pa
11381139

11391140
// Store it in the Pager library so it can be
11401141
// paginated in the views.
1141-
$this->pager = $pager->store($group, $page, $perPage, $total);
1142+
$this->pager = $pager->store($group, $page, $perPage, $total, $segment);
11421143
$perPage = $this->pager->getPerPage($group);
11431144
$offset = ($page - 1) * $perPage;
11441145

system/Validation/Validation.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -709,7 +709,7 @@ protected function getErrorMessage(string $rule, string $field, string $label =
709709
// Check if custom message has been defined by user
710710
if (isset($this->customErrors[$field][$rule]))
711711
{
712-
$message = $this->customErrors[$field][$rule];
712+
$message = lang($this->customErrors[$field][$rule]);
713713
}
714714
else
715715
{
@@ -719,8 +719,8 @@ protected function getErrorMessage(string $rule, string $field, string $label =
719719
$message = lang('Validation.' . $rule);
720720
}
721721

722-
$message = str_replace('{field}', $label ?? $field, $message);
723-
$message = str_replace('{param}', $this->rules[$param]['label'] ?? $param, $message);
722+
$message = str_replace('{field}', empty($label) ? $field : lang($label), $message);
723+
$message = str_replace('{param}', empty($this->rules[$param]['label']) ? $param : lang($this->rules[$param]['label']), $message);
724724

725725
return str_replace('{value}', $value, $message);
726726
}

system/View/Parser.php

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,11 @@ class Parser extends View
9393
/**
9494
* Constructor
9595
*
96-
* @param \Config\View $config
97-
* @param string $viewPath
98-
* @param mixed $loader
99-
* @param boolean $debug
100-
* @param LoggerInterface $logger
96+
* @param \Config\View $config
97+
* @param string $viewPath
98+
* @param mixed $loader
99+
* @param boolean $debug
100+
* @param LoggerInterface $logger
101101
*/
102102
public function __construct($config, string $viewPath = null, $loader = null, bool $debug = null, LoggerInterface $logger = null)
103103
{
@@ -157,20 +157,25 @@ public function render(string $view, array $options = null, bool $saveData = nul
157157
throw ViewException::forInvalidFile($file);
158158
}
159159

160+
if (is_null($this->tempData))
161+
{
162+
$this->tempData = $this->data;
163+
}
164+
160165
$template = file_get_contents($file);
161-
$output = $this->parse($template, $this->data, $options);
166+
$output = $this->parse($template, $this->tempData, $options);
162167
$this->logPerformance($start, microtime(true), $view);
163168

164-
if (! $saveData)
169+
if ($saveData)
165170
{
166-
$this->data = [];
171+
$this->data = $this->tempData;
167172
}
168173
// Should we cache?
169174
if (isset($options['cache']))
170175
{
171176
cache()->save($cacheName, $output, (int) $options['cache']);
172177
}
173-
178+
$this->tempData = null;
174179
return $output;
175180
}
176181

@@ -196,14 +201,22 @@ public function renderString(string $template, array $options = null, bool $save
196201
$saveData = $this->config->saveData;
197202
}
198203

199-
$output = $this->parse($template, $this->data, $options);
204+
if (is_null($this->tempData))
205+
{
206+
$this->tempData = $this->data;
207+
}
208+
209+
$output = $this->parse($template, $this->tempData, $options);
200210

201211
$this->logPerformance($start, microtime(true), $this->excerpt($template));
202212

203-
if (! $saveData)
213+
if ($saveData)
204214
{
205-
$this->data = [];
215+
$this->data = $this->tempData;
206216
}
217+
218+
$this->tempData = null;
219+
207220
return $output;
208221
}
209222

@@ -243,7 +256,8 @@ public function setData(array $data = [], string $context = null): RendererInter
243256
}
244257
}
245258

246-
$this->data = array_merge($this->data, $data);
259+
$this->tempData = $this->tempData ?? $this->data;
260+
$this->tempData = array_merge($this->tempData, $data);
247261

248262
return $this;
249263
}
@@ -532,7 +546,14 @@ protected function parseConditionals(string $template): string
532546

533547
// Parse the PHP itself, or insert an error so they can debug
534548
ob_start();
535-
extract($this->data);
549+
550+
if (is_null($this->tempData))
551+
{
552+
$this->tempData = $this->data;
553+
}
554+
555+
extract($this->tempData);
556+
536557
try
537558
{
538559
eval('?>' . $template . '<?php ');
@@ -542,6 +563,7 @@ protected function parseConditionals(string $template): string
542563
ob_end_clean();
543564
throw ViewException::forTagSyntaxError(str_replace(['?>', '<?php '], '', $template));
544565
}
566+
545567
return ob_get_clean();
546568
}
547569

0 commit comments

Comments
 (0)