-
-
Notifications
You must be signed in to change notification settings - Fork 4
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
1 parent
0dcbcd6
commit c488162
Showing
20 changed files
with
667 additions
and
0 deletions.
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,20 @@ | ||
# EditorConfig is awesome: http://EditorConfig.org | ||
|
||
# Top-most EditorConfig file | ||
root = true | ||
|
||
# Unix-style newlines with a newline ending every file | ||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
|
||
# JS / PHP | ||
[*.{js,php,phpt}] | ||
charset = utf-8 | ||
indent_style = tab | ||
indent_size = 4 | ||
|
||
# Composer | ||
[{composer.json}] | ||
indent_style = space | ||
indent_size = 2 |
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,6 @@ | ||
# IDE | ||
/.idea | ||
|
||
# Composer | ||
/vendor | ||
/composer.lock |
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,18 @@ | ||
Usage | ||
----- | ||
|
||
"scripts": { | ||
"post-install-cmd": [ | ||
"Contributte\\Neonizer\\NeonizerExtension::process" | ||
], | ||
"post-update-cmd": [ | ||
"Contributte\\Neonizer\\NeonizerExtension::process" | ||
] | ||
}, | ||
"extra": { | ||
"neonizer": [ | ||
{ | ||
"file": "files/config.neon" | ||
} | ||
] | ||
} |
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 @@ | ||
{ | ||
"name": "contributte/neonizer", | ||
"description": "Neonizer", | ||
"keywords": [ | ||
"nette" | ||
], | ||
"type": "library", | ||
"license": [ | ||
"MIT" | ||
], | ||
"homepage": "https://github.com/contributte/migrations", | ||
"authors": [ | ||
{ | ||
"name": "Milan Felix Sulc", | ||
"homepage": "https://f3l1x.io" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.6", | ||
"nette/neon": "^2.4" | ||
}, | ||
"require-dev": { | ||
"ninjify/qa": "~0.4.0", | ||
"ninjify/nunjuck": "~0.1.4", | ||
"composer/composer": "^1.5", | ||
"symfony/yaml": "^3.3" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"Contributte\\Neonizer\\": "src/" | ||
} | ||
}, | ||
"scripts": { | ||
"qa": [ | ||
"linter src tests", | ||
"codesniffer src tests" | ||
], | ||
"tester": [ | ||
"tester -s -p php --colors 1 -c tests/php-unix.ini tests/cases" | ||
], | ||
"tester-coverage": [ | ||
"tester -s -p php --colors 1 -c tests/php-unix.ini -d extension=xdebug.so --coverage ./coverage.xml --coverage-src ./src tests/cases" | ||
], | ||
"post-install-cmd": [ | ||
"Contributte\\Neonizer\\NeonizerExtension::process" | ||
], | ||
"post-update-cmd": [ | ||
"Contributte\\Neonizer\\NeonizerExtension::process" | ||
] | ||
}, | ||
"extra": { | ||
"neonizer": { | ||
"files": [ | ||
{ | ||
"dist-file": "files/config.neon.dist" | ||
}, | ||
{ | ||
"dist-file": "files/config.neon.dist", | ||
"file": "files/config.json" | ||
} | ||
] | ||
} | ||
} | ||
} |
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,9 @@ | ||
user: root | ||
password: "" | ||
database: | ||
name: dbname | ||
section: | ||
a: "Section A" | ||
b: "Section B" | ||
c: | ||
d: |
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,95 @@ | ||
<?php | ||
|
||
namespace Contributte\Neonizer\Config; | ||
|
||
use Contributte\Neonizer\Exception\InvalidArgumentException; | ||
use Contributte\Neonizer\Utils; | ||
|
||
class FileConfig | ||
{ | ||
|
||
/** @var string */ | ||
private $file; | ||
|
||
/** @var string */ | ||
private $distFile; | ||
|
||
/** @var string|NULL */ | ||
private $sourceType; | ||
|
||
/** @var string|NULL */ | ||
private $outputType; | ||
|
||
/** | ||
* @param mixed[] $config | ||
*/ | ||
public function __construct(array $config) | ||
{ | ||
// Dist file | ||
if (empty($config['dist-file'])) | ||
throw new InvalidArgumentException( | ||
'The dist-file name is required' | ||
); | ||
$this->distFile = $config['dist-file']; | ||
if (!is_file($this->distFile)) | ||
throw new InvalidArgumentException( | ||
sprintf( | ||
'The dist file "%s" does not exist. Check your dist-file config or create it.', | ||
$this->distFile | ||
) | ||
); | ||
|
||
// File | ||
if (!empty($config['file'])) | ||
$this->file = $config['file']; | ||
if (!$this->file) | ||
$this->file = Utils::removeDistExtensions($this->distFile); | ||
|
||
// Source & output type | ||
if (!$this->sourceType) | ||
$this->sourceType = Utils::detectFileType($this->distFile); | ||
if (!$this->outputType) | ||
$this->outputType = Utils::detectFileType($this->file); | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getFile() | ||
{ | ||
return $this->file; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getDistFile() | ||
{ | ||
return $this->distFile; | ||
} | ||
|
||
/** | ||
* @return string|NULL | ||
*/ | ||
public function getSourceType() | ||
{ | ||
return $this->sourceType; | ||
} | ||
|
||
/** | ||
* @return string|NULL | ||
*/ | ||
public function getOutputType() | ||
{ | ||
return $this->outputType; | ||
} | ||
|
||
/** | ||
* @return bool | ||
*/ | ||
public function isFileExist() | ||
{ | ||
return is_file($this->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,37 @@ | ||
<?php | ||
|
||
namespace Contributte\Neonizer\Decoder; | ||
|
||
use Contributte\Neonizer\Exception\InvalidArgumentException; | ||
|
||
class DecoderFactory implements IDecoderFactory | ||
{ | ||
|
||
/** @var string[] */ | ||
private $decodersMap = [ | ||
'json' => JsonDecoder::class, | ||
'neon' => NeonDecoder::class, | ||
]; | ||
|
||
/** @var IDecoder[] */ | ||
private $decoders = []; | ||
|
||
/** | ||
* @param string|NULL $type | ||
* @return IDecoder | ||
*/ | ||
public function create($type) | ||
{ | ||
if (isset($this->decoders[$type])) { | ||
return $this->decoders[$type]; | ||
} | ||
|
||
if (isset($this->decodersMap[$type])) { | ||
$this->decoders[$type] = new $this->decodersMap[$type]; | ||
return $this->create($type); | ||
} | ||
|
||
throw new InvalidArgumentException('Missing Decoder type ' . (string) $type); | ||
} | ||
|
||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace Contributte\Neonizer\Decoder; | ||
|
||
interface IDecoder | ||
{ | ||
|
||
/** | ||
* @param string $value | ||
* @return mixed[] | ||
*/ | ||
public function decode($value); | ||
|
||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace Contributte\Neonizer\Decoder; | ||
|
||
interface IDecoderFactory | ||
{ | ||
|
||
/** | ||
* @param string|NULL $type | ||
* @return IDecoder | ||
*/ | ||
public function create($type); | ||
|
||
} |
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,17 @@ | ||
<?php | ||
|
||
namespace Contributte\Neonizer\Decoder; | ||
|
||
class JsonDecoder implements IDecoder | ||
{ | ||
|
||
/** | ||
* @param string $value | ||
* @return mixed[] | ||
*/ | ||
public function decode($value) | ||
{ | ||
return json_decode($value, TRUE); | ||
} | ||
|
||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace Contributte\Neonizer\Decoder; | ||
|
||
use Nette\Neon\Neon; | ||
|
||
class NeonDecoder implements IDecoder | ||
{ | ||
|
||
/** | ||
* @param string $value | ||
* @return mixed[] | ||
*/ | ||
public function decode($value) | ||
{ | ||
return Neon::decode($value); | ||
} | ||
|
||
} |
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,37 @@ | ||
<?php | ||
|
||
namespace Contributte\Neonizer\Encoder; | ||
|
||
use Contributte\Neonizer\Exception\InvalidArgumentException; | ||
|
||
class EncoderFactory implements IEncoderFactory | ||
{ | ||
|
||
/** @var string[] */ | ||
private $encodersMap = [ | ||
'json' => JsonEncoder::class, | ||
'neon' => NeonEncoder::class, | ||
]; | ||
|
||
/** @var IEncoder[] */ | ||
private $encoders = []; | ||
|
||
/** | ||
* @param string|NULL $type | ||
* @return IEncoder | ||
*/ | ||
public function create($type) | ||
{ | ||
if (isset($this->encoders[$type])) { | ||
return $this->encoders[$type]; | ||
} | ||
|
||
if (isset($this->encodersMap[$type])) { | ||
$this->encoders[$type] = new $this->encodersMap[$type]; | ||
return $this->create($type); | ||
} | ||
|
||
throw new InvalidArgumentException('Missing Encoder type ' . (string) $type); | ||
} | ||
|
||
} |
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,16 @@ | ||
<?php | ||
|
||
namespace Contributte\Neonizer\Encoder; | ||
|
||
interface IEncoder | ||
{ | ||
|
||
const GENERATED_MESSAGE = 'This file is auto-generated by composer'; | ||
|
||
/** | ||
* @param mixed[] $value | ||
* @return string|NULL | ||
*/ | ||
public function encode($value); | ||
|
||
} |
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,14 @@ | ||
<?php | ||
|
||
namespace Contributte\Neonizer\Encoder; | ||
|
||
interface IEncoderFactory | ||
{ | ||
|
||
/** | ||
* @param string|NULL $type | ||
* @return IEncoder | ||
*/ | ||
public function create($type); | ||
|
||
} |
Oops, something went wrong.