Skip to content

Commit

Permalink
Fixes for #10, #11, #12, #13, #14, #15 and #16. See CHANGELOG.md for …
Browse files Browse the repository at this point in the history
…details.
  • Loading branch information
devvoh committed Jul 22, 2017
1 parent f75b414 commit 59b7f07
Show file tree
Hide file tree
Showing 29 changed files with 632 additions and 128 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
# Parable PHP Framework Changelog

### 0.12.5

__Changes__
- `\Parable\ORM\Model` now has the possibility of `toArrayWithoutEmptyValues` and `exportToArrayWithoutEmptyValues`, and the default `toArray` and `exportToArray` now return empty values.
- `\Parable\ORM\Repository` now offers `buildAndSet()` and `buildOrSet()`. This fixes issue #10.
- All hook `->trigger(...)` calls now make use of constants so that hooking into them is somewhat more comfortable as well. This fixes issue #11.
- It's now possible to add `errorMode` to the database config so you can set the error mode. Use the values defined on `PDO` for this: `ERRMODE_SILENT`, `ERRMODE_WARNING` or `ERRMODE_EXCEPTION`. `ERRMODE_SILENT` is the default, so it's no change for you. Setting more punishing error modes should be a conscious choice. This fixes issue #12.
- Typo in `Init\Example` fixed, was still referencing `initLocations`, whereas that's `inits` now. This fixes #14.
- `\Parable\Http\Request` now no longer sets Method in its `__construct` method, but checks the `$_SERVER` values every time `getMethod` is called.
- `\Parable\Http\Response::setHttpCode()` now throws an Exception when an invalid code is set.

__Bugfixes__
- You can now set a different `homeDir` than `public`, for those who need a different location due to their hosting setup. This makes it possible to keep all app-related code outside of the end-user-accessible dirs. This fixes #13.
- `\Parable\Console` now supports `--option=value` style options, as well as arguments. Simple `$command->addArgument("name", $required = bool)` and you can use them. Arguments are *in-order*, but can be either preceeded or succeeded by options. This fixes #15.
- `\Parable\Http\Response` now returns using the same HTTP protocol it was requested with, since it asks `\Parable\Http\Request` what it should be. This fixes issue #16.

### 0.12.4

__Bugfixes__
Expand Down
3 changes: 3 additions & 0 deletions src/Console/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,9 @@ public function run()
throw new \Parable\Console\Exception('No valid commands found.');
}

$this->parameter->setArguments($command->getArguments());
$this->parameter->checkArguments();

$this->parameter->setOptions($command->getOptions());
$this->parameter->checkOptions();

Expand Down
32 changes: 29 additions & 3 deletions src/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ class Command
/** @var array */
protected $options = [];

/** @var array */
protected $arguments = [];

/** @var \Parable\Console\App */
protected $app;

Expand Down Expand Up @@ -112,6 +115,29 @@ public function getOptions()
return $this->options;
}

/**
* @param string $name
* @param bool $required
*
* @return $this
*/
public function addArgument($name, $required = false)
{
$this->arguments[] = [
'name' => $name,
'required' => $required,
];
return $this;
}

/**
* @return array
*/
public function getArguments()
{
return $this->arguments;
}

/**
* @param \Parable\Console\App $app
* @param \Parable\Console\Output $output
Expand Down Expand Up @@ -148,13 +174,13 @@ public function run()

/**
* @param \Parable\Console\Command $command
* @param array $arguments
* @param array $parameters
* @return mixed
*/
protected function runCommand(\Parable\Console\Command $command, array $arguments = [])
protected function runCommand(\Parable\Console\Command $command, array $parameters = [])
{
$parameter = new \Parable\Console\Parameter();
$parameter->setArguments($arguments);
$parameter->setParameters($parameters);

$command->prepare($this->app, $this->output, $this->input, $parameter);

Expand Down
156 changes: 121 additions & 35 deletions src/Console/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Parameter
const PARAMETER_EXISTS = '__parameter_exists__';

/** @var array */
protected $rawArguments = [];
protected $parameters = [];

/** @var string */
protected $scriptName;
Expand All @@ -17,57 +17,88 @@ class Parameter
protected $commandName;

/** @var array */
protected $arguments = [];
protected $parsedOptions = [];

/** @var array */
protected $rawArguments = [];

/** @var array */
protected $parsedArguments = [];

/** @var array */
protected $commandOptions = [];

/** @var array */
protected $options = [];
protected $commandArguments = [];

public function __construct()
{
$this->setArguments($_SERVER["argv"]);
$this->setParameters($_SERVER["argv"]);
}

/**
* @param array $arguments
* @param array $parameters
*
* @return $this
*/
public function setArguments(array $arguments)
public function setParameters(array $parameters)
{
$this->rawArguments = $arguments;
$this->parseArguments();
$this->parameters = $parameters;
$this->parseParameters();
return $this;
}

/**
* @return array
*/
public function getParameters()
{
return $this->parameters;
}

/**
* @return $this
*/
public function parseArguments()
public function parseParameters()
{
// Reset all previously gathered data
$this->reset();

// Extract the scriptName
$this->scriptName = array_shift($this->rawArguments);
$this->scriptName = array_shift($this->parameters);

// Extract the commandName
if (isset($this->rawArguments[0])
&& !empty($this->rawArguments[0])
&& strpos($this->rawArguments[0], '--') === false
if (isset($this->parameters[0])
&& !empty($this->parameters[0])
&& strpos($this->parameters[0], '--') === false
) {
$this->commandName = array_shift($this->rawArguments);
$this->commandName = array_shift($this->parameters);
}

$optionName = null;
foreach ($this->rawArguments as $key => $argument) {
if (substr($argument, 0, 2) == '--') {
$optionName = ltrim($argument, '-');
$this->arguments[$optionName] = static::PARAMETER_EXISTS;
} elseif ($optionName !== null) {
$this->arguments[$optionName] = $argument;
foreach ($this->parameters as $key => $parameter) {
// check if option
if (substr($parameter, 0, 2) == '--') {
$parameter = ltrim($parameter, '-');
// check if there's an '=' sign in there
$equalsPosition = strpos($parameter, '=');
if ($equalsPosition !== false) {
$optionName = substr($parameter, 0, $equalsPosition);
$optionValue = substr($parameter, $equalsPosition + 1);

$this->parsedOptions[$optionName] = $optionValue;
} else {
$this->parsedOptions[$parameter] = self::PARAMETER_EXISTS;
$optionName = $parameter;
}
} elseif ($optionName) {
$this->parsedOptions[$optionName] = $parameter;
$optionName = null;
} else {
$this->rawArguments[] = $parameter;
}
}

return $this;
}

Expand All @@ -94,7 +125,18 @@ public function getCommandName()
*/
public function setOptions(array $options)
{
$this->options = $options;
$this->commandOptions = $options;
return $this;
}

/**
* @param array $arguments
*
* @return $this
*/
public function setArguments(array $arguments)
{
$this->commandArguments = $arguments;
return $this;
}

Expand All @@ -106,20 +148,22 @@ public function setOptions(array $options)
*/
public function checkOptions()
{
foreach ($this->options as $option) {
foreach ($this->commandOptions as $option) {
// Check if required option is actually passed
if (isset($option['required'])
&& $option['required']
&& !array_key_exists($option['name'], $this->arguments)
&& !array_key_exists($option['name'], $this->parsedOptions)
) {
throw new \Parable\Console\Exception("Required option '--{$option['name']}' not provided.");
}

// Check if non-required but passed option requires a value
if (array_key_exists($option['name'], $this->arguments)
if (array_key_exists($option['name'], $this->parsedOptions)
&& isset($option['valueRequired'])
&& $option['valueRequired']
&& (!$this->arguments[$option['name']] || $this->arguments[$option['name']] == static::PARAMETER_EXISTS)
&& (!$this->parsedOptions[$option['name']]
|| $this->parsedOptions[$option['name']] == self::PARAMETER_EXISTS
)
) {
throw new \Parable\Console\Exception(
"Option '--{$option['name']}' requires a value, which is not provided."
Expand All @@ -130,12 +174,27 @@ public function checkOptions()
if (isset($option['defaultValue'])
&& $option['defaultValue']
&& (
!array_key_exists($option['name'], $this->arguments)
|| $this->arguments[$option['name']] == static::PARAMETER_EXISTS
!array_key_exists($option['name'], $this->parsedOptions)
|| $this->parsedOptions[$option['name']] == self::PARAMETER_EXISTS
)
) {
$this->arguments[$option['name']] = $option['defaultValue'];
$this->parsedOptions[$option['name']] = $option['defaultValue'];
}
}
}

public function checkArguments()
{
foreach ($this->commandArguments as $index => $argument) {
$key = $index + 1;
// Check if required argument is actually passed
if (isset($argument['required'])
&& $argument['required']
&& !array_key_exists($index, $this->rawArguments)
) {
throw new \Parable\Console\Exception("Required argument '{$key}:{$argument['name']}' not provided.");
}
$this->parsedArguments[$argument['name']] = $this->rawArguments[$index];
}
}

Expand All @@ -149,13 +208,13 @@ public function checkOptions()
*/
public function getOption($name)
{
if (!array_key_exists($name, $this->arguments)) {
if (!array_key_exists($name, $this->parsedOptions)) {
return null;
}
if ($this->arguments[$name] == static::PARAMETER_EXISTS) {
if ($this->parsedOptions[$name] == static::PARAMETER_EXISTS) {
return true;
}
return $this->arguments[$name];
return $this->parsedOptions[$name];
}

/**
Expand All @@ -164,20 +223,47 @@ public function getOption($name)
public function getOptions()
{
$returnArray = [];
foreach ($this->arguments as $key => $value) {
foreach ($this->parsedOptions as $key => $option) {
$returnArray[$key] = $this->getOption($key);
}
return $returnArray;
}

/**
* @param string $name
*
* @return mixed|null
*/
public function getArgument($name)
{
if (!array_key_exists($name, $this->parsedArguments)) {
return null;
}
return $this->parsedArguments[$name];
}

/**
* @return array
*/
public function getArguments()
{
$returnArray = [];
foreach ($this->parsedArguments as $key => $argument) {
$returnArray[$key] = $this->getArgument($key);
}
return $returnArray;
}

/**
* @return $this
*/
protected function reset()
{
$this->arguments = [];
$this->scriptName = null;
$this->commandName = null;
$this->scriptName = null;
$this->commandName = null;
$this->rawArguments = [];
$this->parsedArguments = [];
$this->parsedOptions = [];

return $this;
}
Expand Down
Loading

0 comments on commit 59b7f07

Please sign in to comment.