Skip to content

Commit

Permalink
First prototype
Browse files Browse the repository at this point in the history
  • Loading branch information
josefbenjac committed Oct 12, 2017
1 parent 0dcbcd6 commit c488162
Show file tree
Hide file tree
Showing 20 changed files with 667 additions and 0 deletions.
20 changes: 20 additions & 0 deletions .editorconfig
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
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# IDE
/.idea

# Composer
/vendor
/composer.lock
18 changes: 18 additions & 0 deletions README.md
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"
}
]
}
64 changes: 64 additions & 0 deletions composer.json
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"
}
]
}
}
}
9 changes: 9 additions & 0 deletions files/config.neon.dist
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:
95 changes: 95 additions & 0 deletions src/Config/FileConfig.php
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);
}

}
37 changes: 37 additions & 0 deletions src/Decoder/DecoderFactory.php
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);
}

}
14 changes: 14 additions & 0 deletions src/Decoder/IDecoder.php
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);

}
14 changes: 14 additions & 0 deletions src/Decoder/IDecoderFactory.php
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);

}
17 changes: 17 additions & 0 deletions src/Decoder/JsonDecoder.php
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);
}

}
19 changes: 19 additions & 0 deletions src/Decoder/NeonDecoder.php
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);
}

}
37 changes: 37 additions & 0 deletions src/Encoder/EncoderFactory.php
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);
}

}
16 changes: 16 additions & 0 deletions src/Encoder/IEncoder.php
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);

}
14 changes: 14 additions & 0 deletions src/Encoder/IEncoderFactory.php
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);

}
Loading

0 comments on commit c488162

Please sign in to comment.