Skip to content

Commit

Permalink
[2.x] Adds helpers to determine when an app is running on Vapor (#164)
Browse files Browse the repository at this point in the history
* adds active helpers

* update tests

* Apply fixes from StyleCI

* remove type hints

* update return types

* Update Vapor.php

---------

Co-authored-by: StyleCI Bot <[email protected]>
Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
3 people authored Sep 29, 2023
1 parent abfba34 commit de2ddb6
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
5 changes: 4 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,10 @@
"laravel": {
"providers": [
"Laravel\\Vapor\\VaporServiceProvider"
]
],
"aliases": {
"Vapor": "Laravel\\Vapor\\Vapor"
}
}
},
"config": {
Expand Down
50 changes: 50 additions & 0 deletions src/Vapor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Laravel\Vapor;

class Vapor
{
/**
* Determine whether the environment is Vapor.
*/
public static function active(): bool
{
return env('VAPOR_SSM_PATH') !== null;
}

/**
* Determine whether the environment is not Vapor.
*/
public static function inactive(): bool
{
return ! static::active();
}

/**
* Execute the callback if the environment is Vapor.
*
* @param mixed $whenActive
* @param mixed $whenInactive
* @return mixed
*/
public static function whenActive($whenActive, $whenInactive = null)
{
if (static::active()) {
return value($whenActive);
}

return value($whenInactive);
}

/**
* Execute the callback if the environment is not Vapor.
*
* @param mixed $whenInactive
* @param mixed $whenActive
* @return mixed
*/
public static function whenInactive($whenInactive, $whenActive = null)
{
return static::whenActive($whenActive, $whenInactive);
}
}
56 changes: 56 additions & 0 deletions tests/Unit/VaporTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

namespace Laravel\Vapor\Tests\Unit;

use Laravel\Vapor\Vapor;
use Orchestra\Testbench\TestCase;

class VaporTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

unset($_ENV['VAPOR_SSM_PATH']);
}

public function test_vapor_active()
{
$_ENV['VAPOR_SSM_PATH'] = '/my-project-production';

$this->assertTrue(Vapor::active());
}

public function test_vapor_inactive()
{
$this->assertTrue(Vapor::inactive());
}

public function test_vapor_when_active()
{
$_ENV['VAPOR_SSM_PATH'] = '/my-project-production';

$this->assertSame('active', Vapor::whenActive('active', 'inactive'));
}

public function test_vapor_when_active_with_callback()
{
$_ENV['VAPOR_SSM_PATH'] = '/my-project-production';

$this->assertSame('active', Vapor::whenActive(function () {
return 'active';
}, 'inactive'));
}

public function test_vapor_when_inactive()
{
$this->assertSame('inactive', Vapor::whenInactive('inactive', 'active'));
}

public function test_vapor_when_inactive_with_callback()
{
$this->assertSame('inactive', Vapor::whenInactive(function () {
return 'inactive';
}, 'active'));
}
}

0 comments on commit de2ddb6

Please sign in to comment.