Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge branch 'feature/32-auto-composer-command' into develop
Browse files Browse the repository at this point in the history
Close #32
  • Loading branch information
weierophinney committed May 7, 2018
2 parents 4032d10 + b8c7f7a commit 7cffd35
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 1 deletion.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,18 @@ All notable changes to this project will be documented in this file, in reverse

### Added

- Nothing.
- [#32](https://github.com/zfcampus/zf-development-mode/pull/32) adds a new sub-command, `auto-composer`. When invoked, it uses the value of
the environment variable COMPOSER_DEV_MODE to determine whether to enable or disable development
mode locally. If the variable is not present, it does nothing; if `0`, it disables development
mode, and if `1`, it enables development mode. This can be particularly useful as a composer script:

```json
"scripts": {
"development-auto": "zf-development-mode auto-composer",
"post-install-cmd": ["@development-auto"],
"post-update-cmd": ["@development-auto"]
}
```

### Changed

Expand Down
87 changes: 87 additions & 0 deletions src/AutoComposer.php
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;
}
}
3 changes: 3 additions & 0 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ public function __invoke(array $arguments)
case 'status':
$status = new Status();
return $status();
case 'auto-composer':
$auto = new AutoComposer();
return $auto();
default:
fwrite(STDERR, 'Unrecognized argument.' . PHP_EOL . PHP_EOL);
$help(STDERR);
Expand Down
5 changes: 5 additions & 0 deletions src/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ class Help
(do not use in production).
status Determine if development mode is currently
enabled.
auto-composer Enable or disable development mode based on
the environment variable COMPOSER_DEV_MODE.
If the variable is not found, the mode is
untouched. If set to something other than "0",
it's enabled.
To enable development mode, the following file MUST exist:
Expand Down
74 changes: 74 additions & 0 deletions test/AutoComposerTest.php
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());
}
}

0 comments on commit 7cffd35

Please sign in to comment.