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

Commit

Permalink
Merge branch 'hotfix/2'
Browse files Browse the repository at this point in the history
Close #2
Fixes #1
  • Loading branch information
weierophinney committed Feb 16, 2017
2 parents 2cc49b5 + 64332e4 commit dfbc80e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 13 deletions.
24 changes: 23 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,29 @@

All notable changes to this project will be documented in this file, in reverse chronological order by release.

## 1.0.0 - TBD
## 1.1.0 - 2017-02-16

### Added

- [#2](https://github.com/zfcampus/zf-composer-autoloading/pull/2) adds the
flags `-p`/`--modules-path`, allowing the user to specify the directory
holding the module/source tree for which autoloading will be provided.

### Deprecated

- Nothing.

### Removed

- Nothing.

### Fixed

- Nothing.

## 1.0.0 - 2016-08-12

Initial release.

### Added

Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ $ ./vendor/bin/autoload-module-via-composer \
> [help|--help|-h] \
> [--composer|-c <composer path>] \
> [--type|-t <psr0|psr4>] \
> [--modules-path|-p <path>] \
> modulename
```

Expand All @@ -41,6 +42,8 @@ $ ./vendor/bin/autoload-module-via-composer \
- `--type` and `-t` allow you to specify the autoloading type, which should be
one of `psr-0` or `psr-4`; if not provided, the script will attempt to
auto-determine the value based on the directory structure of the module.
- `--modules-path` and `-p` allow you to specify the path to the modules
directory; default to `module`.
- `modulename` is the name of the module for which to setup Composer-based
autoloading.

Expand Down Expand Up @@ -81,3 +84,10 @@ Examples
```bash
$ ./vendor/bin/autoload-module-via-composer -c composer.phar Status
```

1. Specify the path to modules directory, and generate a Composer autoloading
entry.

```bash
$ ./vendor/bin/autoload-module-via-composer -p src Status
```
9 changes: 7 additions & 2 deletions bin/autoload-module-via-composer
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@
*
* Usage:
*
* autoload-module-via-composer [help|--help|-h] [--composer|-c <composer path>] [--type|-t <psr0|psr4>] modulename
* autoload-module-via-composer
* [help|--help|-h]
* [--composer|-c <composer path>]
* [--type|-t <psr0|psr4>]
* [--modules-path|-p <path>]
* modulename
*
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2016-2017 Zend Technologies USA Inc. (http://www.zend.com)
*/

namespace ZF\ComposerAutoloading;
Expand Down
60 changes: 50 additions & 10 deletions src/Command.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2016 Zend Technologies USA Inc. (http://www.zend.com)
* @copyright Copyright (c) 2016-2017 Zend Technologies USA Inc. (http://www.zend.com)
*/

namespace ZF\ComposerAutoloading;
Expand All @@ -21,6 +21,7 @@ class Command
private $defaults = [
'composer' => 'composer',
'type' => false,
'modulesPath' => 'module',
];

/**
Expand All @@ -43,6 +44,11 @@ class Command
*/
private $modulePath;

/**
* @var string Filesystem path to modules directory
*/
private $modulesPath;

/**
* @var string Working path
*/
Expand Down Expand Up @@ -105,12 +111,13 @@ public function __invoke(array $args)
*/
private function help($stream = STDOUT)
{
// @codingStandardsIgnoreStart
$message = <<<'EOH'
Provide Composer-based autoloading for a Zend Framework module.
Usage:
autoload-module-via-composer [help|--help|-h] [--composer|-c <composer path>] [--type|-t psr0|psr4] modulename
autoload-module-via-composer [help|--help|-h] [--composer|-c <composer path>] [--type|-t psr0|psr4] [--modules-path|-p <path>] modulename
Arguments:
Expand All @@ -122,10 +129,13 @@ private function help($stream = STDOUT)
autodetermine the type, default to
PSR-0 autoloading if unable to
determine it.
- [--modules-path|-p <path>] Provide the path to the modules
directory; default to "module"
- modulename The name of the module for which
to provide composer autoloading.

EOH;
// @codingStandardsIgnoreEnd

$message = strtr($message, "\n", PHP_EOL);

Expand Down Expand Up @@ -204,8 +214,9 @@ private function validateComposerJson()
* - No unexpected arguments
* - --composer/-c argument, if present, represents a valid composer binary
* - --type/-t argument, if present, is valid
* - --modules-path/-p argument, if present, represents a valid path to modules directory
*
* Sets the module, modulePath, composer, and type properties.
* Sets the module, modulePath, composer, type and modulesPath properties.
*
* @param array $args
* @return bool Boolean false if invalid arguments detected
Expand All @@ -217,16 +228,10 @@ private function parseArguments(array $args)

// Get module argument (always expected in last position)
$this->module = $module = array_pop($args);
$this->modulePath = $modulePath = sprintf('%s/module/%s', $this->path, $module);

if (! is_dir($modulePath)) {
fwrite(STDERR, sprintf('Could not locate module "%s" in path "%s"%s', $module, $modulePath, PHP_EOL));
return false;
}

// Parse arguments
if (empty($args)) {
return true;
return $this->checkModulePath();
}

$args = array_values($args);
Expand Down Expand Up @@ -267,13 +272,48 @@ private function parseArguments(array $args)
$this->type = preg_replace('/^(psr)([04])$/', '$1-$2', $this->type);
break;

case '--modules-path':
// fall-through
case '-p':
$this->modulesPath = preg_replace('/^\.\//', '', str_replace('\\', '/', $args[$i + 1]));
if (! is_dir(sprintf('%s/%s', $this->path, $this->modulesPath))) {
fwrite(
STDERR,
'Provided path to modules directory does not exist or is not a directory'
. PHP_EOL . PHP_EOL
);
$this->help(STDERR);
return false;
}
break;

default:
fwrite(STDERR, sprintf('Unknown option "%s" provided%s', $args[$i], str_repeat(PHP_EOL, 2)));
$this->help(STDERR);
return false;
}
}

return $this->checkModulePath();
}

/**
* Checks if the module path exists and is a directory.
*
* @return bool
*/
private function checkModulePath()
{
$this->modulePath = sprintf('%s/%s/%s', $this->path, $this->modulesPath, $this->module);

if (! is_dir($this->modulePath)) {
fwrite(
STDERR,
sprintf('Could not locate module "%s" in path "%s"%s', $this->module, $this->modulePath, PHP_EOL)
);
return false;
}

return true;
}

Expand Down

0 comments on commit dfbc80e

Please sign in to comment.