-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a command to split a HAR file #16
- Loading branch information
Showing
7 changed files
with
203 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ build | |
.phpunit.result.cache | ||
vendor | ||
.php_cs.cache | ||
har.phar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#!/usr/bin/env php | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use Deviantintegral\Har\Command\SplitCommand; | ||
|
||
require_once __DIR__ . '/../vendor/autoload.php'; | ||
|
||
// Create the Application | ||
$application = new Symfony\Component\Console\Application; | ||
|
||
$application->add(new SplitCommand()); | ||
|
||
// Run it | ||
$application->run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Deviantintegral\Har\Command; | ||
|
||
use Deviantintegral\Har\Serializer; | ||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
|
||
/** | ||
* Command to split a HAR file into multiple files. | ||
*/ | ||
class SplitCommand extends Command | ||
{ | ||
protected function configure() | ||
{ | ||
parent::configure(); | ||
$this->setName('har:split') | ||
->setDescription('Split a HAR file into one file per entry') | ||
->setHelp('Each entry in the supplied HAR will be split into a single file.') | ||
->addArgument('har', InputArgument::REQUIRED, 'The source HAR file to split.') | ||
->addArgument('destination', InputArgument::OPTIONAL, 'The source directory to save the split files to. Defaults to the current directory.') | ||
->addOption('md5', null, InputOption::VALUE_NONE, 'Save split files with an MD5 hash of the request URL instead of a numeric index.') | ||
->addOption('force', 'f', InputOption::VALUE_NONE, 'Overwrite destination files that already exist.'); | ||
} | ||
|
||
protected function execute(InputInterface $input, OutputInterface $output) | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$source = $input->getArgument('har'); | ||
$contents = file_get_contents($source); | ||
$serializer = new Serializer(); | ||
$har = $serializer->deserializeHar($contents); | ||
|
||
$io->text(sprintf('Splitting %s into one file per entry', | ||
$source | ||
)); | ||
$io->progressStart(\count($har->getLog()->getEntries())); | ||
|
||
foreach ($har->splitLogEntries() as $index => $cloned) { | ||
$destination = $this->getSplitDestination( | ||
$index, | ||
$input->getOption('md5'), | ||
$cloned, | ||
$input->getArgument('destination') ?: getcwd() | ||
); | ||
|
||
if ($input->getOption('force') || !file_exists($destination)) { | ||
if (false === file_put_contents($destination, $serializer->serializeHar($cloned))) { | ||
throw new \RuntimeException(sprintf('Unable to write to %s.', $destination)); | ||
} | ||
} else { | ||
throw new \RuntimeException(sprintf('%s exists. Use --force to overwrite it and all other existing files', $destination)); | ||
} | ||
$io->progressAdvance(); | ||
} | ||
$io->progressFinish(); | ||
} | ||
|
||
/** | ||
* @param $index | ||
* @param $md5 | ||
* @param \Deviantintegral\Har\Har $cloned | ||
* @param string $destination_path | ||
* | ||
* @return string | ||
*/ | ||
private function getSplitDestination( | ||
$index, | ||
$md5, | ||
\Deviantintegral\Har\Har $cloned, | ||
string $destination_path | ||
): string { | ||
$filename = $index + 1 .'.har'; | ||
if ($md5) { | ||
$filename = md5( | ||
(string) $cloned->getLog()->getEntries()[0]->getRequest() | ||
->getUrl() | ||
).'.har'; | ||
} | ||
$destination = $destination_path."/$filename"; | ||
|
||
return $destination; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters