Skip to content

Commit befccd5

Browse files
committed
Initial Commit
- Added php-inc utility and docs Signed-off-by: RJ Garcia <[email protected]>
0 parents  commit befccd5

File tree

7 files changed

+198
-0
lines changed

7 files changed

+198
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor/
2+
/composer.lock
3+
/playground.php

LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Bighead Technology
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Php Inc
2+
3+
Php inc is a utility for generating a single include file of all the files that won't be auto loaded by a Psr compliant autoloader.
4+
5+
Any files that hold functions or constants that don't belong in classes will need to be included manually, but managing all of the `require_once` declarations can be quite a pain.
6+
7+
## Installation
8+
9+
Install via composer at `krak/php-inc`
10+
11+
## Usage
12+
13+
### CLI
14+
15+
The main interface is the `bin/php-inc` command which allows you to run generate a php source file with all of your project's require's.
16+
17+
```
18+
./bin/php-inc {path-to-source} > src/inc.php
19+
```
20+
21+
You may want to make sure your `inc.php` file (or whatever you decide to name it) is ignored by version control, and then generate the file when you build your project.
22+
23+
### Integrating in your own Console Application
24+
25+
If you are using Symfony or Laravel console application, you can just add the command to your own application with `Krak\PhpInc\Command\GenerateCommand`.

bin/php-inc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
$autoload_files = [
5+
__DIR__ . '/../vendor/autoload.php',
6+
__DIR__ . '/../../../autoload.php',
7+
];
8+
9+
foreach ($autoload_files as $file) {
10+
if (file_exists($file)) {
11+
require_once $file;
12+
}
13+
}
14+
15+
$app = new Symfony\Component\Console\Application();
16+
$app->add(new Krak\PhpInc\Command\GenerateCommand());
17+
$app->run();

composer.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"autoload": {
3+
"psr-4": {
4+
"Krak\\PhpInc\\": "src"
5+
},
6+
"files": ["src/php-inc.php"]
7+
},
8+
"require": {
9+
"nikic/iter": "^1.4",
10+
"symfony/console": "^3.2"
11+
}
12+
}

src/Command/GenerateCommand.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace Krak\PhpInc\Command;
4+
5+
use Krak\PhpInc,
6+
Symfony\Component\Console;
7+
8+
class GenerateCommand extends Console\Command\Command
9+
{
10+
protected function configure() {
11+
$this->setName('php-inc:generate')
12+
->setDescription('Generate a php inc file for source files')
13+
->addArgument(
14+
'path',
15+
Console\Input\InputArgument::REQUIRED,
16+
'The path of the source files to scan for'
17+
);
18+
}
19+
20+
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) {
21+
$match = PhpInc\andMatch([
22+
PhpInc\extMatch(['php']),
23+
PhpInc\lowerCaseMatch(),
24+
PhpInc\excludePathMatch('/.*\/Resources\/.*/'),
25+
]);
26+
$scan = PhpInc\scanSrc($match);
27+
$gen = PhpInc\genIncFile();
28+
$phpinc = PhpInc\phpInc($scan, $gen);
29+
$output->write($phpinc($input->getArgument('path')));
30+
}
31+
}

src/php-inc.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
namespace Krak\PhpInc;
4+
5+
use function iter\reduce,
6+
iter\filter;
7+
8+
/** only match lower case filenames */
9+
function lowerCaseMatch() {
10+
return function($finfo) {
11+
$first = $finfo->getFilename()[0];
12+
return ctype_lower($first);
13+
};
14+
}
15+
16+
function extMatch($ext) {
17+
if (!is_array($ext)) {
18+
$ext = [$ext];
19+
}
20+
21+
return function($finfo) use ($ext) {
22+
return in_array($finfo->getExtension(), $ext);
23+
};
24+
}
25+
26+
/** exclude certain paths, if the path matches the re, it will be excluded */
27+
function excludePathMatch($path_re) {
28+
return function($finfo) use ($path_re) {
29+
$res = preg_match($path_re, $finfo->getPathname());
30+
return !boolval($res);
31+
};
32+
}
33+
34+
function orMatch($matches) {
35+
return function($finfo) use ($matches) {
36+
foreach ($matches as $match) {
37+
if ($match($finfo)) {
38+
return true;
39+
}
40+
}
41+
42+
return false;
43+
};
44+
}
45+
46+
function andMatch($matches) {
47+
return function($finfo) use ($matches) {
48+
foreach ($matches as $match) {
49+
if (!$match($finfo)) {
50+
return false;
51+
}
52+
}
53+
54+
return true;
55+
};
56+
}
57+
58+
function scanSrc($match) {
59+
return function($path) use ($match) {
60+
$files = new \RecursiveDirectoryIterator($path);
61+
$files = new \RecursiveIteratorIterator($files);
62+
$files = filter(function($finfo) {
63+
return $finfo->isFile();
64+
}, $files);
65+
$files = filter(function($file) use ($match) {
66+
return $match($file);
67+
}, $files);
68+
return $files;
69+
};
70+
}
71+
72+
function genIncFile() {
73+
return function($base, $files) {
74+
$include_php = reduce(function($acc, $file) use ($base) {
75+
return $acc . sprintf(
76+
"require_once __DIR__ . '%s';\n",
77+
str_replace($base, '', $file->getPathname())
78+
);
79+
}, $files, '');
80+
81+
return sprintf("<?php\n\n%s", $include_php);
82+
};
83+
}
84+
85+
function phpInc($scan, $gen) {
86+
return function($path) use ($scan, $gen) {
87+
$path = realpath($path);
88+
return $gen($path, $scan($path));
89+
};
90+
}

0 commit comments

Comments
 (0)