Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Robin de Graaf committed Oct 31, 2017
2 parents 00a0c88 + e3d0a66 commit 19c7ea9
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 21 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
# Parable PHP Framework Changelog

### 0.12.13

__Changes__
- To ease development a little, try `make server` on a `make`-enabled OS (or run the php built-in webserver yourself with `php-server.php` passed as router script) and it will run (by default) on `http://localhost:5678`.
- `\Parable\Console\Parameter` and `Command` now understand that even arguments deserve default values. Third (optional) parameter added to `addArgument` that will allow for a default value.
- 5 whitespace issues fixed, based on feedback from StyleCI, using the PSR-2 preset. These were the only(!) style discrepancies found.

__Bugfixes__
- `Undefinet offset` fixed in `\Parable\Console\Parameter::checkArguments()`. Requesting arguments that weren't provided no longer results in a notice.

### 0.12.12

__Bugfixes__
Expand Down
4 changes: 4 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,7 @@ tests: dependencies
coverage: dependencies
rm -rf ./coverage
vendor/bin/phpunit --coverage-html ./coverage tests

server:
@echo Running on http://127.0.0.1:5678
php -t ../../.. -S 127.0.0.1:5678 php-server.php
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
[![Latest Stable Version](https://poser.pugx.org/devvoh/parable/v/stable)](https://packagist.org/packages/devvoh/parable)
[![Latest Unstable Version](https://poser.pugx.org/devvoh/parable/v/unstable)](https://packagist.org/packages/devvoh/parable)
[![License](https://poser.pugx.org/devvoh/parable/license)](https://packagist.org/packages/devvoh/parable)
[![StyleCI](https://styleci.io/repos/37279417/shield?branch=master)](https://styleci.io/repos/37279417)

Parable is a small and no-nonsense PHP framework, meant to be fast, readable and written in a way where it's not bogging
you down with unnecessary rules and limitations. Developed with the goal of building small web applications and REST APIs.
Expand Down
40 changes: 40 additions & 0 deletions php-server.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
$baseDir = __DIR__ . "/../../../public";
$baseDir = realpath($baseDir);
$path = $baseDir . $_SERVER["REQUEST_URI"];

if (!is_dir($path) && file_exists($path)) {
$fileinfo = new SplFileObject($path);

$mimetype = mime_content_type($path);

if ($mimetype == "text/plain") {
switch ($fileinfo->getExtension()) {
case "js":
case "json":
$mimetype = "application/javascript";
break;
case "css":
$mimetype = "text/css";
break;
}
}
header("Content-type: {$mimetype}");
echo trim(file_get_contents($path));
include($path);
return;
}

ini_set("date.timezone", "Europe/Amsterdam");

// We set this value so we can detect this further on
$_SERVER["PHP_SERVER"] = true;

// The built-in webserver does not set these in a way Parable expects it (htaccess redirect) so we do it ourselves
$_SERVER["SCRIPT_FILENAME"] = str_replace("php-server.php", "public/index.php", $_SERVER["SCRIPT_FILENAME"]);
$_SERVER["SCRIPT_NAME"] = str_replace(__DIR__, "", $_SERVER["SCRIPT_FILENAME"]);

// Normally the redirect is to index.php?url=REQUEST_URI, but obviously that's not the case here
$_GET["url"] = $_SERVER["REQUEST_URI"];

include($_SERVER["SCRIPT_NAME"]);
8 changes: 5 additions & 3 deletions src/Console/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,14 +118,16 @@ public function getOptions()
/**
* @param string $name
* @param bool $required
* @param mixed $defaultValue
*
* @return $this
*/
public function addArgument($name, $required = false)
public function addArgument($name, $required = false, $defaultValue = null)
{
$this->arguments[] = [
'name' => $name,
'required' => $required,
'name' => $name,
'required' => $required,
'defaultValue' => $defaultValue,
];
return $this;
}
Expand Down
25 changes: 23 additions & 2 deletions src/Console/Parameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ public function checkArguments()
) {
throw new \Parable\Console\Exception("Required argument '{$key}:{$argument['name']}' not provided.");
}
$this->parsedArguments[$argument['name']] = $this->rawArguments[$index];
if (array_key_exists($index, $this->rawArguments)) {
$this->parsedArguments[$argument['name']] = $this->rawArguments[$index];
}
}
}

Expand Down Expand Up @@ -237,11 +239,30 @@ public function getOptions()
public function getArgument($name)
{
if (!array_key_exists($name, $this->parsedArguments)) {
return null;
$commandArgument = $this->getCommandArgument($name);
if (!$commandArgument) {
return null;
}
return $commandArgument["defaultValue"];
}
return $this->parsedArguments[$name];
}

/**
* @param string $name
*
* @return array|null
*/
public function getCommandArgument($name)
{
foreach ($this->commandArguments as $argument) {
if ($name === $argument["name"]) {
return $argument;
}
}
return null;
}

/**
* @return array
*/
Expand Down
2 changes: 1 addition & 1 deletion src/Framework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class App
protected $database;

/** @var string */
protected $version = '0.12.12';
protected $version = '0.12.13';

public function __construct(
\Parable\Filesystem\Path $path,
Expand Down
1 change: 0 additions & 1 deletion src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,5 +202,4 @@ public function getBody()
}
return $this->body;
}

}
8 changes: 2 additions & 6 deletions src/Mail/Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,13 +243,9 @@ public function send()
$this->addHeader("From: {$from}");

$headers = array_merge($this->requiredHeaders, $this->headers);
$headers = implode("\r\n", $headers);

return $this->sendMail(
$to,
$this->subject,
$this->body,
implode("\r\n", $headers)
);
return $this->sendMail($to, $this->subject, $this->body, $headers);
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@
/**@var \Parable\Framework\Autoloader $autoloader */
$autoloader = \Parable\DI\Container::get(\Parable\Framework\Autoloader::class);
$autoloader->addLocation($path->getDir("tests/TestClasses"));
$autoloader->register();
$autoloader->register();
26 changes: 26 additions & 0 deletions tests/Components/Console/AppTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

class AppTest extends \Parable\Tests\Base
{
/** @var \Parable\Console\Parameter */
protected $parameter;

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

Expand All @@ -22,6 +25,9 @@ protected function setUp()

$_SERVER["argv"] = [];

$this->parameter = new \Parable\Console\Parameter();
\Parable\DI\Container::store($this->parameter);

$this->app = \Parable\DI\Container::get(\Parable\Console\App::class);

$this->command1 = new \Parable\Console\Command();
Expand Down Expand Up @@ -102,14 +108,34 @@ public function testSetDefaultCommandRunsDefaultCommand()
$this->assertSame('OK1', $this->app->run());
}

public function testPassCommandOnCommandLineRunsAppropriateCommand()
{
/** @var \Parable\Console\App $app */
$app = new \Parable\Console\App(new \Parable\Console\Output(), new \Parable\Console\Input(), $this->parameter);
$app->addCommand($this->command1);
$app->addCommand($this->command2);

// Same as calling 'php test.php test2'
$this->parameter->setParameters(['./test.php', 'test2']);

$this->assertSame("OK2", $app->run());

// Same as calling 'php test.php test2'
$this->parameter->setParameters(['./test.php', 'test1']);

$this->assertSame("OK1", $app->run());
}

/**
* @dataProvider dpTrueFalse
*
* @param $defaultCommandOnly
*/
public function testSetDefaultCommandWithCommandPassedRespectsDefaultOnlyCommand($defaultCommandOnly)
{
// Same as calling 'php test.php test2'
$_SERVER["argv"] = ['./test.php', 'test2'];

$app = \Parable\DI\Container::createAll(\Parable\Console\App::class);
$app->addCommand($this->command1);
$app->addCommand($this->command2);
Expand Down
12 changes: 7 additions & 5 deletions tests/Components/Console/CommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,20 @@ public function testAddOptionAndGetOptions()
public function testAddArgumentAndGetArguments()
{
$this->command->addArgument('option1', true);
$this->command->addArgument('option2', false);
$this->command->addArgument('option2', false, 12);

// Arguments aren't actually named properly until they've been parsed by Parameter
$this->assertSame(
[
[
'name' => 'option1',
'required' => true,
'name' => 'option1',
'required' => true,
'defaultValue' => null,
],
[
'name' => 'option2',
'required' => false,
'name' => 'option2',
'required' => false,
'defaultValue' => 12,
],
],
$this->command->getArguments()
Expand Down
21 changes: 21 additions & 0 deletions tests/Components/Console/ParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,27 @@ public function testRequiredArgumentThrowsException()
$this->parameter->checkArguments();
}

public function testGetArgumentReturnsAppropriateValues()
{
$this->parameter->setParameters([
'./test.php',
'command-to-run',
'arg1',
'arg2',
]);
$this->parameter->setArguments([
['name' => 'numero1', 'required' => true],
['name' => 'numero2', 'required' => true, 'defaultValue' => 12],
['name' => 'numero3', 'required' => false, 'defaultValue' => 24],
]);

$this->parameter->checkArguments();

$this->assertSame("arg1", $this->parameter->getArgument("numero1"));
$this->assertSame("arg2", $this->parameter->getArgument("numero2"));
$this->assertSame(24, $this->parameter->getArgument("numero3"));
}

public function testInvalidArgumentReturnsNull()
{
$this->assertNull($this->parameter->getArgument("totally not"));
Expand Down
1 change: 0 additions & 1 deletion tests/Components/Framework/DispatcherTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,4 @@ public function testDispatchRoute()

$this->assertSame($route, $this->internal->get('parable_dispatch_after'));
}

}
2 changes: 1 addition & 1 deletion tests/TestClasses/Command/Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@

class Test extends \Parable\Console\Command
{
}
}

0 comments on commit 19c7ea9

Please sign in to comment.