Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
schnittstabil committed Apr 15, 2016
0 parents commit 6c0d1dd
Show file tree
Hide file tree
Showing 9 changed files with 369 additions and 0 deletions.
15 changes: 15 additions & 0 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
engines:
fixme:
enabled: true
phpcodesniffer:
enabled: true
phpmd:
enabled: true
ratings:
paths:
- "src/**/*"
- "**.php"
- "**.module"
- "**.inc"
exclude_paths:
- "tests/**/*"
7 changes: 7 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
tests/ export-ignore
.codeclimate.yml export-ignore
.gitattributes export-ignore
.gitignore export-ignore
.travis.yml export-ignore
license export-ignore
readme.md export-ignore
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build/
composer.lock
vendor
25 changes: 25 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
language: php

php:
- 5.6
- 7
- hhvm

sudo: false

install:
- composer selfupdate
- composer install
- composer global require schnittstabil/sugared-php_codesniffer fabpot/php-cs-fixer
- export PATH=$PATH:`composer global config bin-dir --absolute`

script:
- composer test

after_success:
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then vendor/bin/test-reporter; fi
- if [ "$TRAVIS_PHP_VERSION" == "5.6" ]; then vendor/bin/coveralls -v; fi

addons:
code_climate:
repo_token: 04d3c006e12446d2cea30d81289f839b002c0c31f3e5ef349a9f3df3552cd79f
63 changes: 63 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "schnittstabil/finder-by-config",
"type": "library",
"description": "Create Symfony\\Component\\Finder instances by configuration",
"license": "MIT",
"keywords": ["config", "finder"],
"authors": [
{
"name": "Michael Mayer",
"email": "michael@schnittstabil.de"
}
],
"autoload": {
"psr-4": { "Schnittstabil\\FinderByConfig\\": "src" }
},
"autoload-dev": {
"psr-4": { "Schnittstabil\\FinderByConfig\\": "tests" }
},
"require": {
"php": ">=5.6.0",
"symfony/finder": "^3.0",
"schnittstabil/get": "^2.0"
},
"require-dev": {
"codeclimate/php-test-reporter": "^0.3",
"satooshi/php-coveralls": "^1.0",
"schnittstabil/phpunit-starter": "^0.1",
"squizlabs/php_codesniffer": "^2.6"
},
"extra": {
"you/your-package": {
"simple": ["src", "tests", "composer.json"],
"extended": {
"in": ["."],
"name": ["*.php", "*.json"],
"notName": ["*Test.php"],
"size": ["> 1K"],
"exclude": ["build", "vendor"],
"sortByName": true,
"ignoreDotFiles": true,
"ignoreVCS": true,
"followLinks": false,
"ignoreUnreadableDirs": false
}
}
},
"scripts": {
"clean": "rm -rf build/",
"doc": [
"sugared-sami",
"phpmetrics --report-html build/phpmetrics/index.html src/"
],
"lint": [
"sugared-phpcs",
"php-cs-fixer fix --dry-run --diff src",
"php-cs-fixer fix --dry-run --diff tests"
],
"test": [
"schnittstabil-phpunit-starter",
"composer lint"
]
}
}
21 changes: 21 additions & 0 deletions license
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2016 Michael Mayer

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.
79 changes: 79 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# FinderByConfig [![Build Status](https://travis-ci.org/schnittstabil/finder-by-config.svg?branch=master)](https://travis-ci.org/schnittstabil/finder-by-config) [![Coverage Status](https://coveralls.io/repos/schnittstabil/finder-by-config/badge.svg?branch=master&service=github)](https://coveralls.io/github/schnittstabil/finder-by-config?branch=master) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/schnittstabil/finder-by-config/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/schnittstabil/finder-by-config/?branch=master) [![Code Climate](https://codeclimate.com/github/schnittstabil/finder-by-config/badges/gpa.svg)](https://codeclimate.com/github/schnittstabil/finder-by-config)

[![SensioLabsInsight](https://insight.sensiolabs.com/projects/201a1d53-11a6-4b14-9509-8c2f248f09d5/big.png)](https://insight.sensiolabs.com/projects/201a1d53-11a6-4b14-9509-8c2f248f09d5)

> Create Symfony\Component\Finder instances by configuration
## Install

```
$ composer require schnittstabil/finder-by-config
```

## Usage

```json
{
...
"require": {
"schnittstabil/finder-by-config": ...
},
"extra": {
"you/your-package": {
"simple": ["src", "tests", "composer.json"],
"extended": {
"in": ["."],
"name": ["*.php", "*.json"],
"notName": ["*Test.php"],
"size": ["> 1K"],
"exclude": ["build", "vendor"],
"ignoreDotFiles": true,
"ignoreVCS": true,
"followLinks": false,
"ignoreUnreadableDirs": false
}
}
}
}
```

```php
$config = json_decode(file_get_contents('composer.json'))->extra->{'you/your-package'};

$finder = \Schnittstabil\FinderByConfig\FinderByConfig::createFinder($config->simple);
$finder = \Schnittstabil\FinderByConfig\FinderByConfig::createFinder($config->extended);
```

## Supported configuration options

For details see the [Finder Component Documentation](https://symfony.com/doc/current/components/finder.html).

| Option | Type | Description |
| ---------------------- | ----------------- | ------------------------------------------------------------- |
| `directories` | `bool` | directories only |
| `files` | `bool` | files only |
| `depth` | `string|string[]` | directory depth, e.g. `'> 1'` |
| `date` | `string|string[]` | last modified [strtotime](http://php.net/manual/en/function.strtotime.php) parsable date, files must match e.g. `'>= 2005-10-15'` |
| `name` | `string|string[]` | regexp patterns, globs or simple strings files must match |
| `notName` | `string|string[]` | regexp patterns, globs or simple strings files must not match |
| `contains` | `string|string[]` | pattern (string or regexp) file contents must match |
| `notContains` | `string|string[]` | pattern (string or regexp) file contents must not match |
| `path` | `string|string[]` | regexp patterns, globs or simple strings files must match |
| `notPath` | `string|string[]` | regexp patterns, globs or simple strings files must not match |
| `size` | `string|string[]` | size files must match, e.g. `'<= 1Ki'` |
| `exclude` | `string|string[]` | directories to exclude |
| `ignoreDotFiles` | `bool` | exclude directories and files starting with a dot |
| `ignoreVCS` | `bool` | exclude version control directories |
| `addVCSPattern` | `string|string[]` | additional version control patterns |
| `sortByName` | `bool` | sorts by name |
| `sortByType` | `bool` | sorts by type |
| `sortByAccessedTime` | `bool` | sorts by the last accessed time |
| `sortByChangedTime` | `bool` | sorts by the last inode changed time |
| `sortByModifiedTime` | `bool` | sorts by the last modified time |
| `followLinks` | `bool` | follow symlinks |
| `ignoreUnreadableDirs` | `bool` | ignore unreadable directories |
| `in` | `string|string[]` | directory and file paths |

## License

MIT © [Michael Mayer](http://schnittstabil.de)
106 changes: 106 additions & 0 deletions src/FinderByConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
<?php

namespace Schnittstabil\FinderByConfig;

use function Schnittstabil\Get\getValue;
use Symfony\Component\Finder\Finder;

class FinderByConfig
{
protected function createFromArray(array $paths)
{
$finder = new Finder();
$files = [];
$dirs = [];

foreach ($paths as $path) {
if (is_file($path)) {
$files[] = $path;
continue;
}

$dirs[] = $path;
}

return $finder->append($files)->in($dirs);
}

protected function setWithoutValueOptions(Finder $finder, $config)
{
foreach ([
'directories',
'files',
'sortByName',
'sortByType',
'sortByAccessedTime',
'sortByChangedTime',
'sortByModifiedTime',
'followLinks',
] as $setter) {
if (filter_var(getValue($setter, $config), FILTER_VALIDATE_BOOLEAN)) {
$finder->$setter();
}
}
}

protected function setArrayOptions(Finder $finder, $config)
{
foreach ([
'depth',
'date',
'name',
'notName',
'contains',
'notContains',
'path',
'notPath',
'size',
'exclude',
'addVCSPattern',
] as $setter) {
foreach ((array) getValue($setter, $config, []) as $value) {
$finder->$setter($value);
}
}
}

protected function setStrictBoolOptions(Finder $finder, $config)
{
foreach ([
'ignoreDotFiles',
'ignoreVCS',
'ignoreUnreadableDirs',
] as $setter) {
$value = getValue($setter, $config);
if ($value !== null) {
$finder->$setter(filter_var($value, FILTER_VALIDATE_BOOLEAN));
}
}
}

protected function createFromConfig($config)
{
$finder = $this->createFromArray(getValue('in', $config));
$this->setWithoutValueOptions($finder, $config);
$this->setArrayOptions($finder, $config);
$this->setStrictBoolOptions($finder, $config);

return $finder;
}

public function __invoke($config)
{
if (getValue('in', $config) === null) {
return $this->createFromArray((array) $config);
}

return $this->createFromConfig($config);
}

public static function createFinder($config)
{
$finderByConfig = new static();

return $finderByConfig($config);
}
}
50 changes: 50 additions & 0 deletions tests/FinderByConfigTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

namespace Schnittstabil\FinderByConfig;

class FinderByConfigTest extends \PHPUnit_Framework_TestCase
{
protected static $config;

public static function setUpBeforeClass()
{
static::$config = json_decode(file_get_contents('composer.json'))->extra->{'you/your-package'};
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function testSimpleUsageShouldWork()
{
$finder = \Schnittstabil\FinderByConfig\FinderByConfig::createFinder(static::$config->simple);

$files = array_map(function ($file) {
return $file->getPathname();
}, array_values(iterator_to_array($finder)));

sort($files);

$this->assertSame([
'composer.json',
'src/FinderByConfig.php',
'tests/FinderByConfigTest.php',
], $files);
}

/**
* @SuppressWarnings(PHPMD.StaticAccess)
*/
public function testExtendedUsageShouldWork()
{
$finder = \Schnittstabil\FinderByConfig\FinderByConfig::createFinder(static::$config->extended);

$files = array_map(function ($file) {
return $file->getPathname();
}, array_values(iterator_to_array($finder)));

$this->assertSame([
'./composer.json',
'./src/FinderByConfig.php',
], $files);
}
}

0 comments on commit 6c0d1dd

Please sign in to comment.