@@ -51,7 +51,7 @@ class FileFinder extends StdObject
5151 * @var array
5252 */
5353 protected $ include = [
54- // 'file' => ['README.md'],
54+ // 'file' => ['README.md'], // file name
5555// 'ext' => [
5656 // 'js','css',
5757 // 'ttf','svg', 'eot', 'woff', 'woff2',
@@ -135,7 +135,7 @@ public function reset()
135135 return $ this ;
136136 }
137137
138- public function find ($ recursive = false , $ path = '' , $ pathPrefix = '' )
138+ public function find ($ recursive = true , $ path = '' , $ pathPrefix = '' )
139139 {
140140 return $ this ->findAll ($ recursive , $ path , $ pathPrefix );
141141 }
@@ -147,7 +147,7 @@ public function find($recursive = false, $path = '', $pathPrefix = '')
147147 * @return $this
148148 * @throws InvalidArgumentException
149149 */
150- public function findAll ($ recursive = false , $ path = '' , $ pathPrefix = '' )
150+ public function findAll ($ recursive = true , $ path = '' , $ pathPrefix = '' )
151151 {
152152 $ path = $ path ?: $ this ->sourcePath ;
153153 $ pathPrefix = $ pathPrefix ?: $ this ->pathPrefix ;
@@ -314,6 +314,220 @@ protected function doFilterDir($name /*, $dir*/)
314314 return true ;
315315 }
316316
317+ ////////////////////////////// helper method //////////////////////////////
318+
319+ /**
320+ * @param mixed $data
321+ * @param string $type
322+ * @return $this
323+ */
324+ public function include ($ data , $ type = 'dir ' )
325+ {
326+ switch ($ type ) {
327+ case 'dir ' :
328+ $ this ->includeDir ($ data );
329+ break ;
330+ case 'file ' :
331+ $ this ->includeFile ($ data );
332+ break ;
333+ case 'ext ' :
334+ $ this ->includeExt ($ data );
335+ break ;
336+ default :
337+ break ;
338+ }
339+
340+ return $ this ;
341+ }
342+
343+ /**
344+ * @param string|array $file
345+ * @return self
346+ */
347+ public function includeFile ($ file )
348+ {
349+ if (is_string ($ file )) {
350+ $ file = [$ file ];
351+ }
352+
353+ if (is_array ($ file )) {
354+ foreach ($ file as $ name ) {
355+ if (in_array ($ name , $ this ->include ['file ' ], true )) {
356+ continue ;
357+ }
358+
359+ $ this ->include ['file ' ][] = trim ($ name );
360+ }
361+ }
362+
363+ return $ this ;
364+ }
365+
366+ /**
367+ * @param string|array $ext
368+ * @return self
369+ */
370+ public function includeExt ($ ext )
371+ {
372+ if (is_string ($ ext )) {
373+ $ ext = [$ ext ];
374+ }
375+
376+ if (is_array ($ ext )) {
377+ foreach ($ ext as $ name ) {
378+ if (in_array ($ name , $ this ->include ['ext ' ], true )) {
379+ continue ;
380+ }
381+
382+ $ this ->include ['ext ' ][] = trim ($ name , '. ' );
383+ }
384+ }
385+
386+ return $ this ;
387+ }
388+
389+ /**
390+ * @param string|array $dir
391+ * @return self
392+ */
393+ public function includeDir ($ dir )
394+ {
395+ if (is_string ($ dir )) {
396+ $ dir = [$ dir ];
397+ }
398+
399+ if (is_array ($ dir )) {
400+ foreach ($ dir as $ name ) {
401+ if (in_array ($ name , $ this ->include ['dir ' ], true )) {
402+ continue ;
403+ }
404+
405+ $ this ->include ['dir ' ][] = trim ($ name );
406+ }
407+ }
408+
409+ return $ this ;
410+ }
411+
412+ /**
413+ * @return self
414+ */
415+ public function ignoreVCS ()
416+ {
417+ return $ this ->excludeDir (['.git ' , '.svn ' ])->excludeFile ('.gitignore ' );
418+ }
419+
420+ /**
421+ * @param mixed $data
422+ * @param string $type
423+ * @return $this
424+ */
425+ public function exclude ($ data , $ type = 'dir ' )
426+ {
427+ switch ($ type ) {
428+ case 'dir ' :
429+ $ this ->excludeDir ($ data );
430+ break ;
431+ case 'file ' :
432+ $ this ->excludeFile ($ data );
433+ break ;
434+ case 'ext ' :
435+ $ this ->excludeExt ($ data );
436+ break ;
437+ default :
438+ break ;
439+ }
440+
441+ return $ this ;
442+ }
443+
444+ /**
445+ * @param string|array $file
446+ * @return self
447+ */
448+ public function notName ($ file )
449+ {
450+ return $ this ->excludeFile ($ file );
451+ }
452+
453+ /**
454+ * @param string|array $file
455+ * @return self
456+ */
457+ public function notFile ($ file )
458+ {
459+ return $ this ->excludeFile ($ file );
460+ }
461+
462+ /**
463+ * @param string|array $file
464+ * @return self
465+ */
466+ public function excludeFile ($ file )
467+ {
468+ if (is_string ($ file )) {
469+ $ file = [$ file ];
470+ }
471+
472+ if (is_array ($ file )) {
473+ foreach ($ file as $ name ) {
474+ if (in_array ($ name , $ this ->exclude ['file ' ], true )) {
475+ continue ;
476+ }
477+
478+ $ this ->exclude ['file ' ][] = trim ($ name );
479+ }
480+ }
481+
482+ return $ this ;
483+ }
484+
485+ /**
486+ * @param string|array $ext
487+ * @return self
488+ */
489+ public function excludeExt ($ ext )
490+ {
491+ if (is_string ($ ext )) {
492+ $ ext = [$ ext ];
493+ }
494+
495+ if (is_array ($ ext )) {
496+ foreach ($ ext as $ name ) {
497+ if (in_array ($ name , $ this ->exclude ['ext ' ], true )) {
498+ continue ;
499+ }
500+
501+ $ this ->exclude ['ext ' ][] = trim ($ name );
502+ }
503+ }
504+
505+ return $ this ;
506+ }
507+
508+ /**
509+ * @param string|array $dir
510+ * @return self
511+ */
512+ public function excludeDir ($ dir )
513+ {
514+ if (is_string ($ dir )) {
515+ $ dir = [$ dir ];
516+ }
517+
518+ if (is_array ($ dir )) {
519+ foreach ($ dir as $ name ) {
520+ if (in_array ($ name , $ this ->exclude ['dir ' ], true )) {
521+ continue ;
522+ }
523+
524+ $ this ->exclude ['dir ' ][] = trim ($ name );
525+ }
526+ }
527+
528+ return $ this ;
529+ }
530+
317531 ////////////////////////////// getter/setter method //////////////////////////////
318532
319533 /**
@@ -324,6 +538,16 @@ public function getSourcePath()
324538 return $ this ->sourcePath ;
325539 }
326540
541+ /**
542+ * @param $sourcePath
543+ * @return self
544+ * @throws InvalidArgumentException
545+ */
546+ public function inDir ($ sourcePath )
547+ {
548+ return $ this ->setSourcePath ($ sourcePath );
549+ }
550+
327551 /**
328552 * @param string $sourcePath
329553 * @return $this
@@ -336,7 +560,7 @@ public function setSourcePath($sourcePath)
336560 throw new InvalidArgumentException ('The source path must be an existing dir path. Input: ' . $ sourcePath );
337561 }
338562
339- $ this ->sourcePath = $ sourcePath ;
563+ $ this ->sourcePath = realpath ( $ sourcePath) ;
340564 }
341565
342566 return $ this ;
0 commit comments