PHP code example of igzard / code-storage-php

1. Go to this page and download the library: Download igzard/code-storage-php 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/ */

    

igzard / code-storage-php example snippets


$client = new Client(name: 'your-name', key: $pem, httpClient: $guzzle);

use Igzard\CodeStorage\Client;

$client = new Client(
    name: 'your-name',
    key: "-----BEGIN PRIVATE KEY-----\n...\n-----END PRIVATE KEY-----",
);

$repo = $client->createRepo();

echo $repo->remoteUrl(), PHP_EOL;
echo $repo->importRemoteUrl(), PHP_EOL;

use Igzard\CodeStorage\Model\FileRequestHeaders;

$meta = $repo->headFile(
    path: 'README.md',
    ref: 'main',
    headers: new FileRequestHeaders(range: 'bytes=0-1023', ifNoneMatch: '"b10b5ha"'),
);

echo $meta->statusCode, $meta->etag, $meta->contentRange;

$response = $repo->archiveStream(
    ref: 'main',
     * 1024,
    archivePrefix: 'repo/',
);

$body = $response->getBody();
while (! $body->eof()) {
    fwrite($out, $body->read(65536));
}

$result = $repo->listFilesWithMetadata(ref: 'feature/demo', ephemeral: true);

echo $result->ref;
echo $result->files[0]->lastCommitSha;
echo $result->commits[$result->files[0]->lastCommitSha]->author;

$blame = $repo->getBlame(
    path: 'src/main.php',
    ref: 'main',
    ranges: ['10,30'],
    detectMoves: true,
);

foreach ($blame->lines as $line) {
    printf("%d (%s): %s — %s\n", $line->lineNumber, substr($line->commitSha, 0, 7), $line->authorName, $line->summary);
}

$tags = $repo->listTags(limit: 10);
$repo->createTag(name: 'v1.0.0', target: '0123456789abcdef0123456789abcdef01234567');
$repo->deleteTag(name: 'v1.0.0');

$repo->deleteBranch(name: 'feature/old-onboarding');

// Set ephemeral to delete a branch from the ephemeral namespace
$deleted = $repo->deleteBranch(name: 'merge/123e4567-e89b-12d3-a456-426614174000', ephemeral: true);
echo $deleted->ephemeral;

$repo->createNote(sha: $sha, note: 'LGTM', ref: 'reviews');

$note = $repo->getNote(sha: $sha, ref: 'reviews');
echo $note->note;

// Discover custom notes namespaces with cursor pagination.
$refs = $repo->listNotesRefs(prefix: 'reviews/', limit: 20);
foreach ($refs->refs as $entry) {
    echo $entry->ref, ' ', $entry->sha, PHP_EOL;
}
if ($refs->hasMore) {
    $repo->listNotesRefs(prefix: 'reviews/', cursor: $refs->nextCursor);
}

use Igzard\CodeStorage\Enum\MergeStrategy;
use Igzard\CodeStorage\Model\CommitSignature;

$preview = $repo->previewMerge(sourceBranch: 'feature', targetBranch: 'main', includeContent: true);
echo $preview->status?->value, $preview->result?->value, implode(',', $preview->conflictPaths);

$result = $repo->merge(
    sourceBranch: 'feature',
    sourceIsEphemeral: true,
    targetBranch: 'main',
    // Leave expectedTargetSha empty to merge into the current target tip.
    // Set it to 

$result = $repo
    ->createCommit(
        targetBranch: 'main',
        commitMessage: 'Update docs',
        author: new CommitSignature('Docs Bot', '[email protected]'),
    )
    ->addFileFromString('docs/readme.md', "# Updated\n")
    ->send();

echo $result->commitSha;

foreach ($repo->listCommits(branch: 'main', limit: 20)->commits as $commit) {
    echo $commit->sha, ' ', implode(' ', $commit->parentShas), PHP_EOL;
}

$diff = $repo->getCommitDiff(
    sha: 'head-commit-sha',
    baseSha: 'base-commit-sha',
    gitApplyCompatible: true,
);

$repo = $client->repo(
    id: 'repo-id',
    defaultBranch: 'main',
    createdAt: '2024-06-15T12:00:00Z',
);

echo $repo->remoteUrl();

use Igzard\CodeStorage\Enum\GitHubAuthType;
use Igzard\CodeStorage\Model\GitHubBaseRepo;

$repo = $client->createRepo(baseRepo: new GitHubBaseRepo(
    owner: 'octocat',
    name: 'hello-world',
    authType: GitHubAuthType::Public,
));

echo $repo->id;

use Igzard\CodeStorage\Model\WebhookPushEvent;
use Igzard\CodeStorage\Webhook;

$validation = Webhook::validate($rawBody, getallheaders(), $secret);

if (! $validation->valid) {
    http_response_code(400);
    exit($validation->error);
}

if ($validation->payload instanceof WebhookPushEvent) {
    echo $validation->payload->ref, $validation->payload->after;
}