Refactor variable names for consistency and clarity in ShutdownHandler and PackageWriter classes

This commit is contained in:
netkas 2025-03-11 13:56:05 -04:00
parent 1e45b47f0f
commit 487eb70b0b
Signed by: netkas
GPG key ID: 4D8629441B76E4CC
5 changed files with 233 additions and 231 deletions

View file

@ -9,6 +9,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
This update introduces a quality of life improvement
### Changed
- Refactor variable names for consistency and clarity in ShutdownHandler and PackageWriter classes
## [2.1.6] - 2024-10-29

View file

@ -47,32 +47,32 @@
/**
* @var int
*/
private $package_offset;
private $packageOffset;
/**
* @var int
*/
private $package_length;
private $packageLength;
/**
* @var int
*/
private $header_offset;
private $headerOffset;
/**
* @var int
*/
private $header_length;
private $headerLength;
/**
* @var int
*/
private $data_offset;
private $dataOffset;
/**
* @var int
*/
private $data_length;
private $dataLength;
/**
* @var array
@ -82,12 +82,12 @@
/**
* @var resource
*/
private $package_file;
private $packageFile;
/**
* @var string
*/
private $package_path;
private $packagePath;
/**
* @var array
@ -107,10 +107,10 @@
throw new IOException(sprintf('File \'%s\' does not exist', $file_path));
}
$this->package_path = $file_path;
$this->package_file = fopen($file_path, 'rb');
$this->packagePath = $file_path;
$this->packageFile = fopen($file_path, 'rb');
if($this->package_file === false)
if($this->packageFile === false)
{
throw new IOException(sprintf('Failed to open file \'%s\'', $file_path));
}
@ -122,41 +122,41 @@
// End of data: \xFF\xAA\x55\xF0
// First find the offset of the package by searching for the magic bytes "ncc_pkg"
$this->package_offset = 0;
while(!feof($this->package_file))
$this->packageOffset = 0;
while(!feof($this->packageFile))
{
$buffer = fread($this->package_file, 1024);
$buffer = fread($this->packageFile, 1024);
$buffer_length = strlen($buffer);
$this->package_offset += $buffer_length;
$this->packageOffset += $buffer_length;
if (($position = strpos($buffer, "ncc_pkg")) !== false)
{
$this->package_offset -= $buffer_length - $position;
$this->package_length = 7; // ncc_pkg
$this->header_offset = $this->package_offset + 7;
$this->packageOffset -= $buffer_length - $position;
$this->packageLength = 7; // ncc_pkg
$this->headerOffset = $this->packageOffset + 7;
break;
}
}
// Check for sanity reasons
if($this->package_offset === null || $this->package_length === null)
if($this->packageOffset === null || $this->packageLength === null)
{
throw new IOException(sprintf('File \'%s\' is not a valid package file (missing magic bytes)', $file_path));
}
// Seek the header until the end of headers byte sequence (1F 1F 1F 1F)
fseek($this->package_file, $this->header_offset);
while (!feof($this->package_file))
fseek($this->packageFile, $this->headerOffset);
while (!feof($this->packageFile))
{
$this->headers .= fread($this->package_file, 1024);
$this->headers .= fread($this->packageFile, 1024);
// Search for the position of "1F 1F 1F 1F" within the buffer
if (($position = strpos($this->headers, "\x1F\x1F\x1F\x1F")) !== false)
{
$this->headers = substr($this->headers, 0, $position);
$this->header_length = strlen($this->headers);
$this->package_length += $this->header_length + 4;
$this->data_offset = $this->header_offset + $this->header_length + 4;
$this->headerLength = strlen($this->headers);
$this->packageLength += $this->headerLength + 4;
$this->dataOffset = $this->headerOffset + $this->headerLength + 4;
break;
}
@ -181,19 +181,19 @@
}
// Seek the data until the end of the package (FF AA 55 F0)
fseek($this->package_file, $this->data_offset);
fseek($this->packageFile, $this->dataOffset);
$buffer = '';
while(!feof($this->package_file))
while(!feof($this->packageFile))
{
$current_chunk = fread($this->package_file, 1024);
$this->data_length += strlen($current_chunk);
$current_chunk = fread($this->packageFile, 1024);
$this->dataLength += strlen($current_chunk);
$buffer .= $current_chunk;
// If we detect the end-of-data byte sequence
if (($position = strpos($buffer, "\xFF\xAA\x55\xF0")) !== false)
{
$this->data_length -= strlen($buffer) - $position;
$this->package_length += $this->data_length + 4;
$this->dataLength -= strlen($buffer) - $position;
$this->packageLength += $this->dataLength + 4;
break;
}
@ -206,7 +206,7 @@
}
if($this->data_length === null || $this->data_length === 0)
if($this->dataLength === null || $this->dataLength === 0)
{
throw new IOException(sprintf('File \'%s\' is not a valid package file (missing end of package)', $file_path));
}
@ -275,18 +275,18 @@
{
if(!isset($this->headers[PackageStructure::DIRECTORY->value][$name]))
{
throw new RuntimeException(sprintf('File \'%s\' not found in package \'%s\'', $name, $this->package_path));
throw new RuntimeException(sprintf('File \'%s\' not found in package \'%s\'', $name, $this->packagePath));
}
$location = explode(':', $this->headers[PackageStructure::DIRECTORY->value][$name]);
fseek($this->package_file, ($this->data_offset + (int)$location[0]));
fseek($this->packageFile, ($this->dataOffset + (int)$location[0]));
if(in_array(PackageFlags::COMPRESSION->value, $this->headers[PackageStructure::FLAGS->value], true))
{
return gzuncompress(fread($this->package_file, (int)$location[1]));
return gzuncompress(fread($this->packageFile, (int)$location[1]));
}
return fread($this->package_file, (int)$location[1]);
return fread($this->packageFile, (int)$location[1]);
}
/**
@ -299,7 +299,7 @@
{
if(!isset($this->headers[PackageStructure::DIRECTORY->value][$name]))
{
throw new RuntimeException(sprintf('Resource \'%s\' not found in package \'%s\'', $name, $this->package_path));
throw new RuntimeException(sprintf('Resource \'%s\' not found in package \'%s\'', $name, $this->packagePath));
}
$location = explode(':', $this->headers[PackageStructure::DIRECTORY->value][$name]);
@ -326,8 +326,8 @@
*/
public function getByPointer(int $pointer, int $length): string
{
fseek($this->package_file, ($this->header_length + $pointer));
return fread($this->package_file, $length);
fseek($this->packageFile, ($this->headerLength + $pointer));
return fread($this->packageFile, $length);
}
/**
@ -348,7 +348,7 @@
if(!isset($this->headers[PackageStructure::DIRECTORY->value][$directory]))
{
throw new ConfigurationException(sprintf('Assembly object not found in package \'%s\'', $this->package_path));
throw new ConfigurationException(sprintf('Assembly object not found in package \'%s\'', $this->packagePath));
}
try
@ -357,7 +357,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode assembly from package \'%s\' using ZiProto: %s', $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode assembly from package \'%s\' using ZiProto: %s', $this->packagePath, $e->getMessage()), $e);
}
$this->cache[$directory] = $assembly;
@ -382,7 +382,7 @@
if(!isset($this->headers[PackageStructure::DIRECTORY->value][$directory]))
{
throw new ConfigurationException(sprintf('Metadata object not found in package \'%s\'', $this->package_path));
throw new ConfigurationException(sprintf('Metadata object not found in package \'%s\'', $this->packagePath));
}
try
@ -391,7 +391,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode metadata from package \'%s\' using ZiProto: %s', $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode metadata from package \'%s\' using ZiProto: %s', $this->packagePath, $e->getMessage()), $e);
}
foreach($this->getFlags() as $flag)
@ -429,7 +429,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode installer from package \'%s\' using ZiProto: %s', $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode installer from package \'%s\' using ZiProto: %s', $this->packagePath, $e->getMessage()), $e);
}
$this->cache[$directory] = $installer;
@ -470,7 +470,7 @@
$dependency_name = sprintf('@%s:%s', PackageDirectory::DEPENDENCIES->value, $name);
if(!isset($this->headers[PackageStructure::DIRECTORY->value][$dependency_name]))
{
throw new ConfigurationException(sprintf('Dependency \'%s\' not found in package \'%s\'', $name, $this->package_path));
throw new ConfigurationException(sprintf('Dependency \'%s\' not found in package \'%s\'', $name, $this->packagePath));
}
try
@ -479,7 +479,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode dependency \'%s\' from package \'%s\' using ZiProto: %s', $name, $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode dependency \'%s\' from package \'%s\' using ZiProto: %s', $name, $this->packagePath, $e->getMessage()), $e);
}
}
@ -499,7 +499,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode dependency from pointer \'%s\' with length \'%s\' from package \'%s\' using ZiProto: %s', $pointer, $length, $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode dependency from pointer \'%s\' with length \'%s\' from package \'%s\' using ZiProto: %s', $pointer, $length, $this->packagePath, $e->getMessage()), $e);
}
}
@ -537,7 +537,7 @@
$execution_unit_name = sprintf('@%s:%s', PackageDirectory::EXECUTION_UNITS->value, $name);
if(!isset($this->headers[PackageStructure::DIRECTORY->value][$execution_unit_name]))
{
throw new ConfigurationException(sprintf('Execution unit \'%s\' not found in package \'%s\'', $name, $this->package_path));
throw new ConfigurationException(sprintf('Execution unit \'%s\' not found in package \'%s\'', $name, $this->packagePath));
}
try
@ -546,7 +546,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode execution unit \'%s\' from package file \'%s\' using ZiProto: %s', $name, $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode execution unit \'%s\' from package file \'%s\' using ZiProto: %s', $name, $this->packagePath, $e->getMessage()), $e);
}
}
@ -577,7 +577,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode execution unit from pointer \'%s\' with length \'%s\' from package \'%s\' using ZiProto: %s', $pointer, $length, $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode execution unit from pointer \'%s\' with length \'%s\' from package \'%s\' using ZiProto: %s', $pointer, $length, $this->packagePath, $e->getMessage()), $e);
}
}
@ -636,7 +636,7 @@
$component_name = sprintf('@%s:%s', PackageDirectory::COMPONENTS->value, $name);
if(!isset($this->headers[PackageStructure::DIRECTORY->value][$component_name]))
{
throw new ConfigurationException(sprintf('Component \'%s\' not found in package \'%s\'', $name, $this->package_path));
throw new ConfigurationException(sprintf('Component \'%s\' not found in package \'%s\'', $name, $this->packagePath));
}
try
@ -645,7 +645,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode component \'%s\' from package \'%s\' using ZiProto: %s', $name, $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode component \'%s\' from package \'%s\' using ZiProto: %s', $name, $this->packagePath, $e->getMessage()), $e);
}
}
@ -665,7 +665,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode component from pointer \'%s\' with length \'%s\' from package \'%s\' using ZiProto: %s', $pointer, $length, $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode component from pointer \'%s\' with length \'%s\' from package \'%s\' using ZiProto: %s', $pointer, $length, $this->packagePath, $e->getMessage()), $e);
}
}
@ -682,7 +682,7 @@
$class_name = sprintf('@%s:%s', PackageDirectory::CLASS_POINTER->value, $class);
if(!isset($this->headers[PackageStructure::DIRECTORY->value][$class_name]))
{
throw new ConfigurationException(sprintf('Class map \'%s\' not found in package \'%s\'', $class, $this->package_path));
throw new ConfigurationException(sprintf('Class map \'%s\' not found in package \'%s\'', $class, $this->packagePath));
}
try
@ -691,7 +691,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode component from class pointer \'%s\' from package \'%s\' using ZiProto: %s', $class, $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode component from class pointer \'%s\' from package \'%s\' using ZiProto: %s', $class, $this->packagePath, $e->getMessage()), $e);
}
}
@ -729,7 +729,7 @@
$resource_name = sprintf('@%s:%s', PackageDirectory::RESOURCES->value, $name);
if(!isset($this->headers[PackageStructure::DIRECTORY->value][$resource_name]))
{
throw new ConfigurationException(sprintf('Resource \'%s\' not found in package \'%s\'', $name, $this->package_path));
throw new ConfigurationException(sprintf('Resource \'%s\' not found in package \'%s\'', $name, $this->packagePath));
}
try
@ -738,7 +738,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode resource \'%s\' from package \'%s\' using ZiProto: %s', $name, $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode resource \'%s\' from package \'%s\' using ZiProto: %s', $name, $this->packagePath, $e->getMessage()), $e);
}
}
@ -758,7 +758,7 @@
}
catch(Exception $e)
{
throw new IntegrityException(sprintf('Failed to decode resource from pointer \'%s\' with length \'%s\' from package \'%s\' using ZiProto: %s', $pointer, $length, $this->package_path, $e->getMessage()), $e);
throw new IntegrityException(sprintf('Failed to decode resource from pointer \'%s\' with length \'%s\' from package \'%s\' using ZiProto: %s', $pointer, $length, $this->packagePath, $e->getMessage()), $e);
}
}
@ -788,7 +788,7 @@
*/
public function getPackageOffset(): int
{
return $this->package_offset;
return $this->packageOffset;
}
/**
@ -796,7 +796,7 @@
*/
public function getPackageLength(): int
{
return $this->package_length;
return $this->packageLength;
}
/**
@ -804,7 +804,7 @@
*/
public function getHeaderOffset(): int
{
return $this->header_offset;
return $this->headerOffset;
}
/**
@ -812,7 +812,7 @@
*/
public function getHeaderLength(): int
{
return $this->header_length;
return $this->headerLength;
}
/**
@ -820,7 +820,7 @@
*/
public function getDataOffset(): int
{
return $this->data_offset;
return $this->dataOffset;
}
/**
@ -828,7 +828,7 @@
*/
public function getDataLength(): int
{
return $this->data_length;
return $this->dataLength;
}
/**
@ -842,12 +842,12 @@
{
$checksum = hash($hash, '', $binary);
fseek($this->package_file, $this->package_offset);
$bytes_left = $this->package_length;
fseek($this->packageFile, $this->packageOffset);
$bytes_left = $this->packageLength;
while ($bytes_left > 0)
{
$buffer = fread($this->package_file, min(1024, $bytes_left));
$buffer = fread($this->packageFile, min(1024, $bytes_left));
$buffer_length = strlen($buffer);
$bytes_left -= $buffer_length;
$checksum = hash($hash, ($checksum . $buffer), $binary);
@ -875,13 +875,13 @@
throw new IOException(sprintf('Failed to open file \'%s\'', $path));
}
fseek($this->package_file, $this->package_offset);
$remaining_bytes = $this->package_length;
fseek($this->packageFile, $this->packageOffset);
$remaining_bytes = $this->packageLength;
while($remaining_bytes > 0)
{
$bytes_to_read = min($remaining_bytes, 4096);
$data = fread($this->package_file, $bytes_to_read);
$data = fread($this->packageFile, $bytes_to_read);
if ($data === false)
{
@ -911,9 +911,9 @@
*/
public function __destruct()
{
if(is_resource($this->package_file))
if(is_resource($this->packageFile))
{
fclose($this->package_file);
fclose($this->packageFile);
}
}
}

View file

@ -53,70 +53,70 @@
/**
* @var resource
*/
private $temp_file;
private $tempFile;
/**
* @var resource
*/
private $package_file;
private $packageFile;
/**
* @var string;
*/
private $temporary_path;
private $temporaryPath;
/**
* @var bool
*/
private $data_written;
private $dataWritten;
/**
* PackageWriter constructor.
*
* @throws IOException
*/
public function __construct(string $file_path, bool $overwrite=true)
public function __construct(string $filePath, bool $overwrite=true)
{
if(!$overwrite && is_file($file_path))
if(!$overwrite && is_file($filePath))
{
throw new IOException(sprintf('File \'%s\' already exists', $file_path));
throw new IOException(sprintf('File \'%s\' already exists', $filePath));
}
if(is_file($file_path))
if(is_file($filePath))
{
unlink($file_path);
unlink($filePath);
}
if(is_file($file_path . '.tmp'))
if(is_file($filePath . '.tmp'))
{
unlink($file_path . '.tmp');
unlink($filePath . '.tmp');
}
// Create the parent directory if it doesn't exist
if(!is_dir(dirname($file_path)))
if(!is_dir(dirname($filePath)))
{
if (!mkdir($concurrentDirectory = dirname($file_path), 0777, true) && !is_dir($concurrentDirectory))
if (!mkdir($concurrentDirectory = dirname($filePath), 0777, true) && !is_dir($concurrentDirectory))
{
throw new IOException(sprintf('Directory "%s" was not created', $concurrentDirectory));
}
}
touch($file_path);
touch($file_path . '.tmp');
touch($filePath);
touch($filePath . '.tmp');
$this->data_written = false;
$this->temporary_path = $file_path . '.tmp';
$this->temp_file = @fopen($this->temporary_path, 'wb'); // Create a temporary data file
$this->package_file = @fopen($file_path, 'wb');
$this->dataWritten = false;
$this->temporaryPath = $filePath . '.tmp';
$this->tempFile = @fopen($this->temporaryPath, 'wb'); // Create a temporary data file
$this->packageFile = @fopen($filePath, 'wb');
$this->headers = [
PackageStructure::FILE_VERSION->value => PackageStructureVersions::_2_0->value,
PackageStructure::FLAGS->value => [],
PackageStructure::DIRECTORY->value => []
];
if($this->temp_file === false || $this->package_file === false)
if($this->tempFile === false || $this->packageFile === false)
{
throw new IOException(sprintf('Failed to open file \'%s\'', $file_path));
throw new IOException(sprintf('Failed to open file \'%s\'', $filePath));
}
}
@ -160,7 +160,7 @@
*/
public function setFlags(array $flags): void
{
if($this->data_written)
if($this->dataWritten)
{
throw new IOException('Cannot set flags after data has been written to the package');
}
@ -198,7 +198,7 @@
}
}
if($this->data_written)
if($this->dataWritten)
{
throw new IOException('Cannot add a flag after data has been written to the package');
}
@ -227,7 +227,7 @@
}
}
if($this->data_written)
if($this->dataWritten)
{
throw new IOException('Cannot remove a flag after data has been written to the package');
}
@ -269,10 +269,10 @@
}
}
$pointer = sprintf("%d:%d", ftell($this->temp_file), strlen($data));
$pointer = sprintf("%d:%d", ftell($this->tempFile), strlen($data));
$this->headers[PackageStructure::DIRECTORY->value][$name] = $pointer;
$this->data_written = true;
fwrite($this->temp_file, $data);
$this->dataWritten = true;
fwrite($this->tempFile, $data);
return explode(':', $pointer);
}
@ -395,7 +395,7 @@
public function merge(PackageReader $reader): void
{
$progress_bar = new ConsoleProgressBar(sprintf('Merging %s', $reader->getAssembly()->getPackage()), count($reader->getDirectory()));
$processed_resources = [];
$processedResources = [];
foreach($reader->getDirectory() as $name => $pointer)
{
@ -411,15 +411,15 @@
break;
default:
if(isset($processed_resources[$pointer]))
if(isset($processedResources[$pointer]))
{
Console::outDebug(sprintf('Merging %s as a pointer', $name));
$this->addPointer($name, (int)$processed_resources[$pointer][0], (int)$processed_resources[$pointer][1]);
$this->addPointer($name, (int)$processedResources[$pointer][0], (int)$processedResources[$pointer][1]);
break;
}
Console::outDebug(sprintf('Merging %s', $name));
$processed_resources[$pointer] = $this->add($name, $reader->get($name));
$processedResources[$pointer] = $this->add($name, $reader->get($name));
}
$progress_bar->increaseValue(1, true);
@ -437,34 +437,34 @@
*/
public function close(): void
{
if(!is_resource($this->package_file) || !is_resource($this->temp_file))
if(!is_resource($this->packageFile) || !is_resource($this->tempFile))
{
throw new IOException('Package is already closed');
}
// Close the temporary data file
fclose($this->temp_file);
fclose($this->tempFile);
// Write the magic bytes "ncc_pkg" to the package and the header
fwrite($this->package_file, 'ncc_pkg');
fwrite($this->package_file, ZiProto::encode($this->headers));
fwrite($this->package_file, "\x1F\x1F\x1F\x1F");
fwrite($this->packageFile, 'ncc_pkg');
fwrite($this->packageFile, ZiProto::encode($this->headers));
fwrite($this->packageFile, "\x1F\x1F\x1F\x1F");
// Copy the temporary data file to the package
$temp_file = fopen($this->temporary_path, 'rb');
stream_copy_to_stream($temp_file, $this->package_file);
$temp_file = fopen($this->temporaryPath, 'rb');
stream_copy_to_stream($temp_file, $this->packageFile);
// End the package by writing the end-of-package delimiter (0xFFAA55F0)
fwrite($this->package_file, "\xFF\xAA\x55\xF0");
fwrite($this->packageFile, "\xFF\xAA\x55\xF0");
// Close the file handles
fclose($this->package_file);
fclose($this->packageFile);
fclose($temp_file);
unlink($this->temporary_path);
unlink($this->temporaryPath);
$this->package_file = null;
$this->temp_file = null;
$this->packageFile = null;
$this->tempFile = null;
}
/**
@ -482,14 +482,14 @@
}
finally
{
if(is_resource($this->package_file))
if(is_resource($this->packageFile))
{
fclose($this->package_file);
fclose($this->packageFile);
}
if(is_resource($this->temp_file))
if(is_resource($this->tempFile))
{
fclose($this->temp_file);
fclose($this->tempFile);
}
}
}

View file

@ -56,22 +56,22 @@
/**
* @var array
*/
private static $imported_packages = [];
private static $importedPackages = [];
/**
* @var array
*/
private static $class_map = [];
private static $classMap = [];
/**
* @var PackageManager|null
*/
private static $package_manager;
private static $packageManager;
/**
* @var array
*/
private static $included_files = [];
private static $includedFiles = [];
/**
* Executes the main execution point of an imported package and returns the evaluated result
@ -94,23 +94,23 @@
throw new InvalidArgumentException(sprintf('Package %s is not imported', $package));
}
if(self::$imported_packages[$package] instanceof PackageReader)
if(self::$importedPackages[$package] instanceof PackageReader)
{
if(self::$imported_packages[$package]?->getMetadata()?->getMainExecutionPolicy() === null)
if(self::$importedPackages[$package]?->getMetadata()?->getMainExecutionPolicy() === null)
{
Console::out('The package does not have a main execution policy, skipping execution');
return 0;
}
return ExecutionUnitRunner::executeFromPackage(
self::$imported_packages[$package],
self::$imported_packages[$package]->getMetadata()->getMainExecutionPolicy()
self::$importedPackages[$package],
self::$importedPackages[$package]->getMetadata()->getMainExecutionPolicy()
);
}
if(is_string(self::$imported_packages[$package]))
if(is_string(self::$importedPackages[$package]))
{
$metadata_path = self::$imported_packages[$package] . DIRECTORY_SEPARATOR . FileDescriptor::METADATA->value;
$metadata_path = self::$importedPackages[$package] . DIRECTORY_SEPARATOR . FileDescriptor::METADATA->value;
if(!is_file($metadata_path))
{
@ -118,7 +118,7 @@
}
return ExecutionUnitRunner::executeFromSystem(
self::$imported_packages[$package],
self::$importedPackages[$package],
Metadata::fromArray(ZiProto::decode(IO::fread($metadata_path)))->getMainExecutionPolicy(),
$arguments
);
@ -193,12 +193,12 @@
}
$entry = self::getPackageManager()->getPackageLock()->getEntry($package);
self::$imported_packages[$package] = $entry->getPath($version);
self::$importedPackages[$package] = $entry->getPath($version);
foreach($entry->getClassMap($version) as $class => $component_name)
foreach($entry->getClassMap($version) as $class => $componentName)
{
$component_path = $entry->getPath($version) . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . $component_name;
self::$class_map[strtolower($class)] = $component_path;
$componentPath = $entry->getPath($version) . DIRECTORY_SEPARATOR . 'bin' . DIRECTORY_SEPARATOR . $componentName;
self::$classMap[strtolower($class)] = $componentPath;
}
if($entry->getMetadata($version)->getOption(BuildConfigurationOptions::REQUIRE_FILES->value) !== null)
@ -215,12 +215,12 @@
}
// Get the file contents and prepare it
$evaluated_code = IO::fread($required_file);
$evaluatedCode = IO::fread($required_file);
// Remove the PHP tags
$evaluated_code = preg_replace('/^<\?php|<\?PHP/', '', $evaluated_code, 1);
$evaluatedCode = preg_replace('/^<\?php|<\?PHP/', '', $evaluatedCode, 1);
// Replace __DIR__ with the actual directory that the file is in
$evaluated_code = str_replace('__DIR__', sprintf('"%s"', dirname($required_file)), $evaluated_code);
$evaluatedCode = str_replace('__DIR__', sprintf('"%s"', dirname($required_file)), $evaluatedCode);
set_error_handler(function ($error_number, $message, $file, $line) use ($item, $package)
{
@ -228,9 +228,9 @@
});
// Evaluate the code
eval($evaluated_code);
eval($evaluatedCode);
restore_error_handler();
unset($evaluated_code);
unset($evaluatedCode);
}
catch (ConfigurationException $e)
{
@ -247,32 +247,32 @@
}
}
$safe_package_name = strtoupper($entry->getAssembly($version)->getName());
$safePackageName = strtoupper($entry->getAssembly($version)->getName());
foreach($entry->getMetadata($version)->getConstants() as $constant => $value)
{
$constant_full_name = sprintf("%s_%s", $safe_package_name, $constant);
$constantFullName = sprintf("%s_%s", $safePackageName, $constant);
// Skip if already defined.
if(defined($constant_full_name))
if(defined($constantFullName))
{
if(RuntimeCache::get(sprintf("defined_%s", $constant_full_name)))
if(RuntimeCache::get(sprintf("defined_%s", $constantFullName)))
{
continue;
}
trigger_error(sprintf('Cannot define constant %s from package %s because the constant is already defined', $constant_full_name, $package), E_USER_WARNING);
trigger_error(sprintf('Cannot define constant %s from package %s because the constant is already defined', $constantFullName, $package), E_USER_WARNING);
continue;
}
if(!Validate::constantName($constant_full_name))
if(!Validate::constantName($constantFullName))
{
// trigger warning only
trigger_error(sprintf('Cannot define constant %s from package %s because the constant name is invalid', $constant_full_name, $package), E_USER_WARNING);
trigger_error(sprintf('Cannot define constant %s from package %s because the constant name is invalid', $constantFullName, $package), E_USER_WARNING);
continue;
}
RuntimeCache::set(sprintf("defined_%s", $constant_full_name), true);
define($constant_full_name, $value);
RuntimeCache::set(sprintf("defined_%s", $constantFullName), true);
define($constantFullName, $value);
}
if(isset($entry->getMetadata($version)->getOptions()[PackageFlags::STATIC_DEPENDENCIES->value]))
@ -280,7 +280,7 @@
// Fake import the dependencies
foreach($entry->getVersion($version)->getDependencies() as $dependency)
{
self::$imported_packages[$dependency->getName()] = $entry->getPath($version);
self::$importedPackages[$dependency->getName()] = $entry->getPath($version);
}
}
else
@ -299,83 +299,83 @@
/**
* Imports a package from a package file
*
* @param string $package_path
* @param string $packagePath
* @return string
* @throws ConfigurationException
* @throws ImportException
* @throws IntegrityException
* @throws OperationException
*/
private static function importFromPackage(string $package_path): string
private static function importFromPackage(string $packagePath): string
{
try
{
$package_reader = new PackageReader($package_path);
$packageReader = new PackageReader($packagePath);
}
catch(Exception $e)
{
throw new RuntimeException(sprintf('Failed to import package from file "%s" due to an exception: %s', $package_path, $e->getMessage()), 0, $e);
throw new RuntimeException(sprintf('Failed to import package from file "%s" due to an exception: %s', $packagePath, $e->getMessage()), 0, $e);
}
// Check if the package is already imported
if(in_array($package_reader->getAssembly()->getPackage(), self::$imported_packages, true))
if(in_array($packageReader->getAssembly()->getPackage(), self::$importedPackages, true))
{
$package_name = $package_reader->getAssembly()->getPackage();
unset($package_reader);
return $package_name;
$packageName = $packageReader->getAssembly()->getPackage();
unset($packageReader);
return $packageName;
}
// Import the package
$package_name = $package_reader->getAssembly()->getPackage();
self::$imported_packages[$package_name] = $package_reader;
$packageName = $packageReader->getAssembly()->getPackage();
self::$importedPackages[$packageName] = $packageReader;
// Register the autoloader
foreach($package_reader->getClassMap() as $value)
foreach($packageReader->getClassMap() as $value)
{
self::$class_map[strtolower($value)] = static function() use ($value, $package_name)
self::$classMap[strtolower($value)] = static function() use ($value, $packageName)
{
return self::$imported_packages[$package_name]->getComponentByClass($value)->getData();
return self::$importedPackages[$packageName]->getComponentByClass($value)->getData();
};
}
// Import the required files
if($package_reader->getMetadata()->getOption(BuildConfigurationOptions::REQUIRE_FILES->value) !== null)
if($packageReader->getMetadata()->getOption(BuildConfigurationOptions::REQUIRE_FILES->value) !== null)
{
foreach($package_reader->getMetadata()->getOption(BuildConfigurationOptions::REQUIRE_FILES->value) as $item)
foreach($packageReader->getMetadata()->getOption(BuildConfigurationOptions::REQUIRE_FILES->value) as $item)
{
try
{
eval($package_reader->getComponent($item)->getData());
eval($packageReader->getComponent($item)->getData());
}
catch(ConfigurationException $e)
{
throw new ImportException(sprintf('Failed to import "%s" from %s: %s', $item, $package_name, $e->getMessage()), $e);
throw new ImportException(sprintf('Failed to import "%s" from %s: %s', $item, $packageName, $e->getMessage()), $e);
}
}
}
if($package_reader->getFlag(PackageFlags::STATIC_DEPENDENCIES->value))
if($packageReader->getFlag(PackageFlags::STATIC_DEPENDENCIES->value))
{
// Fake import the dependencies
foreach($package_reader->getDependencies() as $dependency_name)
foreach($packageReader->getDependencies() as $dependency_name)
{
$dependency = $package_reader->getDependency($dependency_name);
self::$imported_packages[$dependency->getName()] = $package_reader;
$dependency = $packageReader->getDependency($dependency_name);
self::$importedPackages[$dependency->getName()] = $packageReader;
}
}
else
{
// Import dependencies recursively
foreach($package_reader->getDependencies() as $dependency)
foreach($packageReader->getDependencies() as $dependency)
{
$dependency = $package_reader->getDependency($dependency);
$dependency = $packageReader->getDependency($dependency);
/** @noinspection UnusedFunctionResultInspection */
self::import($dependency->getName(), $dependency->getVersion());
}
}
return $package_reader->getAssembly()->getPackage();
return $packageReader->getAssembly()->getPackage();
}
/**
@ -386,7 +386,7 @@
*/
public static function isImported(string $package): bool
{
return isset(self::$imported_packages[$package]);
return isset(self::$importedPackages[$package]);
}
/**
@ -396,7 +396,7 @@
*/
public static function getImportedPackages(): array
{
return array_keys(self::$imported_packages);
return array_keys(self::$importedPackages);
}
/**
@ -407,20 +407,20 @@
{
$class = strtolower($class);
if(!isset(self::$class_map[$class]))
if(!isset(self::$classMap[$class]))
{
return;
}
if(is_callable(self::$class_map[$class]))
if(is_callable(self::$classMap[$class]))
{
eval(self::$class_map[$class]());
eval(self::$classMap[$class]());
return;
}
if(is_string(self::$class_map[$class]) && is_file(self::$class_map[$class]))
if(is_string(self::$classMap[$class]) && is_file(self::$classMap[$class]))
{
require_once self::$class_map[$class];
require_once self::$classMap[$class];
}
}
@ -429,12 +429,12 @@
*/
private static function getPackageManager(): PackageManager
{
if(self::$package_manager === null)
if(self::$packageManager === null)
{
self::$package_manager = new PackageManager();
self::$packageManager = new PackageManager();
}
return self::$package_manager;
return self::$packageManager;
}
/**
@ -444,7 +444,7 @@
*/
public static function runtimeGetIncludedFiles(): array
{
return array_merge(get_included_files(), self::$included_files);
return array_merge(get_included_files(), self::$includedFiles);
}
/**
@ -493,20 +493,20 @@
{
print(ob_get_clean());
$exception_stack = null;
$exceptionStack = null;
foreach ($exceptions as $e)
{
if($exception_stack === null)
if($exceptionStack === null)
{
$exception_stack = $e;
$exceptionStack = $e;
}
else
{
$exception_stack = new Exception($exception_stack->getMessage(), $exception_stack->getCode(), $e);
$exceptionStack = new Exception($exceptionStack->getMessage(), $exceptionStack->getCode(), $e);
}
}
throw new RuntimeException('An exception occurred while evaluating the code', 0, $exception_stack);
throw new RuntimeException('An exception occurred while evaluating the code', 0, $exceptionStack);
}
print(ob_get_clean());
@ -526,7 +526,7 @@
*/
private static function acquireFile(string $path, ?string $package=null): string
{
$cwd_checked = false; // sanity check to prevent checking the cwd twice
$CwdChecked = false; // sanity check to prevent checking the cwd twice
// Check if the file is absolute
if(is_file($path))
@ -536,29 +536,29 @@
}
// Since $package is not null, let's try to acquire the file from the package
if($package !== null && isset(self::$imported_packages[$package]))
if($package !== null && isset(self::$importedPackages[$package]))
{
$base_path = basename($path);
$basePath = basename($path);
if(self::$imported_packages[$package] instanceof PackageReader)
if(self::$importedPackages[$package] instanceof PackageReader)
{
$acquired_file = self::$imported_packages[$package]->find($base_path);
$acquiredFile = self::$importedPackages[$package]->find($basePath);
Console::outDebug(sprintf('Acquired file "%s" from package "%s"', $path, $package));
return match (Resolver::componentType($acquired_file))
return match (Resolver::componentType($acquiredFile))
{
PackageDirectory::RESOURCES->value => self::$imported_packages[$package]->getResource(Resolver::componentName($acquired_file))->getData(),
PackageDirectory::COMPONENTS->value => self::$imported_packages[$package]->getComponent(Resolver::componentName($acquired_file))->getData([ComponentDecodeOptions::AS_FILE->value]),
PackageDirectory::RESOURCES->value => self::$importedPackages[$package]->getResource(Resolver::componentName($acquiredFile))->getData(),
PackageDirectory::COMPONENTS->value => self::$importedPackages[$package]->getComponent(Resolver::componentName($acquiredFile))->getData([ComponentDecodeOptions::AS_FILE->value]),
default => throw new IOException(sprintf('Unable to acquire file "%s" from package "%s" because it is not a resource or component', $path, $package)),
};
}
if(is_dir(self::$imported_packages[$package]))
if(is_dir(self::$importedPackages[$package]))
{
$base_path = basename($path);
foreach(IO::scan(self::$imported_packages[$package]) as $file)
$basePath = basename($path);
foreach(IO::scan(self::$importedPackages[$package]) as $file)
{
if(str_ends_with($file, $base_path))
if(str_ends_with($file, $basePath))
{
Console::outDebug(sprintf('Acquired file "%s" from package "%s"', $path, $package));
return IO::fread($file);
@ -568,29 +568,29 @@
}
// If not, let's try the include_path
foreach(explode(PATH_SEPARATOR, get_include_path()) as $file_path)
foreach(explode(PATH_SEPARATOR, get_include_path()) as $filePath)
{
if($file_path === '.' && !$cwd_checked)
if($filePath === '.' && !$CwdChecked)
{
$cwd_checked = true;
$file_path = getcwd();
$CwdChecked = true;
$filePath = getcwd();
}
if(is_file($file_path . DIRECTORY_SEPARATOR . $path))
if(is_file($filePath . DIRECTORY_SEPARATOR . $path))
{
Console::outDebug(sprintf('Acquired file "%s" from include_path', $path));
return IO::fread($file_path . DIRECTORY_SEPARATOR . $path);
return IO::fread($filePath . DIRECTORY_SEPARATOR . $path);
}
if(is_file($file_path . DIRECTORY_SEPARATOR . basename($path)))
if(is_file($filePath . DIRECTORY_SEPARATOR . basename($path)))
{
Console::outDebug(sprintf('Acquired file "%s" from include_path (using basename)', $path));
return IO::fread($file_path . DIRECTORY_SEPARATOR . basename($path));
return IO::fread($filePath . DIRECTORY_SEPARATOR . basename($path));
}
}
// Check the current working directory
if(!$cwd_checked)
if(!$CwdChecked)
{
if(is_file(getcwd() . DIRECTORY_SEPARATOR . $path))
{
@ -607,11 +607,11 @@
// Check the calling script's directory
$called_script_directory = dirname(debug_backtrace()[0]['file']);
$file_path = $called_script_directory . DIRECTORY_SEPARATOR . $path;
if(is_file($file_path))
$filePath = $called_script_directory . DIRECTORY_SEPARATOR . $path;
if(is_file($filePath))
{
Console::outDebug(sprintf('Acquired file "%s" from calling script\'s directory', $path));
return IO::fread($file_path);
return IO::fread($filePath);
}
throw new IOException(sprintf('Unable to acquire file "%s" because it does not exist', $path));
@ -628,7 +628,7 @@
{
try
{
$acquired_file = self::acquireFile($path, $package);
$acquiredFile = self::acquireFile($path, $package);
}
catch(Exception $e)
{
@ -639,19 +639,19 @@
return;
}
$acquired_name = $path;
$acquiredName = $path;
if(!is_file($path))
{
$acquired_name = hash('crc32', $acquired_file);
$acquiredName = hash('crc32', $acquiredFile);
}
if(!in_array($acquired_name, self::$included_files, true))
if(!in_array($acquiredName, self::$includedFiles, true))
{
self::$included_files[] = sprintf('virtual(%s)', $acquired_name);
self::$includedFiles[] = sprintf('virtual(%s)', $acquiredName);
}
self::extendedEvaluate($acquired_file);
self::extendedEvaluate($acquiredFile);
}
/**
@ -682,7 +682,7 @@
{
try
{
$acquired_file = self::acquireFile($path, $package);
$acquiredFile = self::acquireFile($path, $package);
}
catch(Exception $e)
{
@ -691,19 +691,19 @@
throw new RuntimeException(sprintf('Failed to acquire file "%s" at runtime: %s', $path, $e->getMessage()), $e->getCode(), $e);
}
$acquired_name = $path;
$requiredName = $path;
if(!is_file($path))
{
$acquired_name = hash('crc32', $acquired_file);
$requiredName = hash('crc32', $acquiredFile);
}
if(!in_array($acquired_name, self::$included_files, true))
if(!in_array($requiredName, self::$includedFiles, true))
{
self::$included_files[] = sprintf('virtual(%s)', $acquired_name);
self::$includedFiles[] = sprintf('virtual(%s)', $requiredName);
}
self::extendedEvaluate($acquired_file);
self::extendedEvaluate($acquiredFile);
}
/**

View file

@ -29,7 +29,6 @@
use ncc\ThirdParty\Symfony\Filesystem\Filesystem;
use ncc\Utilities\Console;
use ncc\Utilities\Functions;
use ncc\Utilities\RuntimeCache;
class ShutdownHandler
{
@ -41,7 +40,7 @@
/**
* @var array
*/
private static $cleanup_paths = [];
private static $cleanupPaths = [];
/**
* Registers the shutdown handler
@ -66,11 +65,11 @@
*/
public static function shutdown(): void
{
if(count(self::$cleanup_paths) > 0)
if(count(self::$cleanupPaths) > 0)
{
$filesystem = new Filesystem();
foreach(self::$cleanup_paths as $path)
foreach(self::$cleanupPaths as $path)
{
try
{
@ -101,9 +100,9 @@
*/
public static function declareTemporaryPath(string $path): void
{
if(!in_array($path, self::$cleanup_paths, true))
if(!in_array($path, self::$cleanupPaths, true))
{
self::$cleanup_paths[] = $path;
self::$cleanupPaths[] = $path;
}
}
}