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;
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' => '<fg=cyan;options=bold>#</>',
'progressCharacter' => '<fg=cyan>#</>',
'emptyBarCharacter' => '<fg=cyan>#</>',
'message' => '<fg=white;options=bold>%message%</>',
'current' => '<fg=cyan>%current%</>',
'currentMaxSep' => '<fg=black;options=bold>/</>',
'max' => '<fg=cyan;options=bold>%max%</>',
'percent' => '%percent:3s%%',
'elapsed' => '%elapsed:6s%',
'estimated' => '%estimated:-6s%',
'elapsedEstimatedSep' => '/',
'memory' => '%memory:6s%',
'bar' => '<fg=cyan>[</>%bar%<fg=cyan>]</>',
];
$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('<fg=cyan;options=bold>#</>');
$progress->setProgressCharacter('<fg=cyan>#</>');
$progress->setEmptyBarCharacter('<fg=cyan>#</>');
//$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('<warning>Caught RuntimeException</warning>');
$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' => '<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;
}
}