Skip to content

Commit

Permalink
Merge pull request #24 from SergkeiM/feature/add-laravel-support
Browse files Browse the repository at this point in the history
Added laravel support
  • Loading branch information
SergkeiM authored Aug 21, 2024
2 parents 49eac0a + 87bde85 commit 74ba333
Show file tree
Hide file tree
Showing 18 changed files with 571 additions and 57 deletions.
66 changes: 55 additions & 11 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,76 @@ name: PHP Tests
on: [pull_request]

jobs:
lint:
cs-fixer:
name: PHP-CS-Fixer
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: docker://ghcr.io/php-cs-fixer/php-cs-fixer:3-php8.2
with:
args: check
test:
name: Test on PHP ${{ matrix.php-versions }}
tests:
name: PHP ${{ matrix.php }}; Laravel ${{ matrix.laravel }}
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
php-versions: ['8.0', '8.1', '8.2']
php: ['8.0', '8.1', '8.2', '8.3']
laravel: ['9', '10', '11']
exclude:
- php: '8.0'
laravel: '10'
- php: '8.0'
laravel: '11'
- php: '8.1'
laravel: '11'
- php: '8.3'
laravel: '9'
steps:
- uses: actions/checkout@v4
- name: Checkout Code
uses: actions/checkout@v4

- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
php-version: ${{ matrix.php }}
tools: composer:v2
coverage: none
env:
update: true

- name: Setup Problem Matchers
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Install Composer Dependencies
uses: ramsey/composer-install@v3
- name: Select Laravel 9
uses: nick-invision/retry@v3
with:
timeout_minutes: 5
max_attempts: 5
command: composer require "laravel/framework:9.*" "phpunit/phpunit:^9.6" --no-update --no-interaction
if: "matrix.laravel == '9'"

- name: Run phpunit
run: vendor/bin/phpunit --verbose
- name: Select Laravel 10
uses: nick-invision/retry@v3
with:
timeout_minutes: 5
max_attempts: 5
command: composer require "laravel/framework:10.*" "phpunit/phpunit:^10.1" --no-update --no-interaction
if: "matrix.laravel == '10'"

- name: Select Laravel 11
uses: nick-invision/retry@v3
with:
timeout_minutes: 5
max_attempts: 5
command: composer require "laravel/framework:11.*" "phpunit/phpunit:^10.5" --no-update --no-interaction
if: "matrix.laravel == '11'"

- name: Install PHP Dependencies
uses: nick-invision/retry@v3
with:
timeout_minutes: 5
max_attempts: 5
command: composer update --no-interaction --no-progress

- name: Execute PHPUnit
run: vendor/bin/phpunit
27 changes: 20 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
# PHP Client for Cloudflare API

![Build Status](https://github.com/SergkeiM/php-cloudflare-api/actions/workflows/tests.yml/badge.svg)
[![Latest Stable Version](https://poser.pugx.org/sergkeim/php-cloudflare-api/v/stable)](https://packagist.org/packages/sergkeim/php-cloudflare-api)
[![Total Downloads](https://poser.pugx.org/sergkeim/php-cloudflare-api/downloads)](https://packagist.org/packages/sergkeim/php-cloudflare-api)
[![Monthly Downloads](https://poser.pugx.org/sergkeim/php-cloudflare-api/d/monthly)](https://packagist.org/packages/sergkeim/php-cloudflare-api)
[![Daily Downloads](https://poser.pugx.org/sergkeim/php-cloudflare-api/d/daily)](https://packagist.org/packages/sergkeim/php-cloudflare-api)
A simple PHP Client for [Cloudflare API](https://developers.cloudflare.com/api/).

![Banner](/docs/public/preview.jpg)

<p align="center">
<a href="https://github.com/sergkeim/php-cloudflare-api/actions?query=workflow%3ATests">
<img src="https://img.shields.io/github/actions/workflow/status/sergkeim/php-cloudflare-api/tests.yml?label=Tests&style=flat-square" alt="Build Status"/>
</a>
<a href="LICENSE">
<img src="https://img.shields.io/badge/license-MIT-brightgreen?style=flat-square" alt="Software License"/>
</a>
<a href="https://packagist.org/packages/sergkeim/php-cloudflare-api">
<img src="https://img.shields.io/packagist/dt/sergkeim/php-cloudflare-api?style=flat-square" alt="Packagist Downloads"/>
</a>
<a href="https://github.com/sergkeim/php-cloudflare-api/releases">
<img src="https://img.shields.io/github/release/sergkeim/php-cloudflare-api?style=flat-square" alt="Latest Version"/>
</a>
</p>

> **Note**: This package is under active development as I expand it to cover Cloudflare API. Consider the public API of this package a little unstable as I work towards a v1.0.
A simple Object Oriented PHP Client for Cloudflare API.

This package provides convenient access to the Cloudflare REST API using PHP.

- [☁️ Cloudflare API v4](https://developers.cloudflare.com/api/)
Expand All @@ -34,6 +45,8 @@ composer require sergkeim/php-cloudflare-api

* Thanks to [Cloudflare](https://developers.cloudflare.com/api/) for the high quality API and documentation.
* Thanks to [KnpLabs](https://github.com/KnpLabs) for [php-github-api](https://github.com/KnpLabs/php-github-api) used as inspiration for this package.
* Thanks to [Graham Campbell](https://github.com/GrahamCampbell) for [Laravel Manager](https://github.com/GrahamCampbell/Laravel-Manager?tab=readme-ov-file) that provides some manager functionality for Laravel.
* Thanks to [Graham Campbell](https://github.com/GrahamCampbell) for [Laravel TestBench](https://github.com/GrahamCampbell/Laravel-TestBench) that provides some testing functionality for Laravel.
* Thanks to [Guzzle](https://github.com/guzzle/guzzle) for the HTTP Client that makes it easy to send HTTP requests

## License 📎
Expand Down
19 changes: 14 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@
},
"require": {
"php": "^8.0",
"ext-json": "*",
"guzzlehttp/guzzle": "^7.9"
"guzzlehttp/guzzle": "^7.9",
"illuminate/contracts": "^9.0 || ^10.0 || ^11.0",
"illuminate/support": "^9.0 || ^10.0 || ^11.0",
"graham-campbell/manager": "^5.1"
},
"require-dev": {
"phpunit/phpunit": "^8.5 || ^9.4",
"symfony/phpunit-bridge": "^7.1",
"friendsofphp/php-cs-fixer": "^3.62"
"phpunit/phpunit": "^9.6.17 || ^10.5.13",
"friendsofphp/php-cs-fixer": "^3.62",
"graham-campbell/testbench": "^6.1"
},
"autoload": {
"psr-4": {
Expand All @@ -46,5 +48,12 @@
"scripts": {
"test": "vendor/bin/phpunit",
"fix": "vendor/bin/php-cs-fixer fix"
},
"extra": {
"laravel": {
"providers": [
"Cloudflare\\CloudflareServiceProvider"
]
}
}
}
44 changes: 44 additions & 0 deletions config/cloudflare.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of Laravel Cloudflare.
*/

return [

/*
|--------------------------------------------------------------------------
| Default Connection Name
|--------------------------------------------------------------------------
|
| Here you may specify which of the connections below you wish to use as
| your default connection for all work. Of course, you may use many
| connections at once using the manager class.
|
*/

'default' => 'main',

/*
|--------------------------------------------------------------------------
| Cloudflare Connections
|--------------------------------------------------------------------------
|
| Here are each of the connections setup for your application. Example
| configuration has been included, but you may add as many connections as
| you would like.
|
*/

'connections' => [

'main' => [
'token' => 'your-token',
],

'secondary' => [
'token' => 'your-token',
],
],

];
2 changes: 1 addition & 1 deletion docs/app.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export default defineAppConfig({
title: 'PHP Client for Cloudflare API.',
description: 'This package provides convenient access to the Cloudflare REST API using PHP.',
layout: 'default',
image: '/preview.png',
image: '/preview.jpg',
url: 'https://php-cloudflare-api.nuxt.space/',
socials: {
github: 'SergkeiM/php-cloudflare-api'
Expand Down
Binary file added docs/public/preview.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed docs/public/preview.png
Binary file not shown.
48 changes: 16 additions & 32 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,34 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit backupGlobals="false"
backupStaticAttributes="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
<testsuite name="php-cloudflare-api Test Suite">
<directory>./test/</directory>
</testsuite>
</testsuites>

<groups>
<exclude>
<group>integration</group>
</exclude>
</groups>

<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" colors="true" processIsolation="false" stopOnFailure="false" bootstrap="vendor/autoload.php" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd" cacheDirectory=".phpunit.cache" backupStaticProperties="false">
<testsuites>
<testsuite name="php-cloudflare-api Test Suite">
<directory>./test/</directory>
</testsuite>
</testsuites>
<groups>
<exclude>
<group>integration</group>
</exclude>
</groups>
<source>
<include>
<directory suffix=".php">./src/</directory>
</include>
</source>
</phpunit>
25 changes: 25 additions & 0 deletions src/CloudflareFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Cloudflare;

use Illuminate\Support\Arr;
use InvalidArgumentException;

class CloudflareFactory
{
/**
* Make a new github client.
*
* @param string[] $config
*
* @throws \InvalidArgumentException
*
* @return \Cloudflare\Client
*/
public function make(array $config): Client
{
$client = new Client(Arr::get($config, 'token'));

return $client;
}
}
92 changes: 92 additions & 0 deletions src/CloudflareManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?php

namespace Cloudflare;

use GrahamCampbell\Manager\AbstractManager;
use Illuminate\Contracts\Config\Repository;

/**
* This is the cloudflare manager class.
*
* @method \Cloudflare\Client connection(string|null $name = null)
* @method \Cloudflare\Client reconnect(string|null $name = null)
* @method void disconnect(string|null $name = null)
* @method array<string,\Cloudflare\Client> getConnections()
* @method \Cloudflare\Endpoints\Accounts accounts()
* @method \Cloudflare\Endpoints\D1 d1()
* @method \Cloudflare\Endpoints\IP ips()
* @method \Cloudflare\Endpoints\Zones zones()
* @method \Cloudflare\Endpoints\Workers workers()
* @method \Cloudflare\Endpoints\Tunnel tunnel()
*/
class CloudflareManager extends AbstractManager
{
/**
* The factory instance.
*
* @var \Cloudflare\CloudflareFactory
*/
protected CloudflareFactory $factory;

/**
* Create a new github manager instance.
*
* @param \Illuminate\Contracts\Config\Repository $config
* @param \Cloudflare\CloudflareFactory $factory
*
* @return void
*/
public function __construct(Repository $config, CloudflareFactory $factory)
{
parent::__construct($config);
$this->factory = $factory;
}

/**
* Create the connection instance.
*
* @param array $config
*
* @return \Cloudflare\Client
*/
protected function createConnection(array $config): Client
{
return $this->factory->make($config);
}

/**
* Get the configuration name.
*
* @return string
*/
protected function getConfigName(): string
{
return 'cloudflare';
}

/**
* Get the configuration for a connection.
*
* @param string|null $name
*
* @throws \InvalidArgumentException
*
* @return array
*/
public function getConnectionConfig(string $name = null): array
{
$config = parent::getConnectionConfig($name);

return $config;
}

/**
* Get the factory instance.
*
* @return \Cloudflare\CloudflareFactory
*/
public function getFactory(): CloudflareFactory
{
return $this->factory;
}
}
Loading

0 comments on commit 74ba333

Please sign in to comment.