Skip to content

Commit e7c3bf5

Browse files
committed
Feature: Add the short_array_syntax argument
1 parent bde4622 commit e7c3bf5

File tree

5 files changed

+15
-7
lines changed

5 files changed

+15
-7
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ This parameter accepts an array holding options.
7373
- `base_dir_var` : (string) the variable or constant name that is prefixed before the inclusion path.
7474
- `output_var_name` : (string) the variable string that the map array is assigned to. Default: `$aClassMap`. If `return` is set, the variable will not be set but the file just returns the generated map array.
7575
- `do_in_constructor` : (boolean) whether to perform the action in the constructor. Default: `true`.
76-
- `structure` : (string) either `CLASS` or `PATH`. When `CLASS` is set, the generated array keys consist of class names. When `PATH` is set, the generated array keys consist of paths. Default: `CLASS`.
76+
- `structure` : (string) either `CLASS` or `PATH`. When `CLASS` is set, the generated array keys consist of class names. When `PATH` is set, the generated array keys consist of paths. Default: `CLASS`.
77+
- `short_array_syntax` : (boolean) whether to use `array()` or `[]` for array declaration. `true` for `[]`. Default: `false`.
7778
- `search` : (array) the arguments for the directory search options.
7879
- `allowed_extensions`: (array) allowed file extensions to be listed. e.g. `[ 'php', 'inc' ]`
7980
- `exclude_dir_paths`: (array) directory paths to exclude from the list.

changelog.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
# Change Log
22

3+
## 1.2.0 - 2020/01/19
4+
- Added the `short_array_syntax` argument.
5+
36
## 1.1.1 - 2020/11/06
47
- Fixed a redundant white spaces and lines in outputs.
58
- Fixed a bug that listed directories with a name with an extension that is specified with the search option when only files should be listed.

source/PHPClassMapGenerator.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ public function __construct( $sBaseDirPath, $asScanDirPaths, $sOutputFilePath, a
117117
'output_var_name' => '$aClassMap',
118118
'structure' => 'CLASS', // 1.1.0 Accepted values: `CLASS`, `PATH` For `CLASS`, the generated array keys consist of class names. For `PATH` array keys will consist of file paths.
119119
'do_in_constructor' => true, // 1.1.0 Whether to perform the task in the constructor
120+
'short_array_syntax' => false, // [1.2.0+]
120121

121122
// Search options
122123
'search' => array(
@@ -151,18 +152,20 @@ private function ___write( $sOutputFilePath ) {
151152
*/
152153
public function getMap() {
153154

154-
$_aData = array(
155+
$_sOpening = $this->aOptions[ 'short_array_syntax' ] ? '[' : 'array(';
156+
$_sClosing = $this->aOptions[ 'short_array_syntax' ] ? ']' : ')';
157+
$_aData = array(
155158
mb_convert_encoding( '<?php ' . PHP_EOL . $this->sHeaderComment, 'UTF-8', 'auto' ),
156159
'return' === $this->aOptions[ 'output_var_name' ]
157-
? 'return array(' . PHP_EOL
158-
: $this->aOptions[ 'output_var_name' ] . ' = array( ' . PHP_EOL,
160+
? "return {$_sOpening}" . PHP_EOL
161+
: $this->aOptions[ 'output_var_name' ] . " = {$_sOpening} " . PHP_EOL,
159162
);
160163
foreach( $this->get() as $_sClassName => $_sPath ) {
161164
$_sClassName = str_replace( '\\', '\\\\', $_sClassName ); // escape backslashes as \t (tab character) will cause a problem
162165
$_aData[] = " " . '"' . $_sClassName . '"' . ' => '
163166
. $_sPath . ', ' . PHP_EOL;
164167
}
165-
$_aData[] = ');';
168+
$_aData[] = "{$_sClosing};";
166169
return trim( implode( '', $_aData ) );
167170

168171
}

test/class-map.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
2-
return array(
2+
return [
33
"Foo\\FooClass" => CLASS_MAP_BASE_DIR_VAR . "/_scandir/FooClass.php",
44
"Foo\\FooClass_Base" => CLASS_MAP_BASE_DIR_VAR . "/_scandir/FooClass_Base.php",
55
"NoNamespace" => CLASS_MAP_BASE_DIR_VAR . "/_scandir/NoNamespace.php",
66
"Joe\\JoeInterface" => CLASS_MAP_BASE_DIR_VAR . "/_scandir/interfaces/JoeInterface.php",
77
"Bar\\Bar" => CLASS_MAP_BASE_DIR_VAR . "/_scandir/traits/BarTrait.php",
8-
);
8+
];

test/generate-map-_scandir.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@
88
__DIR__ . '/class-map.php',
99
[
1010
'output_var_name' => 'return',
11+
'short_array_syntax' => true,
1112
]
1213
);

0 commit comments

Comments
 (0)