This repository has been archived by the owner on Jan 21, 2020. It is now read-only.
forked from 19FT/NFDevelopmentMode
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/32-auto-composer-command' into develop
Close #32
- Loading branch information
Showing
5 changed files
with
181 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,87 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zfcampus/zf-development-mode for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com) | ||
* @license https://github.com/zfcampus/zf-development-mode/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace ZF\DevelopmentMode; | ||
|
||
/** | ||
* Automatically switch to and from development mode based on type of composer | ||
* install/update used. | ||
* | ||
* If a development install is being performed (`--dev` flag or absence of | ||
* `--no-dev` flag), then it will enable development mode. Otherwise, it | ||
* disables it. This is determined by the value of the `COMPOSER_DEV_MODE` | ||
* environment variable that Composer sets. | ||
* | ||
* If the `COMPOSER_DEV_MODE` environment variable is missing, then the command | ||
* does nothing. | ||
*/ | ||
class AutoComposer | ||
{ | ||
const COMPOSER_DEV_MODE = 'COMPOSER_DEV_MODE'; | ||
|
||
/** | ||
* @var value of COMPOSER_DEV_MODE | ||
*/ | ||
private $composerDevMode; | ||
|
||
/** | ||
* @var resource | ||
*/ | ||
private $errorStream; | ||
|
||
private $expectedValues = [ | ||
'0', // production mode | ||
'1', // development mode | ||
]; | ||
|
||
/** | ||
* @param string Path to project. | ||
*/ | ||
private $projectDir; | ||
|
||
/** | ||
* @param string $projectDir Location to resolve project from. | ||
* @param null|resource $errorStream Stream to which to write errors; defaults to STDERR | ||
*/ | ||
public function __construct($projectDir = '', $errorStream = null) | ||
{ | ||
$this->composerDevMode = getenv(self::COMPOSER_DEV_MODE); | ||
$this->projectDir = $projectDir; | ||
$this->errorStream = is_resource($errorStream) ? $errorStream : STDERR; | ||
} | ||
|
||
/** | ||
* Enable or disable developer mode based on composerDevMode. | ||
* | ||
* @return int | ||
*/ | ||
public function __invoke() | ||
{ | ||
if ($this->composerDevMode === '' || $this->composerDevMode === false) { | ||
// Not running under composer; do nothing. | ||
echo 'COMPOSER_DEV_MODE not set. Nothing to do.' . PHP_EOL; | ||
return 0; | ||
} | ||
|
||
if ($this->composerDevMode === '0') { | ||
$disable = new Disable($this->projectDir, $this->errorStream); | ||
return $disable(); | ||
} | ||
|
||
if ($this->composerDevMode === '1') { | ||
$enable = new Enable($this->projectDir, $this->errorStream); | ||
return $enable(); | ||
} | ||
|
||
printf( | ||
'COMPOSER_DEV_MODE set to unexpected value (%s). Nothing to do.%s', | ||
var_export($this->composerDevMode, true), | ||
PHP_EOL | ||
); | ||
return 1; | ||
} | ||
} |
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
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,74 @@ | ||
<?php | ||
/** | ||
* @see https://github.com/zfcampus/zf-development-mode for the canonical source repository | ||
* @copyright Copyright (c) 2018 Zend Technologies USA Inc. (https://www.zend.com) | ||
* @license https://github.com/zfcampus/zf-development-mode/blob/master/LICENSE.md New BSD License | ||
*/ | ||
|
||
namespace ZFTest\DevelopmentMode; | ||
|
||
use org\bovigo\vfs\vfsStream; | ||
use org\bovigo\vfs\vfsStreamContainer; | ||
use PHPUnit_Framework_TestCase as TestCase; | ||
use ZF\DevelopmentMode\AutoComposer; | ||
|
||
class AutoComposerTest extends TestCase | ||
{ | ||
use RemoveCacheFileTrait; | ||
|
||
/** @var vfsStreamContainer */ | ||
private $projectDir; | ||
|
||
/** @var resource */ | ||
private $errorStream; | ||
|
||
public function setUp() | ||
{ | ||
$this->projectDir = vfsStream::setup('project', null, [ | ||
'config' => [ | ||
'autoload' => [], | ||
], | ||
'cache' => [], | ||
'data' => [], | ||
]); | ||
$this->errorStream = fopen('php://memory', 'w+'); | ||
} | ||
|
||
public function tearDown() | ||
{ | ||
if (is_resource($this->errorStream)) { | ||
fclose($this->errorStream); | ||
} | ||
} | ||
|
||
public function testIndicatesEnvironmentVariableNotSet() | ||
{ | ||
putenv('COMPOSER_DEV_MODE'); | ||
$command = new AutoComposer(vfsStream::url('project'), $this->errorStream); | ||
$this->expectOutputString('COMPOSER_DEV_MODE not set. Nothing to do.' . PHP_EOL); | ||
$this->assertSame(0, $command()); | ||
} | ||
|
||
public function testIndicatesEnvironmentVariableSetNull() | ||
{ | ||
putenv('COMPOSER_DEV_MODE=0'); | ||
$command = new AutoComposer(vfsStream::url('project'), $this->errorStream); | ||
$this->expectOutputString('Development mode was already disabled.' . PHP_EOL); | ||
$this->assertSame(0, $command()); | ||
} | ||
|
||
public function testIndicatesEnvironmentVariableSetOne() | ||
{ | ||
putenv('COMPOSER_DEV_MODE=1'); | ||
$command = new AutoComposer(vfsStream::url('project'), $this->errorStream); | ||
$this->assertSame(1, $command()); | ||
} | ||
|
||
public function testIndicatesEnvironmentVariableSetArbitrary() | ||
{ | ||
putenv('COMPOSER_DEV_MODE=XX'); | ||
$command = new AutoComposer(vfsStream::url('project'), $this->errorStream); | ||
$this->expectOutputString('COMPOSER_DEV_MODE set to unexpected value (\'XX\'). Nothing to do.' . PHP_EOL); | ||
$this->assertSame(1, $command()); | ||
} | ||
} |