Skip to content

Use optimized sprintf #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/AssetMapper/TypeScriptCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function supports(MappedAsset $asset): bool
foreach ($this->typeScriptFilesPaths as $path) {
$realTypeScriptPath = realpath($path);
if (false === $realTypeScriptPath) {
throw new \Exception(sprintf('The TypeScript directory "%s" does not exist', $path));
throw new \Exception(\sprintf('The TypeScript directory "%s" does not exist', $path));
}
// If the asset matches one of the TypeScript files source paths
if ($realSourcePath === $realTypeScriptPath) {
Expand All @@ -46,19 +46,19 @@ public function supports(MappedAsset $asset): bool
}
}

throw new \Exception(sprintf('The TypeScript file "%s" is not in the TypeScript files paths. Check the asset path or your "sensiolabs_typescript.source_dir" in your config', $realSourcePath));
throw new \Exception(\sprintf('The TypeScript file "%s" is not in the TypeScript files paths. Check the asset path or your "sensiolabs_typescript.source_dir" in your config', $realSourcePath));
}

public function compile(string $content, MappedAsset $asset, AssetMapperInterface $assetMapper): string
{
$realSourcePath = realpath($asset->sourcePath);
if (false === $realSourcePath) {
throw new \Exception(sprintf('The TypeScript file "%s" does not exist', $asset->sourcePath));
throw new \Exception(\sprintf('The TypeScript file "%s" does not exist', $asset->sourcePath));
}
foreach ($this->typeScriptFilesPaths as $typeScriptFilesPath) {
$realTypeScriptPath = realpath($typeScriptFilesPath);
if (false === $realTypeScriptPath) {
throw new \Exception(sprintf('The TypeScript directory "%s" does not exist', $typeScriptFilesPath));
throw new \Exception(\sprintf('The TypeScript directory "%s" does not exist', $typeScriptFilesPath));
}
if (str_starts_with($realSourcePath, $realTypeScriptPath)) {
$fileName = basename($realSourcePath, '.ts');
Expand All @@ -69,7 +69,7 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
}

if (!isset($jsFile)) {
throw new \Exception(sprintf('The TypeScript file "%s" is not in the TypeScript files paths. Check the asset path or your "sensiolabs_typescript.source_dir" in your config', $asset->sourcePath));
throw new \Exception(\sprintf('The TypeScript file "%s" is not in the TypeScript files paths. Check the asset path or your "sensiolabs_typescript.source_dir" in your config', $asset->sourcePath));
}
$asset->addFileDependency($jsFile);

Expand Down
2 changes: 1 addition & 1 deletion src/Tools/TypeScriptBinary.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public function __construct(
private readonly string $pathToExecutable,
) {
if (!file_exists($this->pathToExecutable)) {
throw new \Exception(sprintf('The Typescript binary could not be found at the provided path : "%s"', $this->pathToExecutable));
throw new \Exception(\sprintf('The Typescript binary could not be found at the provided path : "%s"', $this->pathToExecutable));
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/Tools/TypeScriptBinaryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public static function getBinaryNameFromServerSpecs(
return 'swc-darwin-x64';
}

throw new \Exception(sprintf('No matching machine found for Darwin platform (Machine: %s).', $machine));
throw new \Exception(\sprintf('No matching machine found for Darwin platform (Machine: %s).', $machine));
}

if (str_contains($os, 'linux')) {
Expand All @@ -76,7 +76,7 @@ public static function getBinaryNameFromServerSpecs(
return 'swc-linux-x64-'.$kernelVersion;
}

throw new \Exception(sprintf('No matching machine found for Linux platform (Machine: %s).', $machine));
throw new \Exception(\sprintf('No matching machine found for Linux platform (Machine: %s).', $machine));
}

if (str_contains($os, 'win')) {
Expand All @@ -90,10 +90,10 @@ public static function getBinaryNameFromServerSpecs(
return 'swc-win32-ia32-msvc.exe';
}

throw new \Exception(sprintf('No matching machine found for Windows platform (Machine: %s).', $machine));
throw new \Exception(\sprintf('No matching machine found for Windows platform (Machine: %s).', $machine));
}

throw new \Exception(sprintf('Unknown platform or architecture (OS: %s, Machine: %s).', $os, $machine));
throw new \Exception(\sprintf('Unknown platform or architecture (OS: %s, Machine: %s).', $os, $machine));
}

private function downloadAndExtract(string $binaryName): void
Expand All @@ -105,10 +105,10 @@ private function downloadAndExtract(string $binaryName): void
if (file_exists($targetPath)) {
return;
}
$url = sprintf(self::SWC_RELEASE_URL_PATTERN, self::VERSION, $binaryName);
$url = \sprintf(self::SWC_RELEASE_URL_PATTERN, self::VERSION, $binaryName);

if ($this->output->isVerbose()) {
$this->output->note(sprintf('Downloading SWC binary from "%s" to "%s"...', $url, $targetPath));
$this->output->note(\sprintf('Downloading SWC binary from "%s" to "%s"...', $url, $targetPath));
} else {
$this->output->note('Downloading SWC binary ...');
}
Expand All @@ -128,7 +128,7 @@ private function downloadAndExtract(string $binaryName): void
]);
$fileHandler = fopen($targetPath, 'w');
if (false === $fileHandler) {
throw new \Exception(sprintf('Could not open file "%s" for writing.', $targetPath));
throw new \Exception(\sprintf('Could not open file "%s" for writing.', $targetPath));
}
foreach ($this->httpClient->stream($response) as $chunk) {
fwrite($fileHandler, $chunk->getContent());
Expand Down
4 changes: 2 additions & 2 deletions src/Tools/WatcherBinaryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public function getBinaryFromServerSpecs(string $os): WatcherBinary
$binaryName = self::getBinaryNameFromServerSpecs($os);
$binaryPath = __DIR__.'/watcher/'.$binaryName;
if (!file_exists($binaryPath)) {
throw new \Exception(sprintf('The watcher binary could not be found at the provided path : "%s"', $binaryPath));
throw new \Exception(\sprintf('The watcher binary could not be found at the provided path : "%s"', $binaryPath));
}

return new WatcherBinary($binaryPath);
Expand All @@ -28,6 +28,6 @@ public static function getBinaryNameFromServerSpecs(string $os): string
return 'watcher-windows.exe';
}

throw new \Exception(sprintf('Unknown platform or architecture (OS: %s).', $os));
throw new \Exception(\sprintf('Unknown platform or architecture (OS: %s).', $os));
}
}
4 changes: 2 additions & 2 deletions src/TypeScriptBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ private function createBuildProcess(string $path, bool $watch = false): Process
$fs = new Filesystem();
$relativePath = rtrim($fs->makePathRelative($path, $this->projectRootDir), '/');
if (str_starts_with($relativePath, '..')) {
throw new \Exception(sprintf('The TypeScript file "%s" is not in the project directory "%s".', $path, $this->projectRootDir));
throw new \Exception(\sprintf('The TypeScript file "%s" is not in the project directory "%s".', $path, $this->projectRootDir));
}
if ($this->configFile && file_exists($this->configFile)) {
$args = array_merge($args, ['--config-file', trim($fs->makePathRelative($this->configFile, $this->projectRootDir), '/')]);
}
$buildProcess = $this->getBuildBinary()->createProcess(array_merge(['compile', $relativePath], $args));
$buildProcess->setWorkingDirectory($this->projectRootDir);

$this->output->note(sprintf('Executing SWC compile on %s.', $relativePath));
$this->output->note(\sprintf('Executing SWC compile on %s.', $relativePath));
if ($this->output->isVerbose()) {
$this->output->writeln([
' Command:',
Expand Down
Loading