Skip to content

Commit

Permalink
create command: detect directory structure [closes #33]
Browse files Browse the repository at this point in the history
  • Loading branch information
hrach committed Dec 1, 2015
1 parent 0bf761a commit 4404844
Showing 1 changed file with 30 additions and 2 deletions.
32 changes: 30 additions & 2 deletions src/Bridges/SymfonyConsole/CreateCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,18 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
$dir = $this->getDirectory($input->getArgument('type'));
$name = $this->getFileName($input->getArgument('label'));
@mkdir($dir, 0777, TRUE); // directory may already exist
$file = "$dir/$name";

if ($this->hasNumericSubdirectory($dir, $foundYear)) {
if ($this->hasNumericSubdirectory($foundYear, $foundMonth)) {
$file = $dir . date('/Y/m/') . $name;
} else {
$file = $dir . date('/Y/') . $name;
}
} else {
$file = "$dir/$name";
}

@mkdir(dirname($file), 0777, TRUE); // directory may already exist
touch($file);
$output->writeln($file);
}
Expand Down Expand Up @@ -66,4 +76,22 @@ private function getFileName($label)
return date('Y-m-d-His-') . Strings::webalize($label, '.') . '.sql';
}


/**
* @param string $dir
* @param string|NULL $found
* @return bool
*/
private function hasNumericSubdirectory($dir, & $found)
{
$items = @scandir($dir); // directory may not exist
foreach ($items as $item) {
if (is_dir($dir . '/' . $item)) {
$found = $dir . '/' . $item;
return TRUE;
}
}
return FALSE;
}

}

0 comments on commit 4404844

Please sign in to comment.