Skip to content

Commit

Permalink
Merge pull request #77 from moltam/feature/multiple-root-scan
Browse files Browse the repository at this point in the history
Add multiple root directory scan
  • Loading branch information
lajax committed Apr 10, 2016
2 parents 4786795 + 7e6b710 commit 5215e3c
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 30 deletions.
12 changes: 10 additions & 2 deletions Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,20 @@ class Module extends \yii\base\Module {
];

/**
* @var string the root directory of the scanning.
* @var string|array The root directory or directories of the scanning. The path can be an alias or
* a full path.
*
* It is possible to define one root directory as string. In this case the `scanRootParentDirectory` will be used
* when determining the actual directory to scan.
*
* Multiple root directories can be declared in an array. In this case all items must point to the exact directory,
* as `scanRootParentDirectory` **will be omitted**.
*/
public $root = '@app';

/**
* @var bool Whether scan the defined `root` parent directory, or the folder itself.
* @var bool Whether scan the defined `root` parent directory, or the folder itself. This option is used only,
* when the `root` option contains a single directory as string (e.g. `'root' => '@app'`).
*
* <b>IMPORTANT</b>: Changing this from `true` to `false` could cause loss of translated items, as
* optimize action removes the missing items.
Expand Down
65 changes: 51 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,27 +122,62 @@ A more complex example including database table with multilingual support is bel

#### Configuring the scan root

The root path can be an alias or a full path (e.g. `@app` or `/webroot/site/`).

The file scanner will scan the configured folders for translatable elements. The following two options
determine the scan root directory: `root`, and `scanRootParentDirectory`. These options are defaults to
values that works with the Yii 2 advanced project template. If you are using basic template, you have to modify
values that works with the Yii 2 advanced project template. If you are using basic template, you have to modify
these settings.

The `root` options tells which is the root folder for project scan. However if `scanRootParentDirectory` is set to `true`
(which is the default value), the scan will run on the parent directory. This is desired behavior on advanced template,
because the `@app` is the root for the current app, which is a subfolder inside the project (so the entire root of the
project is the parent directory of `@app`).
The `root` options tells which is the root folder for project scan. It can contain a single directory (string),
or multiple directories (in an array).

The `scanRootParentDirectory` **is used only** if a single root directory is specified in a string.

**IMPORTANT: Changing these options could cause loss of translated items,
as optimize action removes the missing items.** So be sure to double check your configuration!

**a)** Single root directory:

For basic template the `@app` is also the root for the entire project. Because of this with the default value
of `scanRootParentDirectory`, the scan runs outside the project folder. This is not desired behavior, and
It is possible to define one root directory as string in the `root` option. In this case the `scanRootParentDirectory`
will be used when determining the actual directory to scan.

If `scanRootParentDirectory` is set to `true` (which is the default value), the scan will run on the parent directory.
This is desired behavior on advanced template, because the `@app` is the root for the current app, which is a subfolder
inside the project (so the entire root of the project is the parent directory of `@app`).

For basic template the `@app` is also the root for the entire project. Because of this with the default value
of `scanRootParentDirectory`, the scan runs outside the project folder. This is not desired behavior, and
changing the value to `false` solves this.

**IMPORTANT: Changing the `scanRootParentDirectory` from `true` to `false` could cause loss of translated items,
as optimize action removes the missing items.** Changing the root folder can cause also loss, so be sure to
double check your configuration!
**IMPORTANT: Changing the `scanRootParentDirectory` from `true` to `false` could cause loss of translated items,
as the root will be a different directory.**

For example:

| `root` value | `scanRootParentDirectory` value| Scanned directory |
|---|---|---|
| `/webroot/site/frontend` | `true` | `/webroot/site` |
| `/webroot/site/frontend` | `false` | `/webroot/site/frontend` |

Using of [authManager](http://www.yiiframework.com/doc-2.0/guide-security-authorization.html).
**b)** Multiple root directories:

examples:
Multiple root directories can be defined in an array. In this case all items must point to the exact directory,
as `scanRootParentDirectory` **will be omitted**.

For example:

```php
'root' => [
'@frontend',
'@vendor',
'/some/external/folder',
],
```

#### Using of [authManager](http://www.yiiframework.com/doc-2.0/guide-security-authorization.html)

Examples:

PhpManager:
```php
Expand All @@ -164,7 +199,7 @@ DbManager:
],
```

Front end translation:
#### Front end translation:

```php
'bootstrap' => ['translatemanager'],
Expand Down Expand Up @@ -456,7 +491,9 @@ Use it with the Yii CLI
Known issues
-----------

* Scanner is scanning parent root directory. [#12](https://github.com/lajax/yii2-translate-manager/pull/12)
* Scanner is scanning parent root directory [#12](https://github.com/lajax/yii2-translate-manager/pull/12).

You can overwrite this behavior with the `scanRootParentDirectory` option. (See Config section for details.)
* Frontend translation of strings in hidden tags corrupts HTML. [#45](https://github.com/lajax/yii2-translate-manager/issues/45)

Screenshots
Expand Down
55 changes: 41 additions & 14 deletions services/scanners/ScannerFile.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Yii;
use yii\helpers\Console;
use yii\helpers\FileHelper;
use yii\base\InvalidConfigException;
use lajax\translatemanager\services\Scanner;

/**
Expand Down Expand Up @@ -94,19 +95,33 @@ public function __construct(Scanner $scanner) {
/**
* @inheritdoc Initialise the $files static array.
*/
public function init()
public function init()
{
if (empty(self::$files[static::EXTENSION]) && in_array(static::EXTENSION, $this->module->patterns)) {
$root = realpath($this->_getRoot());
$this->initFiles();

parent::init();
}

protected function initFiles()
{
if (!empty(self::$files[static::EXTENSION]) || !in_array(static::EXTENSION, $this->module->patterns)) {
return;
}

self::$files[static::EXTENSION] = [];

foreach ($this->_getRoots() as $root) {
$root = realpath($root);
Yii::trace("Scanning " . static::EXTENSION . " files for language elements in: $root", 'translatemanager');
self::$files[static::EXTENSION] = FileHelper::findFiles($root, [

$files = FileHelper::findFiles($root, [
'except' => $this->module->ignoredItems,
'only' => [static::EXTENSION],
]);
self::$files[static::EXTENSION] = array_merge(self::$files[static::EXTENSION], $files);
}

parent::init();
self::$files[static::EXTENSION] = array_unique(self::$files[static::EXTENSION]);
}

/**
Expand Down Expand Up @@ -215,17 +230,29 @@ protected function checkTokens($options, $translatorTokens, $tokens)
abstract protected function getLanguageItem($buffer);

/**
* Returns the root directory of the project scan.
* @return string
* Returns the root directories to scan.
* @return array
*/
private function _getRoot()
private function _getRoots()
{
$root = Yii::getAlias($this->module->root);
if ($this->module->scanRootParentDirectory) {
$root = dirname($root);
$directories = [];

if (is_string($this->module->root)) {
$root = Yii::getAlias($this->module->root);
if ($this->module->scanRootParentDirectory) {
$root = dirname($root);
}

$directories[] = $root;
} elseif (is_array($this->module->root)) {
foreach ($this->module->root as $root) {
$directories[] = Yii::getAlias($root);
}
} else {
throw new InvalidConfigException("Invalid `root` option value!");
}
return $root;

return $directories;
}

/**
Expand Down

0 comments on commit 5215e3c

Please sign in to comment.