This repository has been archived by the owner on Feb 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joan Fabrégat
committed
Oct 16, 2018
1 parent
f42df56
commit fb54610
Showing
20 changed files
with
961 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
/.idea | ||
/composer.lock | ||
/vendor | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2018 Joan Fabrégat / Code Inc. SAS | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,35 @@ | ||
# DirectoryClassesIterator | ||
Iterator to list all the PHP classes within a directory | ||
|
||
This package provides two directory iterators to list PHP classes : | ||
* [`DirectoryClassesIterator`](src/DirectoryClassesIterator.php) | ||
* [`RecursiveDirectoryClassesIterator`](src/RecursiveDirectoryClassesIterator.php) | ||
|
||
The iterator loads the `.php` files using [`include_once()`](http://php.net/manual/function.include-once.php) and uses the [`ReflectionClass`](http://php.net/manual/class.reflectionclass.php) API to detect classes found within the loaded files. | ||
|
||
## Usage | ||
```php | ||
<?php | ||
use CodeInc\DirectoryClassesIterator\RecursiveDirectoryClassesIterator; | ||
use CodeInc\DirectoryClassesIterator\DirectoryClassesIterator; | ||
|
||
// recursive listing | ||
$iterator = new RecursiveDirectoryClassesIterator('/path/to/libriaries'); | ||
|
||
// recursive listing with specific extensions | ||
$iterator = new RecursiveDirectoryClassesIterator('/path/to/libriaries', ['php', 'phtml', 'inc']); | ||
|
||
// non recursive listing | ||
$iterator = new DirectoryClassesIterator('/path/to/libriaries'); | ||
``` | ||
|
||
## Installation | ||
|
||
This library is available through [Packagist](https://packagist.org/packages/codeinc/directory-classes-iterator) and can be installed using [Composer](https://getcomposer.org/): | ||
|
||
```bash | ||
composer require codeinc/directory-classes-iterator | ||
``` | ||
|
||
## License | ||
|
||
The library is published under the MIT license (see [`LICENSE`](LICENSE) file). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{ | ||
"name": "codeinc/directory-classes-iterator", | ||
"version": "1.0.0", | ||
"description": "Iterators to list PHP classes within local files", | ||
"homepage": "https://github.com/CodeIncHQ/DirectoryClassesIterator", | ||
"type": "library", | ||
"license": "MIT", | ||
"require": { | ||
"php": ">=7.1" | ||
}, | ||
"autoload": { | ||
"psr-4": {"CodeInc\\DirectoryClassesIterator\\": "src"} | ||
}, | ||
"autoload-dev": { | ||
"psr-4": {"CodeInc\\DirectoryClassesIterator\\Tests\\": "tests"} | ||
}, | ||
"authors": [ | ||
{ | ||
"name": "Joan Fabrégat", | ||
"email": "[email protected]", | ||
"homepage": "https://www.codeinc.fr", | ||
"role": "developer" | ||
} | ||
], | ||
"config": { | ||
"preferred-install": { | ||
"*": "dist" | ||
} | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "^7" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
<?php | ||
// | ||
// +---------------------------------------------------------------------+ | ||
// | CODE INC. SOURCE CODE | | ||
// +---------------------------------------------------------------------+ | ||
// | Copyright (c) 2018 - Code Inc. SAS - All Rights Reserved. | | ||
// | Visit https://www.codeinc.fr for more information about licensing. | | ||
// +---------------------------------------------------------------------+ | ||
// | NOTICE: All information contained herein is, and remains the | | ||
// | property of Code Inc. SAS. The intellectual and technical concepts | | ||
// | contained herein are proprietary to Code Inc. SAS are protected by | | ||
// | trade secret or copyright law. Dissemination of this information or | | ||
// | reproduction of this material is strictly forbidden unless prior | | ||
// | written permission is obtained from Code Inc. SAS. | | ||
// +---------------------------------------------------------------------+ | ||
// | ||
// Author: Joan Fabrégat <[email protected]> | ||
// Date: 16/10/2018 | ||
// Project: AritasIntranet | ||
// | ||
declare(strict_types=1); | ||
namespace CodeInc\DirectoryClassesIterator; | ||
use CodeInc\DirectoryClassesIterator\Exceptions\NotADirectoryException; | ||
|
||
|
||
/** | ||
* Class AbstractDirectoryClassesIterator | ||
* | ||
* @package CodeInc\DirectoryClassesIterator | ||
* @author Joan Fabrégat <[email protected]> | ||
*/ | ||
abstract class AbstractDirectoryClassesIterator implements \IteratorAggregate | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $path; | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $extensions; | ||
|
||
/** | ||
* AbstractDirectoryClassesIterator constructor. | ||
* | ||
* @param string $path | ||
* @param array $extensions | ||
*/ | ||
public function __construct(string $path, array $extensions = ['php']) | ||
{ | ||
if (!is_dir($path)) { | ||
throw new NotADirectoryException($path); | ||
} | ||
$this->path = $path; | ||
$this->extensions = $extensions; | ||
} | ||
|
||
/** | ||
* Returns the directory iterator. | ||
* | ||
* @return \Iterator|\SplFileInfo[] | ||
*/ | ||
abstract protected function getDirectoryIterator():\Iterator; | ||
|
||
/** | ||
* Returns the directory path. | ||
* | ||
* @return string | ||
*/ | ||
public function getPath():string | ||
{ | ||
return $this->path; | ||
} | ||
|
||
/** | ||
* Returns the allowed classes extensions. | ||
* | ||
* @return string[] | ||
*/ | ||
public function getExtensions():array | ||
{ | ||
return $this->extensions; | ||
} | ||
|
||
/** | ||
* Sets the allowed classes extensions. | ||
* | ||
* @param string[] $extensions | ||
*/ | ||
public function setExtensions(array $extensions):void | ||
{ | ||
$this->extensions = $extensions; | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
* @return \ReflectionClass[]|\Generator | ||
* @throws \ReflectionException | ||
*/ | ||
public function getIterator():\Generator | ||
{ | ||
$loadedFiles = []; | ||
/** @var \SplFileInfo $entry */ | ||
foreach ($this->getDirectoryIterator() as $entry) { | ||
if (in_array($entry->getExtension(), $this->extensions)) { | ||
$loadedFiles[] = $entry->getPathname(); | ||
/** @noinspection PhpIncludeInspection */ | ||
include_once $entry->getPathname(); | ||
} | ||
} | ||
foreach (get_declared_classes() as $declaredClass) { | ||
$reflectionClass = new \ReflectionClass($declaredClass); | ||
if (in_array($reflectionClass->getFileName(), $loadedFiles)) { | ||
yield $reflectionClass; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
// | ||
// +---------------------------------------------------------------------+ | ||
// | CODE INC. SOURCE CODE | | ||
// +---------------------------------------------------------------------+ | ||
// | Copyright (c) 2018 - Code Inc. SAS - All Rights Reserved. | | ||
// | Visit https://www.codeinc.fr for more information about licensing. | | ||
// +---------------------------------------------------------------------+ | ||
// | NOTICE: All information contained herein is, and remains the | | ||
// | property of Code Inc. SAS. The intellectual and technical concepts | | ||
// | contained herein are proprietary to Code Inc. SAS are protected by | | ||
// | trade secret or copyright law. Dissemination of this information or | | ||
// | reproduction of this material is strictly forbidden unless prior | | ||
// | written permission is obtained from Code Inc. SAS. | | ||
// +---------------------------------------------------------------------+ | ||
// | ||
// Author: Joan Fabrégat <[email protected]> | ||
// Date: 16/10/2018 | ||
// Project: AritasIntranet | ||
// | ||
declare(strict_types=1); | ||
namespace CodeInc\DirectoryClassesIterator; | ||
|
||
|
||
/** | ||
* Class DirectoryClassesIterator | ||
* | ||
* @package CodeInc\DirectoryClassesIterator | ||
* @author Joan Fabrégat <[email protected]> | ||
*/ | ||
class DirectoryClassesIterator extends AbstractDirectoryClassesIterator | ||
{ | ||
/** | ||
* @inheritdoc | ||
* @return \Iterator|\SplFileInfo[] | ||
*/ | ||
protected function getDirectoryIterator():\Iterator | ||
{ | ||
return new \DirectoryIterator($this->getPath()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
// | ||
// +---------------------------------------------------------------------+ | ||
// | CODE INC. SOURCE CODE | | ||
// +---------------------------------------------------------------------+ | ||
// | Copyright (c) 2018 - Code Inc. SAS - All Rights Reserved. | | ||
// | Visit https://www.codeinc.fr for more information about licensing. | | ||
// +---------------------------------------------------------------------+ | ||
// | NOTICE: All information contained herein is, and remains the | | ||
// | property of Code Inc. SAS. The intellectual and technical concepts | | ||
// | contained herein are proprietary to Code Inc. SAS are protected by | | ||
// | trade secret or copyright law. Dissemination of this information or | | ||
// | reproduction of this material is strictly forbidden unless prior | | ||
// | written permission is obtained from Code Inc. SAS. | | ||
// +---------------------------------------------------------------------+ | ||
// | ||
// Author: Joan Fabrégat <[email protected]> | ||
// Date: 16/10/2018 | ||
// Project: DirectoryClassesIterator | ||
// | ||
declare(strict_types=1); | ||
namespace CodeInc\DirectoryClassesIterator\Exceptions; | ||
|
||
/** | ||
* Interface DirectoryClassesIteratorException | ||
* | ||
* @package CodeInc\DirectoryClassesIterator\Exceptions | ||
* @author Joan Fabrégat <[email protected]> | ||
*/ | ||
interface DirectoryClassesIteratorException | ||
{ | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
// | ||
// +---------------------------------------------------------------------+ | ||
// | CODE INC. SOURCE CODE | | ||
// +---------------------------------------------------------------------+ | ||
// | Copyright (c) 2018 - Code Inc. SAS - All Rights Reserved. | | ||
// | Visit https://www.codeinc.fr for more information about licensing. | | ||
// +---------------------------------------------------------------------+ | ||
// | NOTICE: All information contained herein is, and remains the | | ||
// | property of Code Inc. SAS. The intellectual and technical concepts | | ||
// | contained herein are proprietary to Code Inc. SAS are protected by | | ||
// | trade secret or copyright law. Dissemination of this information or | | ||
// | reproduction of this material is strictly forbidden unless prior | | ||
// | written permission is obtained from Code Inc. SAS. | | ||
// +---------------------------------------------------------------------+ | ||
// | ||
// Author: Joan Fabrégat <[email protected]> | ||
// Date: 16/10/2018 | ||
// Project: DirectoryClassesIterator | ||
// | ||
declare(strict_types=1); | ||
namespace CodeInc\DirectoryClassesIterator\Exceptions; | ||
|
||
use Throwable; | ||
|
||
|
||
/** | ||
* Class NotADirectoryException | ||
* | ||
* @package CodeInc\DirectoryClassesIterator\Exceptions | ||
* @author Joan Fabrégat <[email protected]> | ||
*/ | ||
class NotADirectoryException extends \RuntimeException implements DirectoryClassesIteratorException | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $path; | ||
|
||
/** | ||
* NotADirectoryException constructor. | ||
* | ||
* @param string $path | ||
* @param int $code | ||
* @param Throwable|null $previous | ||
*/ | ||
public function __construct(string $path, int $code = 0, Throwable $previous = null) | ||
{ | ||
$this->path = $path; | ||
parent::__construct( | ||
sprintf("The path '%s' is not a directory.", $path), | ||
$code, | ||
$previous | ||
); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getPath():string | ||
{ | ||
return $this->path; | ||
} | ||
} |
Oops, something went wrong.