Skip to content

Commit

Permalink
post install script
Browse files Browse the repository at this point in the history
Signed-off-by: MarioRadu <[email protected]>
  • Loading branch information
MarioRadu committed Dec 2, 2024
1 parent 368e37c commit c8ed8b0
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 115 deletions.
47 changes: 47 additions & 0 deletions bin/composer-post-install-script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

Check warning on line 1 in bin/composer-post-install-script.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPCodeSniffer [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-ac...

A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 5 and the first side effect is on line 33.

declare(strict_types=1);

function copyFile(string $source, string $destination): void
{
if (! copy($source, $destination)) {
echo "Could not copy $source to $destination." . PHP_EOL;
exit(1);
}
}

function validateInput(array $options, string $projectRoot): array
{
$sourceFile = $options['sourceFile'] ?? null;
$destinationDirectory = $options['destinationDirectory'] ?? null;

$sourceFilePath = realpath($projectRoot . DIRECTORY_SEPARATOR . $sourceFile);
if (! $sourceFilePath || ! file_exists($sourceFilePath)) {
echo "Error: Source file $sourceFile does not exist." . PHP_EOL;
exit(1);
}

$destDirectoryPath = realpath($projectRoot . DIRECTORY_SEPARATOR . $destinationDirectory);
if (! $destDirectoryPath || ! is_dir($destDirectoryPath) || ! is_writable($destDirectoryPath)) {
echo "Error: Destination directory $destinationDirectory does not exist or is not writable." . PHP_EOL;
exit(1);
}

return [$sourceFilePath, $destDirectoryPath];
}

$projectRoot = realpath(__DIR__ . '/..');
$options = getopt("", ["sourceFile:", "destinationDirectory:"]);

[$sourceFilePath, $destinationDirectoryPath] = validateInput($options, $projectRoot);

$sourceFileName = basename($sourceFilePath);
$destinationFilePath = $destinationDirectoryPath . DIRECTORY_SEPARATOR . $sourceFileName;
$destinationFileWithoutDist = str_replace('.dist', '', $destinationFilePath);

if (! file_exists($destinationFileWithoutDist)) {
copyFile($sourceFilePath, $destinationFileWithoutDist);
echo "Files copied successfully from {$options['sourceFile']} to {$options['destinationDirectory']}." . PHP_EOL;
}

exit(0);
60 changes: 60 additions & 0 deletions bin/composer-post-package-install-script.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

Check warning on line 1 in bin/composer-post-package-install-script.php

View workflow job for this annotation

GitHub Actions / ci / QA Checks (PHPCodeSniffer [8.2, latest], ubuntu-latest, laminas/laminas-continuous-integration-ac...

A file should declare new symbols (classes, functions, constants, etc.) and cause no other side effects, or it should execute logic with side effects, but should not do both. The first symbol is defined on line 11 and the first side effect is on line 5.

declare(strict_types=1);

require __DIR__ . '/../vendor/autoload.php';

use Composer\InstalledVersions;

const VENDOR_DIR = 'vendor';

function copyFile(string $source, string $destination): void
{
if (! copy($source, $destination)) {
echo "Could not copy $source to $destination." . PHP_EOL;
exit(1);
}
}

function validateInput(array $options, string $projectRoot): array
{
$package = $options['package'] ?? null;
$sourceFile = $options['sourceFile'] ?? null;
$destinationDirectory = $options['destinationDirectory'] ?? null;

if (empty($package) || ! InstalledVersions::isInstalled($package)) {
echo "Error: Package $package is not installed." . PHP_EOL;
exit(1);
}

$vendorDir = $projectRoot . DIRECTORY_SEPARATOR . VENDOR_DIR;
$sourceFilePath = realpath($vendorDir . DIRECTORY_SEPARATOR . $package . DIRECTORY_SEPARATOR . $sourceFile);

if (! $sourceFilePath || ! file_exists($sourceFilePath)) {
echo "Error: Source file $sourceFile does not exist." . PHP_EOL;
exit(1);
}

$destDirectoryPath = realpath($projectRoot . DIRECTORY_SEPARATOR . $destinationDirectory);
if (! $destDirectoryPath || ! is_dir($destDirectoryPath) || ! is_writable($destDirectoryPath)) {
echo "Error: Destination directory $destinationDirectory does not exist or is not writable." . PHP_EOL;
exit(1);
}

return [$sourceFilePath, $destDirectoryPath];
}

$projectRoot = realpath(__DIR__ . '/..');
$options = getopt("", ["package:", "sourceFile:", "destinationDirectory:"]);

[$sourceFilePath, $destinationDirectoryPath] = validateInput($options, $projectRoot);

$sourceFileName = basename($sourceFilePath);
$destinationFilePath = $destinationDirectoryPath . DIRECTORY_SEPARATOR . $sourceFileName;
$destinationFileWithoutDist = str_replace('.dist', '', $destinationFilePath);

copyFile($sourceFilePath, $destinationFilePath);
copyFile($sourceFilePath, $destinationFileWithoutDist);

echo "Files copied successfully from {$options['package']}." . PHP_EOL;
exit(0);
115 changes: 0 additions & 115 deletions config/autoload/mail.local.php.dist

This file was deleted.

0 comments on commit c8ed8b0

Please sign in to comment.