Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add helpers #25

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Release

on:
push:
tags:
- '*.*.*'

jobs:
release:
runs-on: macos-latest
steps:
- uses: actions/checkout@v2
with:
fetch-depth: '0'

# @see https://github.com/actions/create-release/issues/38#issuecomment-715327220
# @see https://docs.github.com/en/free-pro-team@latest/actions/reference/workflow-commands-for-github-actions#environment-files
- name: Prepare the changelog from the tag message
id: prepare_changelog
run: |
PRERELEASE=false
# Check release type
if [[ $GITHUB_REF_NAME =~ 'alpha' || $GITHUB_REF_NAME =~ 'beta' || $GITHUB_REF_NAME =~ 'rc' ]]; then
echo "This is a prerelease."
PRERELEASE=true
fi
echo "is_prerelease=$PRERELEASE" >> $GITHUB_ENV

# @see https://github.com/actions/create-release
- name: Create Release
id: create_release
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ github.ref }}
release_name: v${{ github.ref_name }}
body: Please refer to [CHANGELOG.md](https://github.com/studiometa/wp-toolkit/blob/${{ github.ref_name }}/CHANGELOG.md) for details.
draft: false
prerelease: ${{ env.is_prerelease }}
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),

### Added

- **Helpers:**
+ Add a `RequestHelper` class ([#25](https://github.com/studiometa/wp-toolkit/pull/25))
+ Add a `request(): Request` function based on [symfony/http-foundation](https://github.com/symfony/http-foundation) ([#25](https://github.com/studiometa/wp-toolkit/pull/25))
+ Add an `EnvHelper` class ([#25](https://github.com/studiometa/wp-toolkit/pull/25))
+ Add an `env( string $key ): string` function ([#25](https://github.com/studiometa/wp-toolkit/pull/25))
- **CleanupManager:** Disable XML-RPC by default ([#21](https://github.com/studiometa/wp-toolkit/pull/21))
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"require": {
"php": "^7.3|^8.0",
"symfony/yaml": "^5.1",
"studiometa/webpack-config": "^4.0"
"studiometa/webpack-config": "^4.0|^5.0",
"symfony/http-foundation": "^5.4|^6.3"
},
"require-dev": {
"squizlabs/php_codesniffer": "^3.4",
Expand All @@ -21,7 +22,10 @@
"autoload": {
"psr-4": {
"Studiometa\\WPToolkit\\": "src/"
}
},
"files": [
"src/functions.php"
]
},
"scripts": {
"phpcs": "phpcs -s --colors --standard=./phpcs.xml",
Expand Down
92 changes: 84 additions & 8 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions src/Helpers/EnvHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
/**
* Request helper functions.
*
* @package studiometa/wp-toolkit
* @author Studio Meta <[email protected]>
* @copyright 2021 Studio Meta
* @license https://opensource.org/licenses/MIT
* @since 1.1.0
*/

namespace Studiometa\WPToolkit\Helpers;

/**
* Request helper class.
*/
class EnvHelper {
/**
* Get an environment variable value.
*
* @param string $key The variable name.
* @return string
*/
public static function get( string $key ): string {

Check warning on line 24 in src/Helpers/EnvHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Helpers/EnvHelper.php#L24

Added line #L24 was not covered by tests
// phpcs:ignore
/** @var array<string, string> Good type. */
$env = $_ENV;

Check warning on line 27 in src/Helpers/EnvHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Helpers/EnvHelper.php#L27

Added line #L27 was not covered by tests
// In some environment, values are not available in the `$_ENV` variables,
// so we use `getenv` as a fallback to try and get the value.
return $env[ $key ] ?? (string) getenv( $key );

Check warning on line 30 in src/Helpers/EnvHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Helpers/EnvHelper.php#L30

Added line #L30 was not covered by tests
}
}
1 change: 0 additions & 1 deletion src/Helpers/PluginHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
* @copyright 2021 Studio Meta
* @license https://opensource.org/licenses/MIT
* @since 1.0.0
* @version 1.0.0
*/

namespace Studiometa\WPToolkit\Helpers;
Expand Down
42 changes: 42 additions & 0 deletions src/Helpers/RequestHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php
/**
* Request helper functions.
*
* @package studiometa/wp-toolkit
* @author Studio Meta <[email protected]>
* @copyright 2021 Studio Meta
* @license https://opensource.org/licenses/MIT
* @since 1.1.0
*/

namespace Studiometa\WPToolkit\Helpers;

use Symfony\Component\HttpFoundation\HeaderBag;
use Symfony\Component\HttpFoundation\InputBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\ParameterBag;

/**
* Request helper class.
*/
class RequestHelper {
/**
* Request instance.
*
* @var Request|null
*/
private static $request = null;

/**
* Get the Request instance.
*
* @return Request
*/
public static function get(): Request {
if ( is_null( self::$request ) ) {
self::$request = Request::createFromGlobals();

Check warning on line 37 in src/Helpers/RequestHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Helpers/RequestHelper.php#L35-L37

Added lines #L35 - L37 were not covered by tests
}

return self::$request;

Check warning on line 40 in src/Helpers/RequestHelper.php

View check run for this annotation

Codecov / codecov/patch

src/Helpers/RequestHelper.php#L40

Added line #L40 was not covered by tests
}
}
35 changes: 35 additions & 0 deletions src/functions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/**
* Helper functions.
*
* @package studiometa/wp-toolkit
* @author Studio Meta <[email protected]>
* @copyright 2023 Studio Meta
* @license https://opensource.org/licenses/MIT
* @since 1.1.0
*/

namespace Studiometa\WPToolkit;

use Symfony\Component\HttpFoundation\Request;
use Studiometa\WPToolkit\Helpers\RequestHelper;
use Studiometa\WPToolkit\Helpers\EnvHelper;

/**
* Get the Request instance.
*
* @return Request
*/
function request(): Request {
return RequestHelper::get();

Check warning on line 24 in src/functions.php

View check run for this annotation

Codecov / codecov/patch

src/functions.php#L24

Added line #L24 was not covered by tests
}

/**
* Get an environment variable value.
*
* @param string $key The variable name.
* @return string
*/
function env( string $key ): string {
return EnvHelper::get( $key );

Check warning on line 34 in src/functions.php

View check run for this annotation

Codecov / codecov/patch

src/functions.php#L34

Added line #L34 was not covered by tests
}
Loading