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 Nov 17, 2017
2 parents 1b7e884 + 6c3ffa2 commit 5505d85
Show file tree
Hide file tree
Showing 26 changed files with 263 additions and 133 deletions.
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,19 @@
# Parable PHP Framework Changelog

### 0.12.14

__Changes__
- `/bin/vendor/parable`, the command-line tool, now shows the Parable version.
- `\Parable\Rights\Rights` has an improved `combine()` method and gained `getRightsFromNames()` and `getNamesFromRights()` methods, so it's easier to work with.
- `\Parable\Framework\App` has lost its dependency on `\Parable\Filesystem\Path`, since it became obsolete after recent changes.
- `\Parable\ORM\Query` now has `addValues()`, so you can add an array of values instead of having to do them one-by-one.
- `\Parable\ORM\Repository` now has `createInstanceForModelName()`, which is now what `Toolkit::getRepository` calls as well. Toolkit's `getRepository` is sticking around, since it's more useful in views.
- Small fixes to increase code base quality.

__Bugfixes__
- The `parable init-structure` command used a hard-coded vendor path, which could eventually cause problems.
- Fixed tests up so that all components have 100% code coverage without `Framework` touching all the things. Also improved and simplified some tests.

### 0.12.13

__Changes__
Expand Down
2 changes: 1 addition & 1 deletion parable
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ ini_set('display_errors', '1');
/** @var \Parable\Console\App $app */
$app = require_once(__DIR__ . '/src/Framework/Bootstrap.php');

$app->setName('Parable');
$app->setName("Parable " . \Parable\Framework\App::PARABLE_VERSION);

// Always add Help & Init
$app->addCommand(\Parable\DI\Container::get(\Parable\Console\Command\Help::class));
Expand Down
7 changes: 4 additions & 3 deletions src/Console/Command/Help.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ class Help extends \Parable\Console\Command

public function run()
{
$this->output->writeln("<yellow>{$this->app->getName()} help</yellow>");
$this->output->writeln('--------------------------------------------------');
$this->output->writeln('Available commands:');
$this->output->newline();
$this->output->writeln("<yellow>{$this->app->getName()}</yellow> " . str_repeat(" ", 36) . "command-line tool");
$this->output->writeln(str_repeat("-", 69));
$this->output->writeln('Help screen - available commands:');
$this->output->newline();

$longestName = 0;
Expand Down
13 changes: 3 additions & 10 deletions src/Framework/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

class App
{
const PARABLE_VERSION = '0.12.14';

const HOOK_HTTP_404 = "parable_http_404";
const HOOK_HTTP_200 = "parable_http_200";
const HOOK_RESPONSE_SEND = "parable_response_send";
Expand All @@ -17,10 +19,6 @@ class App
const HOOK_INIT_DATABASE_BEFORE = "parable_init_database_before";
const HOOK_INIT_DATABASE_AFTER = "parable_init_database_after";


/** @var \Parable\Filesystem\Path */
protected $path;

/** @var \Parable\Framework\Config */
protected $config;

Expand Down Expand Up @@ -48,11 +46,7 @@ class App
/** @var \Parable\ORM\Database */
protected $database;

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

public function __construct(
\Parable\Filesystem\Path $path,
\Parable\Framework\Config $config,
\Parable\Framework\Dispatcher $dispatcher,
\Parable\Framework\Toolkit $toolkit,
Expand All @@ -63,7 +57,6 @@ public function __construct(
\Parable\GetSet\Session $session,
\Parable\ORM\Database $database
) {
$this->path = $path;
$this->config = $config;
$this->dispatcher = $dispatcher;
$this->toolkit = $toolkit;
Expand Down Expand Up @@ -221,6 +214,6 @@ protected function dispatchRoute(\Parable\Routing\Route $route)
*/
public function getVersion()
{
return $this->version;
return self::PARABLE_VERSION;
}
}
39 changes: 24 additions & 15 deletions src/Framework/Command/InitStructure.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ class InitStructure extends \Parable\Console\Command
/** @var \Parable\Filesystem\Path */
protected $path;

/** @var string */
protected $vendor_path;

public function __construct(
\Parable\Filesystem\Path $path
) {
$this->addOption("homeDir", false, true, "public");
$this->path = $path;

$this->vendor_path = __DIR__ . "/../../..";
}

/**
Expand Down Expand Up @@ -86,62 +91,65 @@ public function run()

$this->output->write('Copying files: ');
copy(
$this->path->getDir("vendor/devvoh/parable/structure/.htaccess"),
$this->path->getDir("{$this->vendor_path}/structure/.htaccess"),
$this->path->getDir(".htaccess")
);
$this->output->write('.');
copy(
$this->path->getDir("vendor/devvoh/parable/structure/public/.htaccess"),
$this->path->getDir("{$this->vendor_path}/structure/public/.htaccess"),
$this->path->getDir("{$homeDir}/.htaccess")
);
$this->output->write('.');
copy(
$this->path->getDir("vendor/devvoh/parable/structure/public/index.php_struct"),
$this->path->getDir("{$homeDir}/index.php")
);

// For index.php, we do it a bit differently, since we need to alter the content
$content = file_get_contents($this->path->getDir("{$this->vendor_path}/structure/public/index.php_struct"));
$content = str_replace("###VENDOR_PATH###", $this->vendor_path, $content);
file_put_contents($this->path->getDir("{$homeDir}/index.php"), $content);
$this->output->write('.');

// And we continue copying files
copy(
$this->path->getDir("vendor/devvoh/parable/structure/app/Command/HelloWorld.php_struct"),
$this->path->getDir("{$this->vendor_path}/structure/app/Command/HelloWorld.php_struct"),
$this->path->getDir("app/Command/HelloWorld.php")
);
$this->output->write('.');
copy(
$this->path->getDir("vendor/devvoh/parable/structure/app/Config/App.php_struct"),
$this->path->getDir("{$this->vendor_path}/structure/app/Config/App.php_struct"),
$this->path->getDir("app/Config/App.php")
);
$this->output->write('.');
copy(
$this->path->getDir("vendor/devvoh/parable/structure/app/Config/Custom.php_struct"),
$this->path->getDir("{$this->vendor_path}/structure/app/Config/Custom.php_struct"),
$this->path->getDir("app/Config/Custom.php")
);
$this->output->write('.');
copy(
$this->path->getDir("vendor/devvoh/parable/structure/app/Controller/Home.php_struct"),
$this->path->getDir("{$this->vendor_path}/structure/app/Controller/Home.php_struct"),
$this->path->getDir("app/Controller/Home.php")
);
$this->output->write('.');
copy(
$this->path->getDir("vendor/devvoh/parable/structure/app/Init/Example.php_struct"),
$this->path->getDir("{$this->vendor_path}/structure/app/Init/Example.php_struct"),
$this->path->getDir("app/Init/Example.php")
);
$this->output->write('.');
copy(
$this->path->getDir("vendor/devvoh/parable/structure/app/Model/User.php_struct"),
$this->path->getDir("{$this->vendor_path}/structure/app/Model/User.php_struct"),
$this->path->getDir("app/Model/User.php")
);
$this->output->write('.');
copy(
$this->path->getDir("vendor/devvoh/parable/structure/app/Routing/App.php_struct"),
$this->path->getDir("{$this->vendor_path}/structure/app/Routing/App.php_struct"),
$this->path->getDir("app/Routing/App.php")
);
$this->output->write('.');
copy(
$this->path->getDir("vendor/devvoh/parable/structure/app/View/Home/index.phtml_struct"),
$this->path->getDir("{$this->vendor_path}/structure/app/View/Home/index.phtml_struct"),
$this->path->getDir("app/View/Home/index.phtml")
);
$this->output->write('.');
copy(
$this->path->getDir("vendor/devvoh/parable/structure/app/View/Home/test.phtml_struct"),
$this->path->getDir("{$this->vendor_path}/structure/app/View/Home/test.phtml_struct"),
$this->path->getDir("app/View/Home/test.phtml")
);
$this->output->write('.');
Expand All @@ -157,6 +165,7 @@ public function run()
$htaccess = str_replace("public/$1", "{$homeDir}/$1", $htaccess);
file_put_contents($this->path->getDir('.htaccess'), $htaccess);
}
$this->output->write('.');

$this->output->writeln(" <green>OK</green>");

Expand Down
9 changes: 1 addition & 8 deletions src/Framework/Toolkit.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,7 @@ public function __construct(
*/
public function getRepository($modelName)
{
/** @var \Parable\ORM\Model $model */
$model = \Parable\DI\Container::create($modelName);

/** @var \Parable\ORM\Repository $repository */
$repository = \Parable\DI\Container::create(\Parable\ORM\Repository::class);

$repository->setModel($model);
return $repository;
return \Parable\ORM\Repository::createInstanceForModelName($modelName);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/Http/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,15 +139,15 @@ public function getScheme()
return $_SERVER["REDIRECT_REQUEST_SCHEME"];
}
if (isset($_SERVER["HTTP_X_FORWARDED_PROTO"])) {
// Sometimes available in proxied requests
// Sometimes available in proxy-forwarded requests
return $_SERVER["HTTP_X_FORWARDED_PROTO"];
}
if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] !== "off") {
// Old-style but compatible with IIS
return "https";
}
if (isset($_SERVER['SERVER_PORT']) && $_SERVER['SERVER_PORT'] == 443) {
// Hacky but this is our last attempt, so why not
// This doesn't say much, but this is our last attempt, so why not try
return "https";
}
return "http";
Expand Down
7 changes: 4 additions & 3 deletions src/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

class Response
{
/** @var \Parable\Http\Request */
protected $request;

/** @var int */
protected $httpCode = 200;

Expand Down Expand Up @@ -84,14 +87,12 @@ class Response
/** @var bool */
protected $shouldTerminate = true;

/**
* By default we're going to set the Html Output type.
*/
public function __construct(
\Parable\Http\Request $request
) {
$this->request = $request;

// By default we're going to set the Html Output, but this can be switched at any time before sending..
$this->setOutput(new \Parable\Http\Output\Html);
}

Expand Down
2 changes: 1 addition & 1 deletion src/ORM/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ public function setInstance(\PDO $instance)
}

/**
* If an instance is available, quote/escape the message through PDO's quote function
* If an instance is available, quote/escape the message through PDOs quote function
*
* @param string $string
*
Expand Down
13 changes: 13 additions & 0 deletions src/ORM/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,19 @@ public function addValue($key, $value)
return $this;
}

/**
* @param array $values
*
* @return $this
*/
public function addValues(array $values)
{
foreach ($values as $key => $value) {
$this->addValue($key, $value);
}
return $this;
}

/**
* Sets the order for select queries
*
Expand Down
17 changes: 17 additions & 0 deletions src/ORM/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -296,4 +296,21 @@ protected function handleResult(array $result)
}
return $entities;
}

/**
* @param string $modelName
*
* @return \Parable\ORM\Repository
*/
public static function createInstanceForModelName($modelName)
{
$model = \Parable\DI\Container::create($modelName);

/** @var \Parable\ORM\Repository $repository */
$repository = \Parable\DI\Container::create(static::class);

$repository->setModel($model);

return $repository;
}
}
44 changes: 36 additions & 8 deletions src/Rights/Rights.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,44 @@ public function check($provided, $name)
*/
public function combine(array $rights)
{
$return = [];
$right_combined = str_repeat(0, count($this->rights));
foreach ($rights as $right) {
for ($i = 0; $i < strlen($right); $i++) {
if ($right[$i] == '1') {
$return[$i] = '1';
} elseif ($right[$i] !== 1 && !isset($return[$i])) {
$return[$i] = 0;
}
$right_combined |= $right;
}
return $right_combined;
}

/**
* Turn an array of right names (["read", "create"]) into a binary string of rights ("0011")
*
* @param string[] $names
*
* @return string
*/
public function getRightsFromNames(array $names)
{
$rights_string = "";
foreach ($this->getRights() as $right => $value) {
$rights_string .= in_array($right, $names) ? "1" : "0";
}
return strrev($rights_string);
}

/**
* Turn a binary string of rights ("0011") into an array of right names (["read", "create"])
*
* @param string $rights
*
* @return string[]
*/
public function getNamesFromRights($rights)
{
$names = [];
foreach ($this->rights as $name => $value) {
if ($this->check($rights, $name)) {
$names[] = $name;
}
}
return implode($return);
return $names;
}
}
2 changes: 1 addition & 1 deletion structure/app/View/Home/index.phtml_struct
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
font-size: 4.15rem;
}
.container aside {
font-size: 0.73rem;
font-size: 0.76rem;
margin-top: -0.8rem;
letter-spacing: 5px;
margin-left: 3px;
Expand Down
4 changes: 2 additions & 2 deletions structure/public/index.php_struct
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
ini_set('display_errors', '1');
$vendorDir = "###VENDOR_PATH###";

$bootstrapDirectory = __DIR__ . '/../vendor/devvoh/parable/src/Framework/Bootstrap.php';
$bootstrapDirectory = "{$vendorDir}/src/Framework/Bootstrap.php";
if (!file_exists($bootstrapDirectory)) {
die("<b>ERROR</b>: You need to run <pre style='display:inline-block;background:#e6e6e6;padding:2px 5px'>composer install</pre> before Parable will work.");
}
Expand Down
8 changes: 8 additions & 0 deletions tests/Base.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@

abstract class Base extends \PHPUnit\Framework\TestCase
{
protected function setUp()
{
parent::setUp();

// This key might be handy to have
$GLOBALS['_SESSION'] = [];
}

/**
* Set $value on $object->$propertyName, even if it's private
* or protected.
Expand Down
Loading

0 comments on commit 5505d85

Please sign in to comment.