Skip to content
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

feat: adds get_packages() #22

Merged
merged 1 commit into from
Mar 3, 2024
Merged
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
27 changes: 27 additions & 0 deletions src/inc/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -349,3 +349,30 @@ function cleanSensitiveEnv(array $sensitives): void
putenv($var . '=');
}
}

/**
* Retrieves all packages listed in the 'require' section of the composer.json file.
*
* @param string $app_path The path to the application root directory.
* @return array An array of required packages, or an empty array if the file doesn't exist or on error.
*/
function get_packages( string $app_path ): array
{
$composer_path = $app_path . DIRECTORY_SEPARATOR . 'composer.json';

// Check if the composer.json file exists.
if ( ! is_file( $composer_path ) ) {
return [];
}

// Attempt to decode the JSON content from composer.json.
$composer_json = json_decode( file_get_contents( $composer_path ), true );

// Check for JSON errors.
if ( json_last_error() !== JSON_ERROR_NONE ) {
error_log( 'json error');
return [];
}

return $composer_json['require'] ?? [];
}
Loading