Skip to content

Commit b2d0ce7

Browse files
kenjislonnieezell
authored andcommitted
Merge remote-tracking branch 'upstream/develop' into 4.5
2 parents bc97a0e + 8f2c065 commit b2d0ce7

File tree

9 files changed

+297
-19
lines changed

9 files changed

+297
-19
lines changed

system/CLI/GeneratorTrait.php

+30-17
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ trait GeneratorTrait
5959
*/
6060
protected $classNameLang = '';
6161

62+
/**
63+
* Namespace to use for class.
64+
* Leave null to use the default namespace.
65+
*/
66+
protected ?string $namespace = null;
67+
6268
/**
6369
* Whether to require class name.
6470
*
@@ -302,20 +308,15 @@ protected function qualifyClassName(): string
302308
);
303309

304310
// Gets the namespace from input. Don't forget the ending backslash!
305-
$namespace = trim(
306-
str_replace(
307-
'/',
308-
'\\',
309-
$this->getOption('namespace') ?? APP_NAMESPACE
310-
),
311-
'\\'
312-
) . '\\';
311+
$namespace = $this->getNamespace() . '\\';
313312

314313
if (strncmp($class, $namespace, strlen($namespace)) === 0) {
315314
return $class; // @codeCoverageIgnore
316315
}
317316

318-
return $namespace . $this->directory . '\\' . str_replace('/', '\\', $class);
317+
$directoryString = ! empty($this->directory) ? $this->directory . '\\' : '';
318+
319+
return $namespace . $directoryString . str_replace('/', '\\', $class);
319320
}
320321

321322
/**
@@ -403,14 +404,7 @@ protected function buildContent(string $class): string
403404
*/
404405
protected function buildPath(string $class): string
405406
{
406-
$namespace = trim(
407-
str_replace(
408-
'/',
409-
'\\',
410-
$this->getOption('namespace') ?? APP_NAMESPACE
411-
),
412-
'\\'
413-
);
407+
$namespace = $this->getNamespace();
414408

415409
// Check if the namespace is actually defined and we are not just typing gibberish.
416410
$base = Services::autoloader()->getNamespace($namespace);
@@ -444,6 +438,25 @@ protected function buildPath(string $class): string
444438
) . DIRECTORY_SEPARATOR . $this->basename($file);
445439
}
446440

441+
/**
442+
* Gets the namespace from the command-line option,
443+
* or the default namespace if the option is not set.
444+
* Can be overridden by directly setting $this->namespace.
445+
*/
446+
protected function getNamespace(): string
447+
{
448+
return ! empty($this->namespace)
449+
? $this->namespace
450+
: trim(
451+
str_replace(
452+
'/',
453+
'\\',
454+
$this->getOption('namespace') ?? APP_NAMESPACE
455+
),
456+
'\\'
457+
);
458+
}
459+
447460
/**
448461
* Allows child generators to modify the internal `$hasClassName` flag.
449462
*
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Commands\Generators;
15+
16+
use CodeIgniter\CLI\BaseCommand;
17+
use CodeIgniter\CLI\GeneratorTrait;
18+
19+
/**
20+
* Generates a skeleton command file.
21+
*/
22+
class TestGenerator extends BaseCommand
23+
{
24+
use GeneratorTrait;
25+
26+
/**
27+
* The Command's Group
28+
*
29+
* @var string
30+
*/
31+
protected $group = 'Generators';
32+
33+
/**
34+
* The Command's Name
35+
*
36+
* @var string
37+
*/
38+
protected $name = 'make:test';
39+
40+
/**
41+
* The Command's Description
42+
*
43+
* @var string
44+
*/
45+
protected $description = 'Generates a new test file.';
46+
47+
/**
48+
* The Command's Usage
49+
*
50+
* @var string
51+
*/
52+
protected $usage = 'make:test <name> [options]';
53+
54+
/**
55+
* The Command's Arguments
56+
*
57+
* @var array<string, string>
58+
*/
59+
protected $arguments = [
60+
'name' => 'The command class name.',
61+
];
62+
63+
/**
64+
* The Command's Options
65+
*
66+
* @var array<string, string>
67+
*/
68+
protected $options = [
69+
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
70+
'--force' => 'Force overwrite existing file.',
71+
];
72+
73+
/**
74+
* Actually execute a command.
75+
*/
76+
public function run(array $params)
77+
{
78+
$this->component = 'Test';
79+
$this->template = 'test.tpl.php';
80+
$this->namespace = 'Tests';
81+
82+
$this->classNameLang = 'CLI.generator.className.entity';
83+
$this->generateClass($params);
84+
}
85+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<@php
2+
3+
namespace {namespace};
4+
5+
use CodeIgniter\Test\CIUnitTestCase;
6+
7+
class {class} extends CIUnitTestCase
8+
{
9+
protected function setUp(): void
10+
{
11+
parent::setUp();
12+
}
13+
14+
public function testExample(): void
15+
{
16+
//
17+
}
18+
}

system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReader.php

+28-2
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;
1515

16+
use Config\Routing;
1617
use ReflectionClass;
1718
use ReflectionMethod;
1819

@@ -33,13 +34,18 @@ final class ControllerMethodReader
3334
*/
3435
private array $httpMethods;
3536

37+
private bool $translateURIDashes;
38+
3639
/**
3740
* @param string $namespace the default namespace
3841
*/
3942
public function __construct(string $namespace, array $httpMethods)
4043
{
4144
$this->namespace = $namespace;
4245
$this->httpMethods = $httpMethods;
46+
47+
$config = config(Routing::class);
48+
$this->translateURIDashes = $config->translateURIDashes;
4349
}
4450

4551
/**
@@ -69,7 +75,7 @@ public function read(string $class, string $defaultController = 'Home', string $
6975
foreach ($this->httpMethods as $httpVerb) {
7076
if (strpos($methodName, strtolower($httpVerb)) === 0) {
7177
// Remove HTTP verb prefix.
72-
$methodInUri = lcfirst(substr($methodName, strlen($httpVerb)));
78+
$methodInUri = $this->getUriByMethod($httpVerb, $methodName);
7379

7480
// Check if it is the default method.
7581
if ($methodInUri === $defaultMethod) {
@@ -173,7 +179,27 @@ private function getUriByClass(string $classname): string
173179
$classPath .= lcfirst($part) . '/';
174180
}
175181

176-
return rtrim($classPath, '/');
182+
$classUri = rtrim($classPath, '/');
183+
184+
if ($this->translateURIDashes) {
185+
$classUri = str_replace('_', '-', $classUri);
186+
}
187+
188+
return $classUri;
189+
}
190+
191+
/**
192+
* @return string URI path part from the method
193+
*/
194+
private function getUriByMethod(string $httpVerb, string $methodName): string
195+
{
196+
$methodUri = lcfirst(substr($methodName, strlen($httpVerb)));
197+
198+
if ($this->translateURIDashes) {
199+
$methodUri = str_replace('_', '-', $methodUri);
200+
}
201+
202+
return $methodUri;
177203
}
178204

179205
/**

system/Config/AutoloadConfig.php

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ class AutoloadConfig
9393
protected $corePsr4 = [
9494
'CodeIgniter' => SYSTEMPATH,
9595
'Config' => APPPATH . 'Config',
96+
'Tests' => ROOTPATH . 'tests',
9697
];
9798

9899
/**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Commands;
15+
16+
use CodeIgniter\Test\CIUnitTestCase;
17+
use CodeIgniter\Test\StreamFilterTrait;
18+
19+
/**
20+
* @internal
21+
*
22+
* @group Others
23+
*/
24+
final class TestGeneratorTest extends CIUnitTestCase
25+
{
26+
use StreamFilterTrait;
27+
28+
protected function tearDown(): void
29+
{
30+
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer());
31+
$file = str_replace('ROOTPATH' . DIRECTORY_SEPARATOR, ROOTPATH, trim(substr($result, 14)));
32+
$dir = dirname($file);
33+
if (is_file($file)) {
34+
unlink($file);
35+
}
36+
if (is_dir($dir)) {
37+
rmdir($dir);
38+
}
39+
}
40+
41+
public function testGenerateTest(): void
42+
{
43+
command('make:test Foo/Bar');
44+
$this->assertFileExists(ROOTPATH . 'tests/Foo/Bar.php');
45+
}
46+
}

tests/system/Commands/Utilities/Routes/AutoRouterImproved/ControllerMethodReaderTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313

1414
namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved;
1515

16+
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller;
1617
use CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Home;
18+
use CodeIgniter\Config\Factories;
1719
use CodeIgniter\Test\CIUnitTestCase;
20+
use Config\Routing;
1821
use Tests\Support\Controllers\Newautorouting;
1922
use Tests\Support\Controllers\Remap;
2023

@@ -68,6 +71,43 @@ public function testRead(): void
6871
$this->assertSame($expected, $routes);
6972
}
7073

74+
public function testReadTranslateURIDashes(): void
75+
{
76+
$config = config(Routing::class);
77+
$config->translateURIDashes = true;
78+
Factories::injectMock('config', Routing::class, $config);
79+
80+
$reader = $this->createControllerMethodReader(
81+
'CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers'
82+
);
83+
84+
$routes = $reader->read(Dash_controller::class);
85+
86+
$expected = [
87+
0 => [
88+
'method' => 'get',
89+
'route' => 'dash-folder/dash-controller/somemethod',
90+
'route_params' => '[/..]',
91+
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller::getSomemethod',
92+
'params' => [
93+
'p1' => false,
94+
],
95+
],
96+
[
97+
'method' => 'get',
98+
'route' => 'dash-folder/dash-controller/dash-method',
99+
'route_params' => '/..[/..]',
100+
'handler' => '\CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder\Dash_controller::getDash_method',
101+
'params' => [
102+
'p1' => true,
103+
'p2' => false,
104+
],
105+
],
106+
];
107+
108+
$this->assertSame($expected, $routes);
109+
}
110+
71111
public function testReadDefaultController(): void
72112
{
73113
$reader = $this->createControllerMethodReader(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <[email protected]>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace CodeIgniter\Commands\Utilities\Routes\AutoRouterImproved\Controllers\Dash_folder;
15+
16+
use CodeIgniter\Controller;
17+
18+
class Dash_controller extends Controller
19+
{
20+
public function getSomemethod($p1 = ''): void
21+
{
22+
}
23+
24+
public function getDash_method($p1, $p2 = ''): void
25+
{
26+
}
27+
}

0 commit comments

Comments
 (0)