Added RegexTrait

This commit is contained in:
Eric Wheeler 2015-12-16 17:49:30 -08:00
parent ab98dcec8b
commit 85ef915676
2 changed files with 98 additions and 36 deletions

View File

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

View File

@ -0,0 +1,36 @@
<?php
/**
* Created by PhpStorm.
* User: eric
* Date: 12/14/15
* Time: 10:28 PM
*/
namespace Sikofitt\HathorBundle\Common;
trait RegexTrait
{
/**
* @var string
*/
private $regex;
/**
* @param $part
* Part of the regex to build.
*/
final public function add($part)
{
$this->regex .= $part;
}
/**
* @return string
*/
final public function build()
{
return $this->regex;
}
}