Skip to content

Commit db6811b

Browse files
committed
Added some public methods.
1 parent 581b951 commit db6811b

File tree

6 files changed

+112
-62
lines changed

6 files changed

+112
-62
lines changed

README.md

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ This parameter accepts an array holding options.
8282
- `is_recursive`: (boolean) whether to scan sub-directories.
8383
- `ignore_note_file_names`: (array) ignore note file names that tell the parser to skip the directory. When one of the files exist in the parsing directory, the directory will be skipped. Default: `[ 'ignore-class-map.txt' ]`,
8484

85-
##### Example
85+
##### Example
86+
87+
##### Generating a class map in the script directory.
8688
```php
8789
new \PHPClassMapGenerator\PHPClassMapGenerator(
8890
dirname( __DIR__ ),
@@ -103,6 +105,40 @@ new \PHPClassMapGenerator\PHPClassMapGenerator(
103105
]
104106
);
105107
```
108+
109+
##### Do not write to a file
110+
```php
111+
$_oGenerator = new \PHPClassMapGenerator\PHPClassMapGenerator(
112+
__DIR__, // base dir
113+
__DIR__ . '/_scandir', // scan dir name
114+
__DIR__ . '/class-map.php',
115+
[
116+
'do_in_constructor' => false,
117+
]
118+
);
119+
print_r( $_oGenerator->get() );
120+
```
121+
122+
#### Find CSS files
123+
```php
124+
$_oGenerator = new \PHPClassMapGenerator\PHPClassMapGenerator(
125+
__DIR__, // base dir
126+
__DIR__ . '/_scandir', // scan dir name
127+
__DIR__ . '/class-map.php',
128+
[
129+
'output_var_name' => 'return',
130+
'do_in_constructor' => false,
131+
'structure' => 'PATH',
132+
'search' => [
133+
'allowed_extensions' => [ 'css' ],
134+
'ignore_note_file_names' => [ 'ignore-css-map.txt' ],
135+
],
136+
]
137+
);
138+
139+
print_r( $_oGenerator->get() );
140+
```
141+
To find JavaScript files, change the `'css'` part to `'js'` in the `allowed_extensions` search argument.
106142

107143
## License
108144
Licensed under [MIT](./LICENSE).

changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Added the `structure` argument.
55
- Added the `ignore_note_file_names` search argument.
66
- Added the `do_in_constructor` argument.
7+
- Added some public methods.
78
- Supported no namespace classes.
89

910
## 1.0.1 - 2020/01/20

source/PHPClassMapGenerator.php

Lines changed: 68 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class PHPClassMapGenerator extends PHPClassMapGenerator_Base {
2929

3030
public $sOutputFilePath = '';
3131

32+
public $sHeaderComment = '';
33+
3234
public $aItems = array();
3335

3436
/**
@@ -117,16 +119,57 @@ public function __construct( $sBaseDirPath, $asScanDirPaths, $sOutputFilePath, a
117119
if ( ! $this->aOptions[ 'do_in_constructor' ] ) {
118120
return;
119121
}
120-
$this->do();
122+
123+
$this->write();
121124

122125
}
123126

124127
/**
125128
*
126129
* @since 1.1.0
127130
*/
128-
public function do() {
129-
$this->_do();
131+
public function write() {
132+
$this->___write( $this->sOutputFilePath );
133+
}
134+
135+
/**
136+
* @since 1.1.0
137+
* @return string
138+
*/
139+
public function getMap() {
140+
141+
$_aData = array(
142+
// Heading
143+
mb_convert_encoding( '<?php ' . PHP_EOL . $this->sHeaderComment, 'UTF-8', 'auto' ),
144+
145+
// Start array declaration
146+
'return' === $this->aOptions[ 'output_var_name' ]
147+
? 'return array( ' . PHP_EOL
148+
: $this->aOptions[ 'output_var_name' ] . ' = array( ' . PHP_EOL,
149+
);
150+
151+
// Insert the data
152+
foreach( $this->get() as $_sClassName => $_sPath ) {
153+
$_aData[] = " " . '"' . $_sClassName . '"' . ' => '
154+
. $_sPath . ', ' . PHP_EOL;
155+
}
156+
157+
// Close the array declaration
158+
$_aData[] = ');';
159+
160+
return trim( implode( '', $_aData ) );
161+
162+
}
163+
164+
/**
165+
* @return array
166+
* @since 1.1.0
167+
*/
168+
public function get() {
169+
if ( 'CLASS' !== $this->aOptions[ 'structure' ] ) {
170+
return $this->aItems;
171+
}
172+
return array_map( array( $this, '_getItemConvertedToPath' ), $this->aItems );
130173
}
131174

132175
/**
@@ -150,12 +193,13 @@ protected function _setProperties( $sBaseDirPath, $asScanDirPaths, $sOutputFileP
150193
$this->aOptions = $this->_getOptionsFormatted( $aOptions );
151194
$this->sCarriageReturn = php_sapi_name() == 'cli' ? PHP_EOL : '<br />';
152195
$this->aScanDirPaths = ( array ) $asScanDirPaths;
196+
$this->_setItems();
153197
}
154198

155199
/**
156200
* @since 1.1.0
157201
*/
158-
protected function _do() {
202+
protected function _setItems() {
159203

160204
if ( $this->aOptions[ 'output_buffer' ] ) {
161205
echo 'Searching files under the directories: ' . implode( ', ', $this->aScanDirPaths ) . $this->sCarriageReturn;
@@ -165,16 +209,17 @@ protected function _do() {
165209
$this->aItems = $this->getItems();
166210

167211
// 2. Generate the output script header comment
168-
$_sHeaderComment = $this->_getHeaderComment( $this->aItems, $this->aOptions );
169-
if ( $this->aOptions[ 'output_buffer' ] ) {
170-
echo( $_sHeaderComment ) . $this->sCarriageReturn;
171-
}
212+
$this->___setHeaderComment();
172213

173214
$this->_sort( $this->aItems );
174215

175-
$this->_write( $this->aItems, $this->sBaseDirPath, $this->sOutputFilePath, $_sHeaderComment );
176-
177216
}
217+
private function ___setHeaderComment() {
218+
$this->sHeaderComment = $this->_getHeaderComment( $this->aItems, $this->aOptions );
219+
if ( $this->aOptions[ 'output_buffer' ] ) {
220+
echo( $this->sHeaderComment ) . $this->sCarriageReturn;
221+
}
222+
}
178223

179224
/**
180225
* @param array $aOptions
@@ -224,23 +269,12 @@ protected function _sort( array &$aItems ) {
224269
}
225270
}
226271

227-
/**
228-
* Write to a file.
229-
* @since 1.1.0
230-
*/
231-
protected function _write( array $aItems, $sBaseDirPath, $sOutputFilePath, $sHeaderComment ) {
232-
$this->___write(
233-
$aItems,
234-
$sBaseDirPath,
235-
$sOutputFilePath,
236-
$sHeaderComment,
237-
$this->aOptions[ 'output_var_name' ],
238-
$this->aOptions[ 'base_dir_var' ]
239-
);
240-
}
241-
242272
private function ___sort( array $aItems, array $aExcludingClassNames ) {
243273

274+
if ( 'CLASS' !== $this->aOptions[ 'structure' ] ) {
275+
return $aItems;
276+
}
277+
244278
$aItems = $this->___getDefinedObjectConstructsExtracted( $aItems, $aExcludingClassNames );
245279
foreach( $aItems as $_sClassName => $_aFile ) {
246280
if ( in_array( $_sClassName, $aExcludingClassNames ) ) {
@@ -266,42 +300,23 @@ private function ___getDefinedObjectConstructsExtracted( array $aItems, array $a
266300

267301
}
268302

269-
private function ___write( array $aFiles, $sBaseDirPath, $sOutputFilePath, $sHeadingComment, $sOutputArrayVar, $sBaseDirVar ) {
270-
271-
$_aData = array();
272-
273-
// Create a heading.
274-
$_aData[] = mb_convert_encoding( '<?php ' . PHP_EOL . $sHeadingComment, 'UTF-8', 'auto' );
275-
276-
// Start array declaration
277-
$_aData[] = 'return' === $sOutputArrayVar
278-
? 'return array( ' . PHP_EOL
279-
: $sOutputArrayVar . ' = array( ' . PHP_EOL;
280-
281-
// Insert the data
282-
$sBaseDirVar = $sBaseDirVar ? $sBaseDirVar : '""';
283-
foreach( $aFiles as $_sClassName => $_aFile ) {
284-
$_sPath = str_replace( '\\', '/', $_aFile[ 'path' ] );
285-
$_sPath = $this->___getRelativePath( $sBaseDirPath, $_sPath );
286-
$_aData[] = " " . '"' . $_sClassName . '"' . ' => '
287-
. $sBaseDirVar . ' . "' . $_sPath . '", ' . PHP_EOL;
288-
}
289-
290-
// Close the array declaration
291-
$_aData[] = ');' . PHP_EOL;
303+
private function ___write( $sOutputFilePath ) {
292304

293305
// Remove the existing file.
294306
if ( file_exists( $sOutputFilePath ) ) {
295307
unlink( $sOutputFilePath );
296308
}
297309

298310
// Write to a file.
299-
file_put_contents(
300-
$sOutputFilePath,
301-
trim( implode( '', $_aData ) ) . PHP_EOL,
302-
FILE_APPEND | LOCK_EX
303-
);
311+
file_put_contents( $sOutputFilePath, $this->getMap() . PHP_EOL, FILE_APPEND | LOCK_EX );
312+
313+
}
304314

315+
protected function _getItemConvertedToPath( $aItem ) {
316+
$_sBaseDirVar = $this->aOptions[ 'base_dir_var' ];
317+
$_sPath = str_replace( '\\', '/', $aItem[ 'path' ] );
318+
$_sPath = $this->___getRelativePath( $this->sBaseDirPath, $_sPath );
319+
return $_sBaseDirVar . ' . "' . $_sPath . '"';
305320
}
306321

307322
/**

source/PHPClassMapGenerator_Base.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -276,10 +276,8 @@ protected function _getPHPCode( $sFilePath ) {
276276
protected function _getDefinedObjectConstructs( $sPHPCode ) {
277277

278278
$_aConstructs = array(
279-
'classes' => array(),
280-
'interfaces' => array(),
281-
'traits' => array(),
282-
'namespaces' => array(),
279+
'classes' => array(), 'interfaces' => array(),
280+
'traits' => array(), 'namespaces' => array(),
283281
);
284282
$_aTokens = token_get_all( $sPHPCode );
285283
$_iCount = count( $_aTokens );
@@ -364,7 +362,7 @@ protected function _getParentClass( $sPHPCode ) {
364362
/**
365363
* Generates the heading comment from the given path or class name.
366364
*/
367-
protected function _getHeaderComment( $aFiles, $aOptions ) {
365+
protected function _getHeaderComment( array $aItems, array $aOptions ) {
368366

369367
if ( $aOptions[ 'header_class_path' ] && $aOptions[ 'header_class_name' ] ) {
370368
return $this->___getHeaderComment(
@@ -376,7 +374,7 @@ protected function _getHeaderComment( $aFiles, $aOptions ) {
376374

377375
if ( $aOptions[' header_class_name' ] ) {
378376
return $this->___getHeaderComment(
379-
isset( $aFiles[ $aOptions[ 'header_class_name' ] ] ) ? $aFiles[ $aOptions['header_class_name'] ][ 'path' ] : $aOptions[ 'header_class_path' ],
377+
isset( $aItems[ $aOptions[ 'header_class_name' ] ] ) ? $aItems[ $aOptions['header_class_name'] ][ 'path' ] : $aOptions[ 'header_class_path' ],
380378
$aOptions[ 'header_class_name' ],
381379
$aOptions[ 'header_type' ]
382380
);

test/test-css-files.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@
1919
]
2020
);
2121

22-
print_r( $_oGenerator->getItems() );
22+
print_r( $_oGenerator->get() );

test/test-js-files.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,4 @@
1818
]
1919
);
2020

21-
print_r( $_oGenerator->getItems() );
21+
print_r( $_oGenerator->get() );

0 commit comments

Comments
 (0)