Skip to content

Commit 3052a34

Browse files
Added nested path
1 parent 98071b6 commit 3052a34

File tree

3 files changed

+142
-2
lines changed

3 files changed

+142
-2
lines changed
Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,59 @@
1-
# Creating Action
1+
# Creating Actions
2+
3+
To create an action use the `make:migration:action` artisan command:
4+
5+
```bash
6+
php artisan make:migration:action some_name
7+
```
8+
9+
The new action's file will be placed in your `actions` directory in the base path of your app.
10+
11+
Each action file name contains a timestamp, which allows Laravel to determine the order of the actions.
12+
13+
14+
## Automatically Generate A File Name
15+
16+
If you do not specify the "name" attribute, then the file name will be generated automatically according to the rule:
17+
18+
> git branch name ?: 'auto'
19+
20+
```bash
21+
php artisan make:migration:action
22+
23+
### When the git repository is found (`base_path('.git')` directory is exists).
24+
### For example, HEAD branch name is 'qwerty'.
25+
# 2022_10_11_225116_qwerty.php
26+
# 2022_10_11_225118_qwerty.php
27+
# 2022_10_11_225227_qwerty.php
28+
29+
### When the git repository is not found (`base_path('.git')` directory doesn't exists).
30+
### For example, HEAD branch name is 'qwerty'.
31+
# 2022_10_11_225116_auto.php
32+
# 2022_10_11_225118_auto.php
33+
# 2022_10_11_225227_auto.php
34+
```
35+
36+
## Nested Files
37+
38+
Since version `3.0` you can use nested paths to create actions:
39+
40+
```bash
41+
php artisan make:migration:action Foo/Bar/QweRty
42+
php artisan make:migration:action Foo/Bar/QweRty.php
43+
44+
php artisan make:migration:action Foo\Bar\QweRty
45+
php artisan make:migration:action Foo\Bar\QweRty.php
46+
47+
php artisan make:migration:action foo\bar\QweRty
48+
php artisan make:migration:action foo\bar\QweRty.php
49+
```
50+
51+
All of these commands will create a file called `actions/foo/bar/Y_m_d_His_qwe_rty.php`.
52+
53+
For example:
54+
55+
```bash
56+
php artisan make:migration:action foo\bar\QweRty
57+
58+
# actions/foo/bar/2022_10_11_225734_qwe_rty.php
59+
```

src/Values/Options.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,16 @@ class Options extends DataTransferObject
2525

2626
protected function castName(?string $value): ?string
2727
{
28-
return empty($value) ? null : Str::snake($value);
28+
if (empty($value)) {
29+
return null;
30+
}
31+
32+
return Str::of($value)
33+
->replace('\\', '/')
34+
->replace('.php', '')
35+
->explode('/')
36+
->map(fn (string $path) => Str::snake($path))
37+
->implode(DIRECTORY_SEPARATOR)
38+
->toString();
2939
}
3040
}

tests/Commands/MakeTest.php

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,76 @@ public function testAutoName()
3535

3636
$this->assertFileExists($path);
3737
}
38+
39+
public function testNestedRightSlashWithoutExtension()
40+
{
41+
$name = 'Foo/bar/QweRty';
42+
43+
$path = $this->getActionsPath() . '/foo/bar/' . date('Y_m_d_His') . '_qwe_rty.php';
44+
45+
$this->assertFileDoesNotExist($path);
46+
47+
$this->artisan(Names::MAKE, compact('name'))->assertExitCode(0);
48+
49+
$this->assertFileExists($path);
50+
51+
$this->assertEquals(
52+
file_get_contents(__DIR__ . '/../fixtures/app/stubs/make_example.stub'),
53+
file_get_contents($path)
54+
);
55+
}
56+
57+
public function testNestedRightSlashWithExtension()
58+
{
59+
$name = 'Foo/bar/QweRty.php';
60+
61+
$path = $this->getActionsPath() . '/foo/bar/' . date('Y_m_d_His') . '_qwe_rty.php';
62+
63+
$this->assertFileDoesNotExist($path);
64+
65+
$this->artisan(Names::MAKE, compact('name'))->assertExitCode(0);
66+
67+
$this->assertFileExists($path);
68+
69+
$this->assertEquals(
70+
file_get_contents(__DIR__ . '/../fixtures/app/stubs/make_example.stub'),
71+
file_get_contents($path)
72+
);
73+
}
74+
75+
public function testNestedLeftSlashWithoutExtension()
76+
{
77+
$name = 'Foo\\bar\\QweRty';
78+
79+
$path = $this->getActionsPath() . '/foo/bar/' . date('Y_m_d_His') . '_qwe_rty.php';
80+
81+
$this->assertFileDoesNotExist($path);
82+
83+
$this->artisan(Names::MAKE, compact('name'))->assertExitCode(0);
84+
85+
$this->assertFileExists($path);
86+
87+
$this->assertEquals(
88+
file_get_contents(__DIR__ . '/../fixtures/app/stubs/make_example.stub'),
89+
file_get_contents($path)
90+
);
91+
}
92+
93+
public function testNestedLeftSlashWithExtension()
94+
{
95+
$name = 'Foo\\bar\\QweRty.php';
96+
97+
$path = $this->getActionsPath() . '/foo/bar/' . date('Y_m_d_His') . '_qwe_rty.php';
98+
99+
$this->assertFileDoesNotExist($path);
100+
101+
$this->artisan(Names::MAKE, compact('name'))->assertExitCode(0);
102+
103+
$this->assertFileExists($path);
104+
105+
$this->assertEquals(
106+
file_get_contents(__DIR__ . '/../fixtures/app/stubs/make_example.stub'),
107+
file_get_contents($path)
108+
);
109+
}
38110
}

0 commit comments

Comments
 (0)