Skip to content

Commit

Permalink
Merge pull request #6 from RoyVoetman/2.x
Browse files Browse the repository at this point in the history
2.x
  • Loading branch information
RoyVoetman authored Nov 30, 2020
2 parents 8045311 + 2e8ed3f commit 1edaab6
Show file tree
Hide file tree
Showing 10 changed files with 601 additions and 702 deletions.
68 changes: 15 additions & 53 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,81 +8,43 @@ A Gitlab Storage filesystem for [Flysystem](https://flysystem.thephpleague.com/d

This package contains a Flysystem adapter for Gitlab. Under the hood, Gitlab's [Repository (files) API](https://docs.gitlab.com/ee/api/repository_files.html) v4 is used.

> Flysystem version 2 support still in development
## Installation

```bash
composer require royvoetman/flysystem-gitlab-storage
```

## Integrations

* Laravel - https://github.com/royvoetman/laravel-gitlab-storage

## Usage
```php
// Create a Gitlab Client to talk with the API
$client = new Client('personal-access-token', 'project-id', 'branch', 'base-url');
$client = new Client('project-id', 'branch', 'base-url', 'personal-access-token');

// Create the Adapter that implentents Flysystems AdapterInterface
$adapter = new GitlabAdapter($client));

// Create FileSystem
$filesystem = new Filesystem($adapter);

// write a file
$filesystem->write('path/to/file.txt', 'contents');

// update a file
$filesystem->update('path/to/file.txt', 'new contents');
// Create the Adapter that implements Flysystems AdapterInterface
$adapter = new GitlabAdapter(
// Gitlab API Client
$client,
// Optional path prefix
'path/prefix',
);

// read a file
$contents = $filesystem->read('path/to/file.txt');

// check if a file exists
$exists = $filesystem->has('path/to/file.txt');

// delete a file
$filesystem->delete('path/to/file.txt');

// rename a file
$filesystem->rename('filename.txt', 'newname.txt');

// copy a file
$filesystem->copy('filename.txt', 'duplicate.txt');

// delete a directory
$filesystem->deleteDir('path/to/directory');
// The FilesystemOperator
$filesystem = new League\Flysystem\Filesystem($adapter);

// see http://flysystem.thephpleague.com/api/ for full list of available functionality
```

### Access token (required for private projects)
Gitlab supports server side API authentication with Personal Access tokens

For more information on how to create your own Personal Access token: [Gitlab Docs](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html)

### Project ID
Every project in Gitlab has its own Project ID. It can be found at to top of the frontpage of your repository. [See](https://stackoverflow.com/questions/39559689/where-do-i-find-the-project-id-for-the-gitlab-api#answer-53126068)

### Base URL
This will be the URL where you host your gitlab server (e.g. https://gitlab.com)

## Debugging
In some cases, the need arises to debug the Http Messeages returned by the Gitlab API. For this need, the adapter has a built-in `debug mode`. When this mode is enabled, it will tell the adapter to prevent catching the `GuzzleHttp\Exception\GuzzleException`.

To check for the current state of de adaptor the `isDebugEnabled` method can be called on the adapter.

Enabling debug mode can be done with either of the following two options:

### At instantiation
```php
$adapter = new GitlabAdapter($client, '', true); // Third parameter defines debug mode
```
### Access token (required for private projects)
Gitlab supports server side API authentication with Personal Access tokens

### At runtime
```php
$adapter->setDebug(true);
```
For more information on how to create your own Personal Access token: [Gitlab Docs](https://docs.gitlab.com/ee/user/profile/personal_access_tokens.html)

## Changelog

Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@
}
],
"require": {
"php": "^7.1",
"php": "^7.4",
"ext-json": "*",
"guzzlehttp/guzzle": "^6.3",
"league/flysystem": "^1.0"
"league/flysystem": "2.0.0-RC1"
},
"require-dev": {
"phpunit/phpunit": "^9.2"
Expand Down
35 changes: 24 additions & 11 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use GuzzleHttp\Client as HttpClient;
use GuzzleHttp\Psr7\Response;
use InvalidArgumentException;

/**
* Class GitlabAdapter
Expand All @@ -15,7 +16,7 @@ class Client
const VERSION_URI = "/api/v4";

/**
* @var string
* @var ?string
*/
protected $personalAccessToken;

Expand All @@ -37,17 +38,17 @@ class Client
/**
* Client constructor.
*
* @param string $personalAccessToken
* @param string $projectId
* @param string $branch
* @param string $baseUrl
* @param string|null $personalAccessToken
*/
public function __construct(string $personalAccessToken, string $projectId, string $branch, string $baseUrl)
public function __construct(string $projectId, string $branch, string $baseUrl, ?string $personalAccessToken = null)
{
$this->personalAccessToken = $personalAccessToken;
$this->projectId = $projectId;
$this->branch = $branch;
$this->baseUrl = $baseUrl;
$this->personalAccessToken = $personalAccessToken;
}

/**
Expand Down Expand Up @@ -80,6 +81,20 @@ public function read($path)
return $this->responseContents($response);
}

/**
* @param $path
*
* @return mixed|string
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function blame($path)
{
$path = urlencode($path);

$response = $this->request('GET', "files/$path/blame");

return $this->responseContents($response);
}

/**
* @param string $path
Expand Down Expand Up @@ -116,7 +131,7 @@ public function upload(string $path, string $contents, string $commitMessage, $o
public function uploadStream(string $path, $resource, string $commitMessage, $override = false): array
{
if (!is_resource($resource)) {
throw new \InvalidArgumentException(sprintf('Argument must be a valid resource type. %s given.',
throw new InvalidArgumentException(sprintf('Argument must be a valid resource type. %s given.',
gettype($resource)));
}

Expand All @@ -139,13 +154,13 @@ public function delete(string $path, string $commitMessage)
}

/**
* @param string $directory
* @param string|null $directory
* @param bool $recursive
*
* @return mixed
* @return iterable
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function tree(string $directory = null, bool $recursive = false): array
public function tree(string $directory = null, bool $recursive = false): iterable
{
if ($directory === '/' || $directory === '') {
$directory = null;
Expand All @@ -161,10 +176,8 @@ public function tree(string $directory = null, bool $recursive = false): array
'page' => $page++
]);

$tree = array_merge($this->responseContents($response), $tree ?? []);
yield $this->responseContents($response);
} while ($this->responseHasNextPage($response));

return $tree;
}

/**
Expand Down
Loading

0 comments on commit 1edaab6

Please sign in to comment.