Skip to content

Commit 549505d

Browse files
committed
Initial commit
0 parents  commit 549505d

14 files changed

+483
-0
lines changed

.gitattributes

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
* text=auto
2+
3+
/.github export-ignore
4+
/tests export-ignore
5+
.gitattributes export-ignore
6+
.gitignore export-ignore
7+
.styleci.yml export-ignore
8+
phpunit.xml export-ignore

.github/workflows/coverage.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
name: coverage
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
tests:
7+
runs-on: ubuntu-latest
8+
9+
name: Coverage
10+
11+
steps:
12+
- name: Checkout code
13+
uses: actions/checkout@v1
14+
15+
- name: Cache dependencies
16+
uses: actions/cache@v1
17+
with:
18+
path: ~/.composer/cache/files
19+
key: dependencies-php-7.4-illuminate-7.*-composer-${{ hashFiles('composer.json') }}
20+
21+
- name: Setup PHP
22+
uses: shivammathur/setup-php@v2
23+
with:
24+
php-version: 7.4
25+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd
26+
coverage: xdebug
27+
28+
- name: Install dependencies
29+
run: composer update --prefer-dist --no-interaction --no-suggest
30+
31+
- name: Calculate coverage statistics
32+
run: vendor/bin/phpunit --coverage-clover 'clover.xml'
33+
34+
- name: Send coverage statistics
35+
uses: codecov/codecov-action@v1

.github/workflows/tests.yml

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
jobs:
10+
tests:
11+
runs-on: ubuntu-latest
12+
13+
strategy:
14+
fail-fast: true
15+
matrix:
16+
php: [7.2, 7.3, 7.4]
17+
illuminate: [6.*, 7.*]
18+
stability: [prefer-lowest, prefer-stable]
19+
include:
20+
- illuminate: 6.*
21+
testbench: 4.*
22+
- illuminate: 7.*
23+
testbench: 5.*
24+
exclude:
25+
- php: 7.4
26+
illuminate: 6.*
27+
28+
name: P${{ matrix.php }} - I${{ matrix.illuminate }} - ${{ matrix.stability }}
29+
30+
steps:
31+
- name: Checkout code
32+
uses: actions/checkout@v1
33+
34+
- name: Cache dependencies
35+
uses: actions/cache@v1
36+
with:
37+
path: ~/.composer/cache/files
38+
key: dependencies-php-${{ matrix.php }}-illuminate-${{ matrix.illuminate }}-composer-${{ hashFiles('composer.json') }}
39+
40+
- name: Setup PHP
41+
uses: shivammathur/setup-php@v2
42+
with:
43+
php-version: ${{ matrix.php }}
44+
extensions: dom, curl, libxml, mbstring, zip, pcntl, pdo, sqlite, pdo_sqlite, gd
45+
coverage: none
46+
47+
- name: Install dependencies
48+
run: |
49+
composer require "illuminate/contracts:${{ matrix.illuminate }}" --no-interaction --no-update
50+
composer require "illuminate/support:${{ matrix.illuminate }}" --no-interaction --no-update
51+
composer require "orchestra/testbench:${{ matrix.testbench }}" --no-interaction --no-update
52+
composer update --${{ matrix.stability }} --prefer-dist --no-interaction --no-suggest
53+
54+
- name: Execute tests
55+
run: vendor/bin/phpunit --verbose

.gitignore

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/.idea
2+
vendor
3+
/composer.lock
4+
composer.phar
5+
clover.json
6+
clover.xml
7+
.phpunit.result.cache

.styleci.yml

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
preset: laravel
2+
disabled:
3+
- simplified_null_return

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Release Notes
2+
3+
Initial release

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 Mark Walet
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Laravel Packagist
2+
3+
A Laravel wrapper for the [spatie/packagist-api](https://github.com/spatie/packagist-api) package.
4+
5+
## Installation
6+
You can install this package with composer:
7+
8+
```shell
9+
composer require markwalet/laravel-packagist
10+
```
11+
12+
Laravel uses Package auto-discovery, so you don't have to register the service provider. If you want to register the service provider manually, add the following line to your `config/app.php` file:
13+
14+
```php
15+
MarkWalet\Packagist\PackagistServiceProvider::class,
16+
```
17+
18+
## Usage
19+
20+
There are two main ways how you can make Packagist calls:
21+
22+
**Using the application container**
23+
24+
```php
25+
/** @var \Spatie\Packagist\PackagistClient $client */
26+
$client = app(\Spatie\Packagist\PackagistClient::class);
27+
28+
$client->getPackage('markwalet', 'laravel-packagist');
29+
```
30+
31+
**Using the facade**
32+
```php
33+
Packagist::getPackage('markwalet', 'laravel-packagist');
34+
```
35+
### Available methods
36+
37+
**List package names**
38+
```php
39+
// All packages
40+
Packagist::getPackagesNames();
41+
42+
// Filter on type.
43+
Packagist::getPackagesNamesByType('composer-plugin');
44+
45+
// Filter on organization
46+
Packagist::getPackagesNamesByVendor('spatie');
47+
```
48+
49+
**Searching for packages**
50+
```php
51+
// Search packages by name.
52+
Packagist::searchPackagesByName('packagist');
53+
54+
// Search packages by tag.
55+
Packagist::searchPackagesByTags('psr-3');
56+
57+
// Search packages by type.
58+
Packagist::searchPackagesByType('composer-plugin');
59+
60+
// Combined search.
61+
Packagist::searchPackages('packagist', ['type' => 'library']);
62+
```
63+
64+
**Pagination**
65+
66+
Searching for packages returns a paginated result. You can change the pagination settings by adding more parameters.
67+
68+
```php
69+
// Get the third page, 10 items per page.
70+
Packagist::searchPackagesByName('packagist', 3, 10);
71+
```
72+
73+
**Getting package data.**
74+
```php
75+
// Using the Composer metadata. (faster, but less data)
76+
Packagist::getPackageMetadata('spatie/packagist-api');
77+
Packagist::getPackageMetadata('spatie', 'packagist-api');
78+
79+
// Using the API. (slower, cached for 12 hours by Packagist.
80+
Packagist::getPackage('spatie/packagist-api');
81+
Packagist::getPackage('spatie', 'packagist-api');
82+
```
83+
84+
**Get Statistics**
85+
```php
86+
$packagist->getStatistics();
87+
```
88+
89+
## Configuration
90+
By default, the api url for Packagist is set to `https://packagist.org`. If you want to override that, you can add the following code block to your `config/services.php` file:
91+
92+
```php
93+
'packagist' => [
94+
'base_url' => 'https://packagist.org',
95+
'repo_url' => 'https://repo.packagist.org',
96+
],
97+
```

composer.json

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "markwalet/laravel-packagist",
3+
"description": "A Laravel wrapper for the `spatie/packagist-api` package.",
4+
"type": "library",
5+
"license": "MIT",
6+
"authors": [
7+
{
8+
"name": "Mark Walet",
9+
"email": "[email protected]",
10+
"homepage": "https://markwalet.me",
11+
"role": "Owner"
12+
}
13+
],
14+
"require": {
15+
"php": "^7.2",
16+
"illuminate/contracts": "6.*|7.*",
17+
"illuminate/support": "6.*|7.*",
18+
"spatie/packagist-api": "^2.0",
19+
"ext-json": "*"
20+
},
21+
"require-dev": {
22+
"phpunit/phpunit": "^8.5",
23+
"orchestra/testbench": "4.*|5.*"
24+
},
25+
"autoload": {
26+
"psr-4": {
27+
"MarkWalet\\Packagist\\": "src/"
28+
}
29+
},
30+
"autoload-dev": {
31+
"psr-4": {
32+
"MarkWalet\\Packagist\\Tests\\": "tests/"
33+
}
34+
},
35+
"config": {
36+
"sort-packages": true
37+
},
38+
"extra": {
39+
"laravel": {
40+
"providers": [
41+
"MarkWalet\\Packagist\\PackagistServiceProvider"
42+
],
43+
"aliases": {
44+
"Packagist": "MarkWalet\\Packagist\\Facades\\Packagist"
45+
}
46+
}
47+
}
48+
}

phpunit.xml

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit bootstrap="vendor/autoload.php"
3+
backupGlobals="false"
4+
backupStaticAttributes="false"
5+
colors="true"
6+
verbose="true"
7+
processIsolation="false"
8+
stopOnFailure="false">
9+
<testsuites>
10+
<testsuite name="Laravel Packagist Test Suite">
11+
<directory suffix="Test.php">./tests/</directory>
12+
</testsuite>
13+
</testsuites>
14+
<filter>
15+
<whitelist addUncoveredFilesFromWhitelist="true" processUncoveredFilesFromWhitelist="true">
16+
<directory suffix=".php">./src</directory>
17+
<exclude>
18+
<directory suffix=".php">./src/Exceptions/*</directory>
19+
</exclude>
20+
</whitelist>
21+
</filter>
22+
</phpunit>

src/Facades/Packagist.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
namespace MarkWalet\Packagist\Facades;
4+
5+
use Illuminate\Support\Facades\Facade;
6+
use Spatie\Packagist\PackagistClient;
7+
8+
/**
9+
* Class Packagist.
10+
*
11+
* @method static array|null getPackagesNames(?string $type = null, ?string $vendor = null)
12+
* @method static array|null getPackagesNamesByType(string $type)
13+
* @method static array|null getPackagesNamesByVendor(string $vendor)
14+
* @method static array|null searchPackages($name = null, array $filters = [], ?int $page = 1, int $perPage = 15)
15+
* @method static array|null searchPackagesByName(string $name, ?int $page = 1, int $perPage = 15):
16+
* @method static array|null searchPackagesByTags(string $tags, ?string $name = null, ?int $page = 1, int $perPage = 15)
17+
* @method static array|null searchPackagesByType(string $type, ?string $name = null, ?int $page = 1, int $perPage = 15)
18+
* @method static array|null getPackage(string $vendor, ?string $package = null)
19+
* @method static array|null getPackageMetadata(string $vendor, ?string $package = null)
20+
* @method static array|null getStatistics()
21+
* @see PackagistClient
22+
*/
23+
class Packagist extends Facade
24+
{
25+
/**
26+
* Get the registered name of the component.
27+
*
28+
* @return string
29+
*/
30+
protected static function getFacadeAccessor()
31+
{
32+
return PackagistClient::class;
33+
}
34+
}

src/PackagistServiceProvider.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
namespace MarkWalet\Packagist;
4+
5+
use GuzzleHttp\Client;
6+
use Illuminate\Config\Repository;
7+
use Illuminate\Contracts\Foundation\Application;
8+
use Illuminate\Contracts\Support\DeferrableProvider;
9+
use Illuminate\Support\ServiceProvider;
10+
use Spatie\Packagist\PackagistClient;
11+
use Spatie\Packagist\PackagistUrlGenerator;
12+
13+
class PackagistServiceProvider extends ServiceProvider implements DeferrableProvider
14+
{
15+
/**
16+
* Register any application services.
17+
*
18+
* @return void
19+
*/
20+
public function register()
21+
{
22+
// Bind manager to application.
23+
$this->app->bind(PackagistClient::class, function (Application $app) {
24+
$urlGenerator = $app->make(PackagistUrlGenerator::class);
25+
26+
return new PackagistClient(new Client(), $urlGenerator);
27+
});
28+
29+
$this->app->bind(PackagistUrlGenerator::class, function (Application $app) {
30+
/** @var Repository $config */
31+
$config = $app['config'];
32+
33+
return new PackagistUrlGenerator(
34+
$config->get('services.packagist.base_url', null),
35+
$config->get('services.packagist.repo_url', null)
36+
);
37+
});
38+
}
39+
40+
/**
41+
* Get the services provided by the provider.
42+
*
43+
* @return array
44+
*/
45+
public function provides()
46+
{
47+
return [
48+
PackagistClient::class,
49+
];
50+
}
51+
}

0 commit comments

Comments
 (0)