From c196abc793fa45968c60f25d116a59b77dd3e55d Mon Sep 17 00:00:00 2001 From: Robin de Graaf Date: Mon, 17 Apr 2017 11:38:59 +0000 Subject: [PATCH] 0.10.0 breaks backwards compatibility but streamlines and improves many components. See CHANGELOG.md. --- CHANGELOG.md | 30 +++++ composer.json | 2 +- parable | 31 +++++ src/Auth/Exception.php | 7 - src/Console/App.php | 2 + src/Console/Command.php | 14 ++ src/Console/Command/Help.php | 47 +++++++ src/Console/Command/Init.php | 142 +++++++++++++++++++++ src/DI/Container.php | 8 +- src/{Events => Event}/Dock.php | 2 +- src/{Events => Event}/Hook.php | 2 +- src/Framework/App.php | 8 +- src/{Auth => Framework}/Authentication.php | 8 +- src/Framework/Dispatcher.php | 6 +- src/Framework/Log.php | 6 +- src/Framework/Mail/Mailer.php | 90 +++++++++++++ src/Framework/Mail/TemplateVariables.php | 12 ++ src/Framework/{Routes => Routing}/Base.php | 2 +- src/Framework/View.php | 51 ++++---- src/Http/Response.php | 4 +- src/Http/Url.php | 2 +- src/Http/Values/GetSet.php | 44 ++++++- src/Mail/Mailer.php | 136 ++++++++++++++------ src/Routing/Router.php | 2 +- src/{Auth => Tool}/Rights.php | 2 +- src/parable | 114 ----------------- structure/app/Command/HelloWorld.php | 17 +++ structure/app/Config/App.php | 5 + structure/app/Init/Example.php | 2 +- structure/app/{Routes => Routing}/App.php | 4 +- structure/app/View/Home/index.phtml | 2 +- 31 files changed, 588 insertions(+), 216 deletions(-) create mode 100644 parable delete mode 100644 src/Auth/Exception.php create mode 100644 src/Console/Command/Help.php create mode 100644 src/Console/Command/Init.php rename src/{Events => Event}/Dock.php (98%) rename src/{Events => Event}/Hook.php (98%) rename src/{Auth => Framework}/Authentication.php (94%) create mode 100644 src/Framework/Mail/Mailer.php create mode 100644 src/Framework/Mail/TemplateVariables.php rename src/Framework/{Routes => Routing}/Base.php (91%) rename src/{Auth => Tool}/Rights.php (98%) delete mode 100755 src/parable create mode 100644 structure/app/Command/HelloWorld.php rename structure/app/{Routes => Routing}/App.php (94%) diff --git a/CHANGELOG.md b/CHANGELOG.md index fac63f66..c6f62bf4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,35 @@ # Parable PHP Framework Changelog +### 0.10.0 + +Note: Breaks backwards compatibility in some instances, and based on your implementation might require small tweaks or none whatsoever. By reading the following +changelog carefully it should be trivial to fix any incompatibilities. + +__Changes__ +- `\Parable\Auth\Authentication` has been moved to `\Parable\Framework\Authentication`, since it cannot function without packages from `Framework` and `Http`. +- `\Parable\Auth\Rights` has been moved to `\Parable\Tool\Rights`, since by itself it does not warrent an `Auth` namespace. +- `\Parable\Mail\Mailer` has also been improved: + - The main class been simplified and all template logic has been moved to `\Parable\Framework\Mail\Mailer`, since those require external logic. + - `\Parable\Framework\Mail\Mailer` now uses a `GetSet` implementation (`\Parable\Mail\TemplateVariables`) for its template variables and requires a full path for `loadTemplate` calls. + - `\Parable\Mail\Mailer` has gained `requiredHeader`, to distinguish between headers it wants to enforce itself and ones set by the dev. + - `\Parable\Mail\Mailer` has gained `getSubject()`, `getBody()`, `getHeaders()`, `getRequiredHeaders()`. + - `\Parable\Mail\Mailer` has gained `resetMailData()`, `resetRecipients()`, `resetSender()`, `reset()` (resets all but sender). +- All classes previously using `Routes` as a namespace now use `Routing` to make the namespace more consistently singular. +- `\Parable\Console` has once again gotten some love: + - `\Parable\Console\Command` namespace has been added, with 2 commands: `Help` and `Init`. + - It's now possible to add your own commands to the `parable` command, as shown in structure's `\Config\App` (`$config->get('console.commands')`). + - `\Parable\Console\App` is now always available to Commands, by setting it through `setApp()`. + - `parable.php` has been moved up one directory. +- `\Parable\Events` namespace has been changed to `\Parable\Event` for consistency. +- `\Parable\Http\Values\GetSet` has gained the following methods: `getAllAndReset()`, `getAndRemove($key)`, `reset()` and `count()`. + +__Bugfixes__ +- `\Parable\Framework\View` list of `@property` tags updated, since some classes were missing. + +__Miscellaneous__ +- Where logical and readable, double-quote {}-style concatenation added. +- Comments improved in places. + ### 0.9.8 __Changes__ diff --git a/composer.json b/composer.json index bccfe8a6..8dcfda26 100644 --- a/composer.json +++ b/composer.json @@ -20,5 +20,5 @@ "Parable\\": "src/" } }, - "bin": ["src/parable"] + "bin": ["parable"] } diff --git a/parable b/parable new file mode 100644 index 00000000..2d648bb1 --- /dev/null +++ b/parable @@ -0,0 +1,31 @@ +#!/usr/bin/env php +setBasedir(BASEDIR); + +$app->setName('Parable'); + +// Always add Help & Init +$app->addCommand(\Parable\DI\Container::get(\Parable\Console\Command\Help::class)); +$app->addCommand(\Parable\DI\Container::get(\Parable\Console\Command\Init::class)); + +// Attempt to load commands set by the user +if (file_exists($path->getDir('app'))) { + /** @var \Parable\Framework\Config $config */ + $config = \Parable\DI\Container::get(\Parable\Framework\Config::class)->load(); + if ($config->get('console.commands')) { + // We don't try/catch because the dev shouldn't add non-existing classes. + foreach ($config->get('console.commands') as $commandClassName) { + $app->addCommand(\Parable\DI\Container::get($commandClassName)); + } + } +} + +$app->setDefaultCommand('help'); +$app->run(); diff --git a/src/Auth/Exception.php b/src/Auth/Exception.php deleted file mode 100644 index b2f3787b..00000000 --- a/src/Auth/Exception.php +++ /dev/null @@ -1,7 +0,0 @@ -setApp($this); + $this->parameter->setOptions($command->getOptions()); $this->parameter->checkOptions(); diff --git a/src/Console/Command.php b/src/Console/Command.php index afd1ec61..9a1f70a4 100644 --- a/src/Console/Command.php +++ b/src/Console/Command.php @@ -16,6 +16,20 @@ class Command /** @var array */ protected $options = []; + /** @var \Parable\Console\App */ + protected $app; + + /** + * @param \Parable\Console\App $app + * + * @return $this + */ + public function setApp(\Parable\Console\App $app) + { + $this->app = $app; + return $this; + } + /** * @param string $name * diff --git a/src/Console/Command/Help.php b/src/Console/Command/Help.php new file mode 100644 index 00000000..7521ac3d --- /dev/null +++ b/src/Console/Command/Help.php @@ -0,0 +1,47 @@ +writeln("{$this->app->getName()} help"); + $output->writeln('--------------------------------------------------'); + $output->writeln('Available commands:'); + $output->newline(); + + $longestName = 0; + foreach ($this->app->getCommands() as $command) { + $strlen = strlen($command->getName()); + if ($strlen > $longestName) { + $longestName = $strlen; + } + } + + foreach ($this->app->getCommands() as $command) { + $name = $command->getName(); + $output->write(str_pad(" {$name}", $longestName + 22, ' ', STR_PAD_RIGHT)); + $output->write("{$command->getDescription()}"); + $output->newline(); + } + $output->newline(); + return $this; + } +} diff --git a/src/Console/Command/Init.php b/src/Console/Command/Init.php new file mode 100644 index 00000000..dc6666ed --- /dev/null +++ b/src/Console/Command/Init.php @@ -0,0 +1,142 @@ +path = $path; + } + + /** + * @param \Parable\Console\Output $output + * @param \Parable\Console\Input $input + * @param \Parable\Console\Parameter $parameter + * + * @return $this + */ + public function run( + \Parable\Console\Output $output, + \Parable\Console\Input $input, + \Parable\Console\Parameter $parameter + ) { + $output->writeln([ + "Parable initialization script", + "-----------------------------------", + "This script will initialize Parable's structure.", + "", + "WARNING", + "This will overwrite existing files without notice!", + "", + ]); + + for (;;) { + $output->write("Do you want to continue? [y/N] "); + if ($input->getYesNo(false)) { + break; + } else { + $output->writeln(["", "You chose not to continue.", ""]); + return $this; + } + } + + /** @var \Parable\Filesystem\Path $path */ + $output->newline(); + $output->write('Creating folder structure: '); + + $dirs = [ + 'app', + 'app/Command', + 'app/Config', + 'app/Controller', + 'app/Init', + 'app/Model', + 'app/Routing', + 'app/View', + 'app/View/Home', + 'public', + ]; + + foreach ($dirs as $dir) { + if (!file_exists($this->path->getDir($dir))) { + mkdir($this->path->getDir($dir)); + } + $output->write('.'); + } + + $output->writeln(" OK"); + + $output->write('Copying files: '); + copy( + $this->path->getDir('vendor/devvoh/parable/structure/.htaccess'), + $this->path->getDir('.htaccess') + ); + $output->write('.'); + copy( + $this->path->getDir('vendor/devvoh/parable/structure/public/.htaccess'), + $this->path->getDir('public/.htaccess') + ); + $output->write('.'); + copy( + $this->path->getDir('vendor/devvoh/parable/structure/public/index.php'), + $this->path->getDir('public/index.php') + ); + $output->write('.'); + copy( + $this->path->getDir('vendor/devvoh/parable/structure/app/Command/HelloWorld.php'), + $this->path->getDir('app/Command/HelloWorld.php') + ); + $output->write('.'); + copy( + $this->path->getDir('vendor/devvoh/parable/structure/app/Config/App.php'), + $this->path->getDir('app/Config/App.php') + ); + $output->write('.'); + copy( + $this->path->getDir('vendor/devvoh/parable/structure/app/Controller/Home.php'), + $this->path->getDir('app/Controller/Home.php') + ); + $output->write('.'); + copy( + $this->path->getDir('vendor/devvoh/parable/structure/app/Init/Example.php'), + $this->path->getDir('app/Init/Example.php') + ); + $output->write('.'); + copy( + $this->path->getDir('vendor/devvoh/parable/structure/app/Model/User.php'), + $this->path->getDir('app/Model/User.php') + ); + $output->write('.'); + copy( + $this->path->getDir('vendor/devvoh/parable/structure/app/Routing/App.php'), + $this->path->getDir('app/Routing/App.php') + ); + $output->write('.'); + copy( + $this->path->getDir('vendor/devvoh/parable/structure/app/View/Home/index.phtml'), + $this->path->getDir('app/View/Home/index.phtml') + ); + $output->write('.'); + copy( + $this->path->getDir('vendor/devvoh/parable/structure/app/View/Home/test.phtml'), + $this->path->getDir('app/View/Home/test.phtml') + ); + $output->write('.'); + + $output->writeln(" OK"); + + $output->writeln(["", "Completed!", ""]); + return $this; + } +} diff --git a/src/DI/Container.php b/src/DI/Container.php index 227c0cfc..79ada183 100644 --- a/src/DI/Container.php +++ b/src/DI/Container.php @@ -31,8 +31,8 @@ public static function get($className, $parentClassName = '') && isset(self::$relations[$parentClassName]) && isset(self::$relations[$parentClassName][$className]) ) { - $message = 'Cyclical dependency found: ' . $className . ' depends on ' . $parentClassName; - $message .= ' but is itself a dependency of ' . $parentClassName . '.'; + $message = "Cyclical dependency found: {$className} depends on {$parentClassName}"; + $message .= " but is itself a dependency of {$parentClassName}."; throw new \Parable\DI\Exception($message); } @@ -55,9 +55,9 @@ public static function get($className, $parentClassName = '') public static function create($className, $parentClassName = '') { if (!class_exists($className)) { - $message = 'Could not create instance of "' . $className . '"'; + $message = "Could not create instance of '{$className}'"; if ($parentClassName) { - $message .= ', required by "' . $parentClassName . '"'; + $message .= ", required by '{$parentClassName}'"; } throw new \Parable\DI\Exception($message); } diff --git a/src/Events/Dock.php b/src/Event/Dock.php similarity index 98% rename from src/Events/Dock.php rename to src/Event/Dock.php index 73d92350..803cca21 100644 --- a/src/Events/Dock.php +++ b/src/Event/Dock.php @@ -1,6 +1,6 @@ get() as $name => $route) { + foreach (\Parable\DI\Container::get(\Routing\App::class)->get() as $name => $route) { $this->router->addRoute($name, $route); } return $this; diff --git a/src/Auth/Authentication.php b/src/Framework/Authentication.php similarity index 94% rename from src/Auth/Authentication.php rename to src/Framework/Authentication.php index c1f42bba..84fc4b54 100644 --- a/src/Auth/Authentication.php +++ b/src/Framework/Authentication.php @@ -1,6 +1,6 @@ userClassName . ' could not be instantiated.'); + throw new \Parable\Framework\Exception($this->userClassName . ' could not be instantiated.'); } $this->userClassName = $className; @@ -172,7 +172,7 @@ public function setUserClassName($className) public function setUser($user) { if (!($user instanceof $this->userClassName)) { - throw new \Parable\Auth\Exception('Invalid object provided, type ' . $this->userClassName . ' required.'); + throw new \Parable\Framework\Exception("Invalid object provided, type {$this->userClassName} required."); } $this->user = $user; return $this; diff --git a/src/Framework/Dispatcher.php b/src/Framework/Dispatcher.php index 7bfc4493..ed321904 100644 --- a/src/Framework/Dispatcher.php +++ b/src/Framework/Dispatcher.php @@ -4,7 +4,7 @@ class Dispatcher { - /** @var \Parable\Events\Hook */ + /** @var \Parable\Event\Hook */ protected $hook; /** @var \Parable\Filesystem\Path */ @@ -17,7 +17,7 @@ class Dispatcher protected $response; public function __construct( - \Parable\Events\Hook $hook, + \Parable\Event\Hook $hook, \Parable\Filesystem\Path $path, \Parable\Framework\View $view, \Parable\Http\Response $response @@ -67,7 +67,7 @@ public function dispatch(\Parable\Routing\Route $route) $controllerName = str_replace('\\', '/', $reflection->getName()); $controllerName = str_replace('Controller/', '', $controllerName); $templateFile = $this->path->getDir( - 'app/View/' . $controllerName . '/' . $route->action . '.phtml' + "app/View/{$controllerName}/{$route->action}.phtml" ); } } diff --git a/src/Framework/Log.php b/src/Framework/Log.php index 22bfa2f5..37395f99 100644 --- a/src/Framework/Log.php +++ b/src/Framework/Log.php @@ -86,13 +86,13 @@ public function getPath() // Create directory $created = @mkdir($this->path, $this->getMode(), true); if (!$created) { - throw new \Parable\Framework\Exception('Could not create log directory: ' . $this->path); + throw new \Parable\Framework\Exception("Could not create log directory: {$this->path}"); } } if (!is_writable($this->path)) { $chmod = chmod($this->path, $this->getMode()); if (!$chmod) { - throw new \Parable\Framework\Exception('Log directory is not writable: ' . $this->path); + throw new \Parable\Framework\Exception("Log directory is not writable: {$this->path}"); } } return $this->path; @@ -121,7 +121,7 @@ public function write($message, $logFile = null, $showTimezone = false) if ($showTimezone) { $timeString .= ' ' . $now->getTimezone()->getName(); } - $message = '[' .$timeString . '] ' . $message; + $message = "[{$timeString}] {$message}"; // And append it to the log file_put_contents($logPath, $message, FILE_APPEND | LOCK_EX); diff --git a/src/Framework/Mail/Mailer.php b/src/Framework/Mail/Mailer.php new file mode 100644 index 00000000..decd09cd --- /dev/null +++ b/src/Framework/Mail/Mailer.php @@ -0,0 +1,90 @@ +view = $view; + $this->templateVariables = $templateVariables; + $this->path = $path; + } + + /** + * @param array $data + * + * @return $this + */ + public function setTemplateVariables(array $data) + { + $this->templateVariables->setAll($data); + return $this; + } + + /** + * @return array + */ + public function getTemplateVariables() + { + return $this->templateVariables->getAll(); + } + + /** + * @param string $key + * @param mixed $value + * + * @return $this + */ + public function setTemplateVariable($key, $value) + { + $this->templateVariables->set($key, $value); + return $this; + } + + /** + * @param string $key + * + * @return mixed|null + */ + public function getTemplateVariable($key) + { + return $this->templateVariables->get($key); + } + + /** + * @param string $path + * + * @return $this + * @throws \Parable\Framework\Exception + */ + public function loadTemplate($path) + { + $path = $this->path->getDir($path); + + if (!file_exists($path)) { + throw new \Parable\Framework\Exception("Email template '{$path}' does not exist."); + } + + $content = $this->view->partial($path); + $this->setBody(trim($content)); + return $this; + } +} diff --git a/src/Framework/Mail/TemplateVariables.php b/src/Framework/Mail/TemplateVariables.php new file mode 100644 index 00000000..853d8a88 --- /dev/null +++ b/src/Framework/Mail/TemplateVariables.php @@ -0,0 +1,12 @@ +...property * - * @property \Parable\Auth\Rights $rights - * @property \Parable\Auth\Authentication $authentication - * @property \Parable\Events\Dock $dock - * @property \Parable\Events\Hook $hook - * @property \Parable\Filesystem\Path $path - * @property \Parable\Framework\App $app - * @property \Parable\Framework\Config $config - * @property \Parable\Framework\Dispatcher $dispatcher - * @property \Parable\Framework\Log $log - * @property \Parable\Framework\Toolkit $toolkit - * @property \Parable\Framework\View $view - * @property \Parable\Http\Request $request - * @property \Parable\Http\Response $response - * @property \Parable\Http\Url $url - * @property \Parable\Http\SessionMessage $sessionMessage - * @property \Parable\Http\Values $values - * @property \Parable\Http\Values\Cookie $cookie - * @property \Parable\Http\Values\Get $get - * @property \Parable\Http\Values\Internal $internal - * @property \Parable\Http\Values\Post $post - * @property \Parable\Http\Values\Session $session - * @property \Parable\ORM\Query $query - * @property \Parable\ORM\Database $database - * @property \Parable\Routing\Router $router + * @property \Parable\Event\Dock $dock + * @property \Parable\Event\Hook $hook + * @property \Parable\Filesystem\Path $path + * @property \Parable\Framework\App $app + * @property \Parable\Framework\Authentication $authentication + * @property \Parable\Framework\Config $config + * @property \Parable\Framework\Debug $debug + * @property \Parable\Framework\Dispatcher $dispatcher + * @property \Parable\Framework\Log $log + * @property \Parable\Framework\Toolkit $toolkit + * @property \Parable\Framework\View $view + * @property \Parable\Framework\Mail\Mailer $mailer + * @property \Parable\Framework\Mail\TemplateVariables $templateVariables + * @property \Parable\Http\Request $request + * @property \Parable\Http\Response $response + * @property \Parable\Http\SessionMessage $sessionMessage + * @property \Parable\Http\Url $url + * @property \Parable\Http\Values $values + * @property \Parable\Http\Values\Cookie $cookie + * @property \Parable\Http\Values\Get $get + * @property \Parable\Http\Values\Internal $internal + * @property \Parable\Http\Values\Post $post + * @property \Parable\Http\Values\Session $session + * @property \Parable\ORM\Query $query + * @property \Parable\ORM\Database $database + * @property \Parable\Routing\Router $router + * @property \Parable\Tool\Rights $rights */ class View { diff --git a/src/Http/Response.php b/src/Http/Response.php index 97df3829..49a33372 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -142,9 +142,9 @@ public function send() { $this->output->prepare($this); - header("HTTP/1.1 " . $this->getHttpCode() . " " . $this->getHttpCodeText()); + header("HTTP/1.1 {$this->getHttpCode()} {$this->getHttpCodeText()}"); foreach ($this->headers as $key => $value) { - header($key . ': ' . $value); + header("{$key}: {$value}"); } echo $this->getContent(); diff --git a/src/Http/Url.php b/src/Http/Url.php index 9bf0a66e..f5750cce 100644 --- a/src/Http/Url.php +++ b/src/Http/Url.php @@ -70,7 +70,7 @@ public function redirect($url) ) { $url = $this->getUrl($url); } - header('location: ' . $url); + header("location: {$url}"); die(); } } diff --git a/src/Http/Values/GetSet.php b/src/Http/Values/GetSet.php index bd406071..03d21cc3 100644 --- a/src/Http/Values/GetSet.php +++ b/src/Http/Values/GetSet.php @@ -64,6 +64,38 @@ public function get($key) return null; } + /** + * @return array + */ + public function getAllAndReset() + { + $data = $this->getAll(); + $this->reset(); + return $data; + } + + /** + * @param string $key + * + * @return mixed|null + */ + public function getAndRemove($key) + { + $data = $this->get($key); + if ($data) { + $this->remove($key); + } + return $data; + } + + /** + * @return int + */ + public function count() + { + return count($this->getAll()); + } + /** * Set specific value by key if resource set * @@ -128,6 +160,16 @@ public function setAll(array $values) */ public function remove($key) { - return $this->set($key, null); + $this->set($key, null); + return $this; + } + + /** + * @return $this + */ + public function reset() + { + $this->setAll([]); + return $this; } } diff --git a/src/Mail/Mailer.php b/src/Mail/Mailer.php index 1fdbce52..166b584a 100644 --- a/src/Mail/Mailer.php +++ b/src/Mail/Mailer.php @@ -4,15 +4,6 @@ class Mailer { - /** @var \Parable\Framework\View */ - protected $view; - - /** @var \Parable\Http\Values\Internal */ - protected $internal; - - /** @var array */ - protected $templateVariables = []; - /** @var array */ protected $addresses = [ 'to' => [], @@ -28,16 +19,14 @@ class Mailer protected $body; /** @var array */ - protected $headers = []; + protected $requiredHeaders = []; - public function __construct( - \Parable\Framework\View $view, - \Parable\Http\Values\Internal $internal - ) { - $this->view = $view; - $this->internal = $internal; + /** @var array */ + protected $headers = []; - $this->headers = [ + public function __construct() + { + $this->requiredHeaders = [ "MIME-Version: 1.0", "Content-type: text/html; charset=UTF-8", ]; @@ -155,6 +144,14 @@ public function setSubject($subject) return $this; } + /** + * @return string + */ + public function getSubject() + { + return $this->subject; + } + /** * @param string $body * @@ -166,50 +163,52 @@ public function setBody($body) return $this; } + /** + * @return string + */ + public function getBody() + { + return $this->body; + } + /** * @param string $header * * @return $this */ - public function addHeader($header) + public function addRequiredHeader($header) { - $this->headers[] = $header; + $this->requiredHeaders[] = $header; return $this; } /** - * @param array $data - * - * @return $this + * @return array */ - public function setTemplateVariables(array $data) + public function getRequiredHeaders() { - $this->templateVariables = $data; - return $this; + return $this->requiredHeaders; } /** - * @param string $name + * @param string $header * * @return $this */ - public function loadTemplate($name) + public function addHeader($header) { - // Temporarily set the template variables as internal values - foreach ($this->templateVariables as $key => $value) { - $this->internal->set($key, $value); - } - - $content = $this->view->partial("app/View/Email/{$name}.phtml"); - $this->setBody($content); - - // And remove the values again, to not clutter internal storage - foreach ($this->templateVariables as $key => $value) { - $this->internal->remove($key); - } + $this->headers[] = $header; return $this; } + /** + * @return array + */ + public function getHeaders() + { + return $this->headers; + } + /** * @return $this * @throws \Parable\Mail\Exception @@ -243,6 +242,8 @@ public function send() $from = $this->getAddresses('from'); $this->addHeader("From: {$from}"); + $headers = array_merge($this->requiredHeaders, $this->headers); + mail( $to, $this->subject, @@ -251,4 +252,61 @@ public function send() ); return $this; } + + /** + * Reset just the subject, body, headers + * + * @return $this + */ + public function resetMailData() + { + // Reset the mail values + $this->subject = null; + $this->body = null; + $this->headers = []; + + // And any template variables currently stored + $this->removeTemplateVariables(); + $this->templateVariables = []; + + return $this; + } + + /** + * Reset all the addresses currently stored to receive + * the e-mail. + * + * @return $this + */ + public function resetRecipients() + { + $this->addresses['to'] = []; + $this->addresses['cc'] = []; + $this->addresses['bcc'] = []; + return $this; + } + + /** + * Reset the sender email and name. + * + * @return $this + */ + public function resetSender() + { + $this->addresses['from'] = []; + return $this; + } + + /** + * Reset the class so it can be re-used for another, different + * e-mail. This resets everything BUT the sender email and name. + * + * @return $this + */ + public function reset() + { + $this->resetMailData(); + $this->resetRecipients(); + return $this; + } } diff --git a/src/Routing/Router.php b/src/Routing/Router.php index c3377197..b045d76a 100644 --- a/src/Routing/Router.php +++ b/src/Routing/Router.php @@ -52,7 +52,7 @@ public function addRoute($name, array $routeArray) public function getRouteByName($name) { if (!isset($this->routes[$name])) { - throw new \Parable\Routing\Exception('Route named "' . $name . '" does not exist.'); + throw new \Parable\Routing\Exception("Route named '{$name}' does not exist."); } return $this->routes[$name]; } diff --git a/src/Auth/Rights.php b/src/Tool/Rights.php similarity index 98% rename from src/Auth/Rights.php rename to src/Tool/Rights.php index d0714969..3d0105a2 100644 --- a/src/Auth/Rights.php +++ b/src/Tool/Rights.php @@ -1,6 +1,6 @@ setName('Parable'); - -/* - * Set up some dependencies - */ -/** @var \Parable\Filesystem\Path $path */ -$path = \Parable\DI\Container::get(\Parable\Filesystem\Path::class); -$path->setBasedir(BASEDIR); - -$command->setName('init'); -$command->setDescription('This command initializes a parable structure.'); -$command->setCallable(function ( - \Parable\Console\Output $output, - \Parable\Console\Input $input, - \Parable\Console\Parameter $parameter -) use ( - $path -) { - $output->writeln([ - "Parable initialization script", - "-----------------------------------", - "This script will initialize Parable's structure.", - "", - ]); - - for (;;) { - $output->write("Do you want to continue? [y/N] "); - if ($input->getYesNo(false)) { - break; - } else { - $output->writeln(["", "You chose not to continue.", ""]); - return; - } - } - - /** @var \Parable\Filesystem\Path $path */ - $output->newline(); - $output->write('Creating folder structure...'); - - $dirs = [ - 'app', - 'app/Config', - 'app/Controller', - 'app/Init', - 'app/Model', - 'app/Routes', - 'app/View', - 'app/View/Home', - 'public', - ]; - - foreach ($dirs as $dir) { - if (!file_exists($path->getDir($dir))) { - mkdir($path->getDir($dir)); - } - } - - $output->writeln("OK"); - - $output->write('Copying files...'); - copy( - $path->getDir('vendor/devvoh/parable/structure/.htaccess'), - $path->getDir('.htaccess') - ); - copy( - $path->getDir('vendor/devvoh/parable/structure/public/.htaccess'), - $path->getDir('public/.htaccess') - ); - copy( - $path->getDir('vendor/devvoh/parable/structure/public/index.php'), - $path->getDir('public/index.php') - ); - copy( - $path->getDir('vendor/devvoh/parable/structure/app/Routes/App.php'), - $path->getDir('app/Routes/App.php') - ); - copy( - $path->getDir('vendor/devvoh/parable/structure/app/Config/App.php'), - $path->getDir('app/Config/App.php') - ); - copy( - $path->getDir('vendor/devvoh/parable/structure/app/Controller/Home.php'), - $path->getDir('app/Controller/Home.php') - ); - copy( - $path->getDir('vendor/devvoh/parable/structure/app/Init/Example.php'), - $path->getDir('app/Init/Example.php') - ); - copy( - $path->getDir('vendor/devvoh/parable/structure/app/Model/User.php'), - $path->getDir('app/Model/User.php') - ); - copy( - $path->getDir('vendor/devvoh/parable/structure/app/View/Home/index.phtml'), - $path->getDir('app/View/Home/index.phtml') - ); - - $output->writeln("OK"); - - $output->writeln(["", "Completed!", ""]); -}); - -$app->addCommand($command); - -$app->run(); diff --git a/structure/app/Command/HelloWorld.php b/structure/app/Command/HelloWorld.php new file mode 100644 index 00000000..d86d95fc --- /dev/null +++ b/structure/app/Command/HelloWorld.php @@ -0,0 +1,17 @@ +writeln('Hello, world!'); + } +} \ No newline at end of file diff --git a/structure/app/Config/App.php b/structure/app/Config/App.php index d69fc91d..35b80244 100644 --- a/structure/app/Config/App.php +++ b/structure/app/Config/App.php @@ -29,6 +29,11 @@ public function getValues() 'password' => 'password', 'database' => 'database', ], + 'console' => [ + 'commands' => [ + \Command\HelloWorld::class, + ] + ] ]; } } diff --git a/structure/app/Init/Example.php b/structure/app/Init/Example.php index e7e7c13a..62f39291 100644 --- a/structure/app/Init/Example.php +++ b/structure/app/Init/Example.php @@ -5,7 +5,7 @@ class Example { public function __construct( - \Parable\Events\Hook $hook, + \Parable\Event\Hook $hook, \Parable\Http\Response $response ) { /* diff --git a/structure/app/Routes/App.php b/structure/app/Routing/App.php similarity index 94% rename from structure/app/Routes/App.php rename to structure/app/Routing/App.php index 23818e5b..25df5d3b 100644 --- a/structure/app/Routes/App.php +++ b/structure/app/Routing/App.php @@ -1,8 +1,8 @@