diff --git a/src/Sikofitt/HathorBundle/Command/ScannerCommand.php b/src/Sikofitt/HathorBundle/Command/ScannerCommand.php index 09e13cd..9fbe6a4 100644 --- a/src/Sikofitt/HathorBundle/Command/ScannerCommand.php +++ b/src/Sikofitt/HathorBundle/Command/ScannerCommand.php @@ -5,6 +5,8 @@ namespace Sikofitt\HathorBundle\Command; +use Sikofitt\HathorBundle\Common\RegexTrait; +use Symfony\Component\Console\Style\SymfonyStyle; use Symfony\Component\Finder\Finder; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputOption; @@ -12,11 +14,12 @@ use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Helper\ProgressBar; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand as BaseCommand; -use Sikofitt\HathorBundle\Common\Music\ID3; - +//use Sikofitt\HathorBundle\Common\Music\ID3; +use Sikofitt\PhpId3; class ScannerCommand extends BaseCommand { + use RegexTrait; protected function configure() { @@ -36,12 +39,15 @@ class ScannerCommand extends BaseCommand $extensions = $input->getArgument('extensions'); // \b(\.mp2|\.mp3|\.mp4|\.wma|\.ogg)\b$ + $this->add('/\b('); $extensionRegex = '/\b('; foreach($extensions as $extension) { + $this->add(sprintf('\.%s|',$extension)); $extensionRegex .= sprintf('\.%s|',$extension); } - + $this->add(')\b$/i'); + $regex = $this->build(); $extensionRegex[strlen($extensionRegex)-1] = ')'; $extensionRegex .= '\b$/i'; @@ -55,51 +61,51 @@ class ScannerCommand extends BaseCommand ->name($extensionRegex); $progress = new ProgressBar($output, $finder->count()); $cols = shell_exec('tput cols'); - $progress->setBarWidth($cols - 2); - $format = [ - 'barCharacter' => '#', - 'progressCharacter' => '#', - 'emptyBarCharacter' => '#', - 'message' => '%message%', - 'current' => '%current%', - 'currentMaxSep' => '/', - 'max' => '%max%', - 'percent' => '%percent:3s%%', - 'elapsed' => '%elapsed:6s%', - 'estimated' => '%estimated:-6s%', - 'elapsedEstimatedSep' => '/', - 'memory' => '%memory:6s%', - 'bar' => '[%bar%]', - ]; - $format = (object)$format; - $progress->setFormat(""); + $maxCount = strlen($finder->count()) * 2; + $otherCount = 12; + $progress->setBarWidth($cols - ($maxCount+$otherCount)); + $format = $this->getCustomFormat(); + $progress->setFormat( + $format->message . + $format->current . + $format->currentMaxSep . + $format->max . + $format->bar . + $format->percent . + $format->elapsed . + $format->elapsedEstimatedSep . + $format->estimated . + $format->memory + ); $progress->clear(); $progress->start(); - $getID3 = new ID3; - $progress->setBarCharacter('#'); - $progress->setProgressCharacter('#'); - $progress->setEmptyBarCharacter('#'); - //$progress->setRedrawFrequency(100); + $getID3 = new PhpId3; + $progress->setBarCharacter($format->barCharacter); + $progress->setProgressCharacter($format->progressCharacter); + $progress->setEmptyBarCharacter($format->emptyBarCharacter); + $io = new SymfonyStyle($input, $output); foreach ($finder as $file) { $fileLength = mb_strlen($file->getFilename()); - if($fileLength >= (int)$cols) + if($fileLength > ($cols - $progress->getBarWidth()) + $otherCount) { - $fileName = substr($file->getFilename(), 0, (int)$cols -60); + $fileName = substr($file->getFilename(), 0, (int)$cols - $otherCount); + } else { - $fileName = $file->getFilename(); + $fileName = $file->getFilename(); } -// $currentFile = str_pad($fileName . ' ...', ($cols - strlen(' ...')), ' '); - $currentFile = $fileName; + $currentFile = str_pad($fileName . ' ...', ($cols - $otherCount + strlen('...') + 1 ), ' '); + $progress->setMessage($currentFile); - $getID3->setFile($file->getRealPath()); + $getID3->setFileName($file->getRealPath()); try { - - $allMusicData[] = $getID3->getFileData (); + $allMusicData[] = $getID3->getInfo (); } catch(\Exception $e) { - $progress->setMessage('Caught RuntimeException'); + $io->warning(['','Exception was caught.', 'Message : ' . $e->getMessage()]); + + continue; } @@ -108,10 +114,30 @@ class ScannerCommand extends BaseCommand } $progress->setMessage('Finished!'); $progress->finish(); - $output->writeln(''); + $io->writeln(PHP_EOL); + //$io->warning($regex); + $io->success('Finished!'); //var_dump($allMusicData); } + + private function getCustomFormat() { + return (object)[ + 'barCharacter' => '#', + 'progressCharacter' => '#', + 'emptyBarCharacter' => '#', + 'message' => ' %message%' . PHP_EOL, + 'current' => ' %current%', + 'currentMaxSep' => '/', + 'max' => '%max%', + 'percent' => ' %percent:3s%%' .PHP_EOL, + 'elapsed' => ' %elapsed:6s% Elapsed', + 'estimated' => '%estimated:-6s% Remaining ', + 'elapsedEstimatedSep' => ' / ', + 'memory' => '(%memory:6s%)', + 'bar' => ' [%bar%]', + ]; + } } diff --git a/src/Sikofitt/HathorBundle/Common/RegexTrait.php b/src/Sikofitt/HathorBundle/Common/RegexTrait.php new file mode 100644 index 0000000..8a64e57 --- /dev/null +++ b/src/Sikofitt/HathorBundle/Common/RegexTrait.php @@ -0,0 +1,36 @@ +regex .= $part; + + } + + /** + * @return string + */ + final public function build() + { + return $this->regex; + } +}