Skip to content

feat(php): Change environment variables loading in the playground #691

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

Merged
merged 1 commit into from
Jun 14, 2022
Merged
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
14 changes: 14 additions & 0 deletions playground/php/loadEnv.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
require '../../../clients/algoliasearch-client-php/vendor/autoload.php';

// Gets the vars from local environment
$env = getenv();

// If the script has been run from docker's playground, fetches the vars from .env file instead
if (isset($env['DOCKER']) && $env['DOCKER'] === "true") {
$dotenv = Dotenv\Dotenv::createImmutable('../..');
$dotenv->load();
$env = $_ENV;
}

return $env;
4 changes: 2 additions & 2 deletions playground/php/src/abtesting.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

require '../../../clients/algoliasearch-client-php/vendor/autoload.php';
$env = require_once('../loadEnv.php');

use Algolia\AlgoliaSearch\Api\AbtestingClient;

$client = AbtestingClient::create(getenv('ALGOLIA_APPLICATION_ID'), getenv('ALGOLIA_ANALYTICS_KEY'));
$client = AbtestingClient::create($env['ALGOLIA_APPLICATION_ID'], $env['ALGOLIA_ANALYTICS_KEY']);

$abTest = [
'name' => 'testing',
Expand Down
6 changes: 3 additions & 3 deletions playground/php/src/analytics.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

require '../../../clients/algoliasearch-client-php/vendor/autoload.php';
$env = require_once('../loadEnv.php');

use Algolia\AlgoliaSearch\Api\AnalyticsClient;

$client = AnalyticsClient::create(getenv('ALGOLIA_APPLICATION_ID'), getenv('ALGOLIA_ANALYTICS_KEY'));
$indexName = getenv('ANALYTICS_INDEX');
$client = AnalyticsClient::create($env['ALGOLIA_APPLICATION_ID'], $env['ALGOLIA_ANALYTICS_KEY']);
$indexName = $env['ANALYTICS_INDEX'];

var_dump(
$client->getTopFilterForAttribute(
Expand Down
6 changes: 3 additions & 3 deletions playground/php/src/insights.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?php

require '../../../clients/algoliasearch-client-php/vendor/autoload.php';
$env = require_once('../loadEnv.php');

use Algolia\AlgoliaSearch\Api\InsightsClient;

$client = InsightsClient::create(getenv('ALGOLIA_APPLICATION_ID'), getenv('ALGOLIA_ADMIN_KEY'));
$indexName = getenv('SEARCH_INDEX');
$client = InsightsClient::create($env['ALGOLIA_APPLICATION_ID'], $env['ALGOLIA_ADMIN_KEY']);
$indexName = $env['SEARCH_INDEX'];

$twoDaysAgoMs = (time() - (2 * 24 * 60 * 60)) * 1000;

Expand Down
4 changes: 2 additions & 2 deletions playground/php/src/personalization.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php

require '../../../clients/algoliasearch-client-php/vendor/autoload.php';
$env = require_once('../loadEnv.php');

use Algolia\AlgoliaSearch\Api\PersonalizationClient;

$client = PersonalizationClient::create(getenv('ALGOLIA_APPLICATION_ID'), getenv('ALGOLIA_RECOMMENDATION_KEY'));
$client = PersonalizationClient::create($env['ALGOLIA_APPLICATION_ID'], $env['ALGOLIA_RECOMMENDATION_KEY']);

var_dump(
$client->deleteUserProfile('userToken')
Expand Down
4 changes: 2 additions & 2 deletions playground/php/src/query-suggestions.php
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<?php

require '../../../clients/algoliasearch-client-php/vendor/autoload.php';
$env = require_once('../loadEnv.php');

use Algolia\AlgoliaSearch\Api\QuerySuggestionsClient;

$client = QuerySuggestionsClient::create(getenv('ALGOLIA_APPLICATION_ID'), getenv('QUERY_SUGGESTIONS_KEY'));
$client = QuerySuggestionsClient::create($env['ALGOLIA_APPLICATION_ID'], $env['QUERY_SUGGESTIONS_KEY']);

var_dump($client->getAllConfigs());
8 changes: 4 additions & 4 deletions playground/php/src/recommend.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
<?php

require '../../../clients/algoliasearch-client-php/vendor/autoload.php';
$env = require_once('../loadEnv.php');

use Algolia\AlgoliaSearch\Api\RecommendClient;

$client = RecommendClient::create(getenv('ALGOLIA_APPLICATION_ID'), getenv('ALGOLIA_ADMIN_KEY'));
$indexName = getenv('SEARCH_INDEX');
$query = getenv('SEARCH_QUERY');
$client = RecommendClient::create($env['ALGOLIA_APPLICATION_ID'], $env['ALGOLIA_ADMIN_KEY']);
$indexName = $env['SEARCH_INDEX'];
$query = $env['SEARCH_QUERY'];

var_dump($client->getRecommendations(
[
Expand Down
12 changes: 6 additions & 6 deletions playground/php/src/search.php
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
<?php

require '../../../clients/algoliasearch-client-php/vendor/autoload.php';
$env = require_once('../loadEnv.php');

use Algolia\AlgoliaSearch\Api\SearchClient;

$client = SearchClient::create(
getenv('ALGOLIA_APPLICATION_ID'),
getenv('ALGOLIA_ADMIN_KEY')
$env['ALGOLIA_APPLICATION_ID'],
$env['ALGOLIA_ADMIN_KEY']
);
$indexName = getenv('SEARCH_INDEX');
$indexName = $env['SEARCH_INDEX'];


$response = $client->saveObject(
$indexName,
['objectID' => "111", 'name' => getenv('SEARCH_QUERY')],
['objectID' => "111", 'name' => $env['SEARCH_QUERY']],
);

var_dump($response);
Expand All @@ -23,7 +23,7 @@
var_dump(
$client->search([
'requests' => [
['indexName' => $indexName, 'query' => getenv('SEARCH_QUERY')],
['indexName' => $indexName, 'query' => $env['SEARCH_QUERY']],
],
])
);
3 changes: 2 additions & 1 deletion templates/php/composer.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^3.5.0",
"phpunit/phpunit": "^9.3"
"phpunit/phpunit": "^9.3",
"vlucas/phpdotenv": "^5.4"
},
"autoload": {
"psr-4": { "{{escapedInvokerPackage}}\\" : "{{srcBasePath}}/" },
Expand Down