PHP code example of private-packagist / api-client
1. Go to this page and download the library: Download private-packagist/api-client library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
private-packagist / api-client example snippets
// This file is generated by Composer
lient();
$client->authenticate('api-key', 'api-secret');
$packages = $client->packages()->all();
$jobs = $client->organization()->sync();
use PrivatePackagist\ApiClient\TeamPermissions;
$permissions = new TeamPermissions;
// Grant all permissions.
$permissions->canEditTeamPackages = true;
$permissions->canAddPackages = true;
$permissions->canCreateSubrepositories = true;
$permissions->canManageVendorCustomers = true;
$permissions->canManageVendorCustomers = true;
use PrivatePackagist\ApiClient\TeamPermissions;
$permissions = TeamPermissions::fromFlags(
TeamPermissions::PERMISSION_CAN_EDIT_TEAM_PACKAGES | TeamPermissions::PERMISSION_CAN_ADD_PACKAGES,
);
use PrivatePackagist\ApiClient\TeamPermissions;
$team = $client->teams()->all()[0];
$permissions = TeamPermissions::fromTeamResponse($team);
$teams = $client->teams()->all();
use PrivatePackagist\ApiClient\TeamPermissions;
$permissions = new TeamPermissions;
$team = $client->teams()->create('New Team Name', $permissions);
$team = $client->teams()->show($teamId);
use PrivatePackagist\ApiClient\TeamPermissions;
$permissions = new TeamPermissions;
$team = $client->teams()->edit($teamId, 'Altered Team Name', $permissions);
// Create a new token with access to all packages
$token = $client->tokens()->create([
'description' => 'New Team Token',
'access' => 'read',
'accessToAllPackages' => true,
]);
// Create a new token with access to packages a team has access to
$token = $client->tokens()->create([
'description' => 'New Team Token',
'access' => 'read',
'teamId' => 1, // Get teamId from the list of teams to determine to which packages the token has access to
]);
$customerId = 42;
$packages = [
[
'name' => 'acme-website/package',
'versionConstraint' => '^1.0 | ^2.0', // optional version constraint to limit updates the customer receives
'expirationDate' => (new \DateTime())->add(new \DateInterval('P1Y'))->format('c'), // optional expiration date to limit updates the customer receives
'minimumAccessibleStability' => 'beta', // optional stability to restrict customers to specific package version stabilities like alpha, beta, or RC
],
];
$packages = $client->customers()->addOrEditPackages($customerId, $packages);
$vendorBundleId = 42;
$vendorBundleData = [
'name' => 'Bundle name',
'minimumAccessibleStability' => 'dev',
'versionConstraint' => '^1.0',
'assignAllPackages' => true,
'synchronizationIds' => [123], // A list of synchronization ids for which new packages should automatically be added to the bundle.
];
$vendorBundle = $client->vendorBundles()->edit($vendorBundleId, $vendorBundleData);
$filters = [
'origin' => \PrivatePackagist\ApiClient\Api\Packages::ORIGIN_PRIVATE, // optional filter to only receive packages that can be added to customers,
'security-issue-state' => \PrivatePackagist\ApiClient\Api\SecurityIssues::STATE_OPEN, // optional filter to filter packages with open security issues,
];
$packages = $client->packages()->all($filters);
// Either use package name:
$package = $client->packages()->show('acme-website/package');
// Or the package ID:
$package = $client->packages()->show(123);
$config = [
"monitorAllBranches" => false, // If set to true then monitoredBranches will be ignored and can be omitted
"monitoredBranches" => [
"dev-main"
],
];
$client->packages()->editSecurityMonitoringConfig('acme-website/package', $config);
$fileName = 'package1.zip'; // your package archive artifact containing a valid composer.json in root directory
$file = file_get_contents($fileName);
$client->packages()->artifacts()->create($file, 'application/zip', $fileName);
// in case you want to replace the artifact file with a newly uploaded one
// 1. get current artifact ids
$result = $client->packages()->artifacts()->showPackageArtifacts('acme-website/package');
$artifactIds = array_column($result, 'id'); // [41, 42]
// 2. upload the new artifact file
$fileName = 'package1.zip';
$file = file_get_contents($fileName);
$response = $client->packages()->artifacts()->create($file, 'application/zip', $fileName);
$newArtifactId = $response['id'];
// 3. let's say we don't want to have the artifact file id = 41 and use the newly uploaded file instead
$artifactIds = array_shift($artifactIds);
$artifactIds[] = $newArtifactId;
$client->packages()->editArtifactPackage('acme-website/package', $artifactIds);