Skip to content

Commit

Permalink
Add support for loading personal token from environment variable or `…
Browse files Browse the repository at this point in the history
….env` file (#14)

* Add support for loading personal token from environment variable

The config class will look for `ENVATO_TOKEN`.

* Add support for loading personal token from `.env` file

Plugin requires vlucas/phpdotenv v4 or v5.

* Update Installation and Configuration instructions

Changed:
- Added notice that the plugin can be installed locally.
- Improved instructions on configuring the plugin to propose either the local `composer.json` file or the global `config.json` file.
- Added code block hint for environment variable.

* Improve environment variable check in EnvatoConfig
  • Loading branch information
mcaskill authored Feb 23, 2023
1 parent 3abd984 commit 12039e5
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
22 changes: 19 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,26 @@ Package version locking can only be achieved by local persistent cache, not acro

### Installation

This Composer plugin must be installed globally as it adds a virtual package repository.
This Composer plugin adds a virtual package repository.

It is recommended to install it globally:

```shell
composer global require --update-no-dev szepeviktor/composer-envato
```

It can also be installed per-project:

```shell
composer require --update-no-dev szepeviktor/composer-envato
```

### Configuration

Add all your Envato products to your `config.json` (located in `$COMPOSER_HOME`) as "packages".
Add all your Envato products as "packages" in either the local `composer.json`
file or the global `config.json` file (located in `$COMPOSER_HOME`).

You find `item-id` at the end of product URL-s.
You can find the `item-id` at the end of product URL-s.
e.g. `https://themeforest.net/item/avada-responsive-multipurpose-theme/2833226`

```json
Expand All @@ -48,6 +57,13 @@ e.g. `https://themeforest.net/item/avada-responsive-multipurpose-theme/2833226`

:bulb: Please use the vendor name `envato` for consistency.

The personal token can also be read from an environment variable or a `.env` file.
Create a `.env` file, where the `composer.json` file lives, and add the following:

```ini
ENVATO_TOKEN="<YOUR ENVATO PERSONAL TOKEN FROM https://build.envato.com/create-token>"
```

### Usage

Once the plugin is installed and configured,
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"require": {
"php": "^7.4 || ^8.0",
"composer-plugin-api": "^2.0",
"composer/installers": "^1.10 || ^2.0"
"composer/installers": "^1.10 || ^2.0",
"vlucas/phpdotenv": "^4.3 || ^5.5"
},
"require-dev": {
"composer/composer": "^2.3.0",
Expand Down
21 changes: 21 additions & 0 deletions src/EnvatoConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,14 @@
namespace SzepeViktor\Composer\Envato;

use Composer\Config;
use Dotenv\Dotenv;

class EnvatoConfig
{
public const ENVATO_CONFIG = 'envato';

public const ENV_VAR_TOKEN = 'ENVATO_TOKEN';

/**
* @var array<mixed>
*/
Expand All @@ -25,6 +28,9 @@ public function __construct(Config $composerConfig)
$envatoConfig = $composerConfig->get(self::ENVATO_CONFIG);
$this->config = \is_array($envatoConfig) ? $envatoConfig : [];

$this->loadDotenv();
$this->mergeEnvConfig();

$this->valid = \array_key_exists('token', $this->config)
&& \is_string($this->config['token'])
&& $this->config['token'] !== ''
Expand Down Expand Up @@ -63,4 +69,19 @@ static function ($name, $data) {
$this->config['packages']
);
}

protected function loadDotenv(): void
{
$cwd = \getcwd();
if (\is_string($cwd) && \file_exists($cwd . DIRECTORY_SEPARATOR . '.env')) {
Dotenv::createImmutable($cwd)->safeLoad();
}
}

protected function mergeEnvConfig(): void
{
if (\array_key_exists(self::ENV_VAR_TOKEN, $_ENV)) {
$this->config['token'] = $_ENV[self::ENV_VAR_TOKEN];
}
}
}

0 comments on commit 12039e5

Please sign in to comment.