Skip to content

Commit 868a333

Browse files
committed
Initial commit
0 parents  commit 868a333

19 files changed

+519
-0
lines changed

.docheader

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/**
2+
* @see https://github.com/open-code-modeling/php-code-generator-transformator for the canonical source repository
3+
* @copyright https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/COPYRIGHT.md
4+
* @license https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/LICENSE.md MIT License
5+
*/

.gitignore

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
.env
2+
app.env
3+
.phpunit.result.cache
4+
build/*
5+
vendor/
6+
composer.lock
7+
.idea
8+
.php_cs.cache

.php_cs

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
$config = new Prooph\CS\Config\Prooph();
4+
$config->getFinder()->in(__DIR__);
5+
6+
$cacheDir = getenv('TRAVIS') ? getenv('HOME') . '/.php-cs-fixer' : __DIR__;
7+
8+
$config->setCacheFile($cacheDir . '/.php_cs.cache');
9+
10+
return $config;

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Changelog

COPYRIGHT.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Copyright (c) 2020 Sandro Keil

LICENSE.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# MIT License
2+
3+
Copyright (c) 2020 Sandro Keil
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, 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,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# PHP Code Generator Transformator
2+
3+
Transformator for PHP Code Generator. Can be used standalone in a workflow or by a Cartridge.
4+
5+
## Installation
6+
7+
```bash
8+
$ composer require open-code-modeling/php-code-generator-transformator --dev
9+
```

composer.json

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
{
2+
"name": "open-code-modeling/php-code-generator-transformator",
3+
"description": "Open Code Modeling PHP Code Generator Transformator",
4+
"license": "MIT",
5+
"type": "library",
6+
"authors": [
7+
{
8+
"name": "Sandro Keil",
9+
"homepage": "https://sandro-keil.de",
10+
"role": "maintainer"
11+
}
12+
],
13+
"support": {
14+
"issues": "https://github.com/open-code-modeling/php-code-generator-transformator/issues",
15+
"source": "https://github.com/open-code-modeling/php-code-generator-transformator"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"OpenCodeModeling\\CodeGenerator\\Transformator\\": "src/"
20+
}
21+
},
22+
"autoload-dev": {
23+
"psr-4": {
24+
"OpenCodeModelingTest\\CodeGenerator\\Transformator\\": "tests/"
25+
}
26+
},
27+
"require": {
28+
"ext-dom": "*"
29+
},
30+
"require-dev": {
31+
"open-code-modeling/php-code-generator": "*",
32+
"phpspec/prophecy-phpunit": "^2.0",
33+
"phpstan/phpstan": "^0.12.33",
34+
"phpstan/phpstan-strict-rules": "^0.12.4",
35+
"phpunit/phpunit": "^9.2.6",
36+
"prooph/php-cs-fixer-config": "^0.3",
37+
"roave/security-advisories": "dev-master",
38+
"squizlabs/php_codesniffer": "^3.4"
39+
},
40+
"suggest": {
41+
"open-code-modeling/php-code-generator": "To use the static factory methods for Code Generator workflow configuration"
42+
},
43+
"minimum-stability": "dev",
44+
"prefer-stable": true,
45+
"scripts": {
46+
"check": [
47+
"@cs",
48+
"@docheader",
49+
"@test"
50+
],
51+
"docheader": "vendor/bin/docheader check src/ tests/",
52+
"cs": "php-cs-fixer fix src -v --diff --dry-run",
53+
"cs-fix": "php-cs-fixer fix src -v --diff",
54+
"test": "vendor/bin/phpunit",
55+
"analyse": "php vendor/bin/phpstan.phar analyse --no-interaction"
56+
},
57+
"config": {
58+
"sort-packages": true,
59+
"platform": {
60+
}
61+
},
62+
"archive": {
63+
"exclude": [
64+
".coveralls.yml",
65+
".travis.yml",
66+
"build",
67+
"phpunit.xml*",
68+
"tests"
69+
]
70+
}
71+
}

phpstan.neon.dist

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
parameters:
2+
level: 5
3+
paths:
4+
- src/
5+
excludes_analyse:
6+
- src/PhpCodeSniffer.php

phpunit.xml.dist

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
colors="true"
4+
cacheResult="true"
5+
stopOnFailure="false"
6+
convertErrorsToExceptions="true"
7+
convertNoticesToExceptions="true"
8+
convertWarningsToExceptions="true"
9+
backupGlobals="false"
10+
backupStaticAttributes="false"
11+
failOnRisky="true"
12+
failOnWarning="true"
13+
>
14+
<testsuite name="Open Code Modeling Code Generator Transformator - Test Suite">
15+
<directory suffix=".php">./tests</directory>
16+
</testsuite>
17+
18+
<filter>
19+
<whitelist>
20+
<directory>./src/</directory>
21+
</whitelist>
22+
</filter>
23+
</phpunit>

src/CodeListMiddleware.php

+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-generator-transformator for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\CodeGenerator\Transformator;
12+
13+
use OpenCodeModeling\CodeGenerator\Transformator\Exception\RuntimeException;
14+
use OpenCodeModeling\CodeGenerator\Workflow;
15+
16+
class CodeListMiddleware
17+
{
18+
/**
19+
* @var callable
20+
**/
21+
private $component;
22+
23+
public function __construct(callable $stringMiddleware)
24+
{
25+
$this->component = $stringMiddleware;
26+
}
27+
28+
public function __invoke(array $codeList): array
29+
{
30+
foreach ($codeList as $name => &$info) {
31+
if (! isset($info['code']) || ! isset($info['filename'])) {
32+
throw new RuntimeException(
33+
'Invalid code list info provided. Need array with key "code" and "filename".'
34+
);
35+
}
36+
37+
$info['code'] = ($this->component)($info['code']);
38+
}
39+
40+
return $codeList;
41+
}
42+
43+
public static function workflowComponentDescription(
44+
callable $component,
45+
string $inputCodeList,
46+
string $output
47+
): Workflow\Description {
48+
return new Workflow\ComponentDescriptionWithSlot(
49+
new self($component),
50+
$output,
51+
$inputCodeList
52+
);
53+
}
54+
}

src/CodeListToFiles.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-generator-transformator for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\CodeGenerator\Transformator;
12+
13+
use OpenCodeModeling\CodeGenerator\Transformator\Exception\RuntimeException;
14+
use OpenCodeModeling\CodeGenerator\Workflow;
15+
use OpenCodeModeling\CodeGenerator\Workflow\ComponentDescriptionWithInputSlotOnly;
16+
17+
class CodeListToFiles
18+
{
19+
/**
20+
* @var StringToFile
21+
**/
22+
private $stringToFile;
23+
24+
public function __construct(StringToFile $stringToFile)
25+
{
26+
$this->stringToFile = $stringToFile;
27+
}
28+
29+
public function __invoke(array $codeList): void
30+
{
31+
foreach ($codeList as $name => $info) {
32+
if (! isset($info['code']) || ! isset($info['filename'])) {
33+
throw new RuntimeException(
34+
'Invalid code list info provided. Need array with key "code" and "filename".'
35+
);
36+
}
37+
38+
($this->stringToFile)($info['code'], $info['filename']);
39+
}
40+
}
41+
42+
public static function workflowComponentDescription(
43+
StringToFile $stringToFile,
44+
string $inputCodeList
45+
): Workflow\Description {
46+
return new ComponentDescriptionWithInputSlotOnly(
47+
new self($stringToFile),
48+
$inputCodeList
49+
);
50+
}
51+
}

src/Exception/CouldNotReadFile.php

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-generator-transformator for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\CodeGenerator\Transformator\Exception;
12+
13+
class CouldNotReadFile extends RuntimeException
14+
{
15+
public static function withFile(string $file): self
16+
{
17+
return new self(
18+
\sprintf('Could not read file "%s".', $file)
19+
);
20+
}
21+
}

src/Exception/RuntimeException.php

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-generator-transformator for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\CodeGenerator\Transformator\Exception;
12+
13+
use RuntimeException as BaseRuntimeException;
14+
15+
class RuntimeException extends BaseRuntimeException implements TransformatorException
16+
{
17+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-generator-transformator for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\CodeGenerator\Transformator\Exception;
12+
13+
/**
14+
* Base exception interface
15+
*
16+
* All exceptions must implements this exception to catch exceptions of this library
17+
*/
18+
interface TransformatorException
19+
{
20+
}

src/FileToDomDocument.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
/**
4+
* @see https://github.com/open-code-modeling/php-code-generator-transformator for the canonical source repository
5+
* @copyright https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/COPYRIGHT.md
6+
* @license https://github.com/open-code-modeling/php-code-generator-transformator/blob/master/LICENSE.md MIT License
7+
*/
8+
9+
declare(strict_types=1);
10+
11+
namespace OpenCodeModeling\CodeGenerator\Transformator;
12+
13+
use DOMDocument;
14+
use OpenCodeModeling\CodeGenerator\Transformator\Exception\RuntimeException;
15+
use OpenCodeModeling\CodeGenerator\Workflow;
16+
17+
/**
18+
* Loads a XML file with DOM. This is needed for xsl:include for instance.
19+
*/
20+
final class FileToDomDocument
21+
{
22+
public function __invoke(string $file): DOMDocument
23+
{
24+
// TODO file check / error handling
25+
$dom = new DOMDocument();
26+
27+
if (false === $dom->load($file)) {
28+
throw new RuntimeException(
29+
\sprintf('Error loading XML file "%s"', $file)
30+
);
31+
}
32+
33+
return $dom;
34+
}
35+
36+
public static function workflowComponentDescription(
37+
string $inputFile,
38+
string $output
39+
): Workflow\Description {
40+
return new Workflow\ComponentDescriptionWithSlot(
41+
new self(),
42+
$output,
43+
$inputFile
44+
);
45+
}
46+
}

0 commit comments

Comments
 (0)