-
Notifications
You must be signed in to change notification settings - Fork 144
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[2.x] Adds helpers to determine when an app is running on Vapor (#164)
* 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
1 parent
abfba34
commit de2ddb6
Showing
3 changed files
with
110 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')); | ||
} | ||
} |