Skip to content

Commit 4030729

Browse files
committed
create command: detect directory structure [closes #33]
1 parent 0bf761a commit 4030729

File tree

1 file changed

+30
-2
lines changed

1 file changed

+30
-2
lines changed

src/Bridges/SymfonyConsole/CreateCommand.php

+30-2
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
3434
{
3535
$dir = $this->getDirectory($input->getArgument('type'));
3636
$name = $this->getFileName($input->getArgument('label'));
37-
@mkdir($dir, 0777, TRUE); // directory may already exist
38-
$file = "$dir/$name";
37+
38+
if ($this->hasNumericSubdirectory($dir, $foundYear)) {
39+
if ($this->hasNumericSubdirectory($foundYear, $foundMonth)) {
40+
$file = $dir . date('/Y/m/') . $name;
41+
} else {
42+
$file = $dir . date('/Y/') . $name;
43+
}
44+
} else {
45+
$file = "$dir/$name";
46+
}
47+
48+
@mkdir(dirname($file), 0777, TRUE); // directory may already exist
3949
touch($file);
4050
$output->writeln($file);
4151
}
@@ -66,4 +76,22 @@ private function getFileName($label)
6676
return date('Y-m-d-His-') . Strings::webalize($label, '.') . '.sql';
6777
}
6878

79+
80+
/**
81+
* @param string $dir
82+
* @param string|NULL $found
83+
* @return bool
84+
*/
85+
private function hasNumericSubdirectory($dir, & $found)
86+
{
87+
$items = @scandir($dir); // directory may not exist
88+
foreach ($items as $item) {
89+
if ($item !== '.' && $item !== '..' && is_dir($dir . '/' . $item)) {
90+
$found = $dir . '/' . $item;
91+
return TRUE;
92+
}
93+
}
94+
return FALSE;
95+
}
96+
6997
}

0 commit comments

Comments
 (0)