Permalink
Browse files

Improved Composer support, no longer using App::getModel and App:getL…

…ibrary, include trace in exceptions log
  • Loading branch information...
AliasIO committed Apr 18, 2015
1 parent 53f355e commit ad11bae49b0a4ba22cb83f6884d55c6917271c81
Showing with 72 additions and 159 deletions.
  1. +6 −1 .gitignore
  2. +18 −24 README.md
  3. +5 −3 composer.json
  4. +1 −1 phpunit.xml.dist
  5. +4 −6 public/index.php
  6. 0 {vendor → src}/HelloWorld/Controllers/Error404.php
  7. +4 −2 {vendor → src}/HelloWorld/Controllers/Index.php
  8. 0 {vendor → src}/HelloWorld/Libraries/Example.php
  9. 0 {vendor → src}/HelloWorld/Models/Example.php
  10. 0 {vendor → src}/HelloWorld/Plugins/Example.php
  11. 0 {vendor → src}/HelloWorld/views/error404.php
  12. 0 {vendor → src}/HelloWorld/views/footer.php
  13. +1 −1 {vendor → src}/HelloWorld/views/header.php
  14. 0 {vendor → src}/HelloWorld/views/index.php
  15. +1 −1 {vendor → src}/Mock/App.php
  16. 0 {vendor → src}/Mock/Controllers/Index.php
  17. 0 {vendor → src}/Mock/Libraries/Mock.php
  18. 0 {vendor → src}/Mock/Models/Mock.php
  19. 0 {vendor → src}/Mock/Plugins/Mock.php
  20. +9 −0 src/Mock/View.php
  21. 0 {vendor → src}/Mock/views/index.php
  22. +4 −47 {vendor → src}/Swiftlet/Abstracts/App.php
  23. +4 −4 {vendor → src}/Swiftlet/Abstracts/Common.php
  24. 0 {vendor → src}/Swiftlet/Abstracts/Controller.php
  25. 0 {vendor → src}/Swiftlet/Abstracts/Library.php
  26. 0 {vendor → src}/Swiftlet/Abstracts/Model.php
  27. 0 {vendor → src}/Swiftlet/Abstracts/Plugin.php
  28. +1 −1 {vendor → src}/Swiftlet/Abstracts/View.php
  29. +0 −2 {vendor → src}/Swiftlet/App.php
  30. 0 {vendor → src}/Swiftlet/Exception.php
  31. +0 −23 {vendor → src}/Swiftlet/Interfaces/App.php
  32. 0 {vendor → src}/Swiftlet/Interfaces/Common.php
  33. 0 {vendor → src}/Swiftlet/Interfaces/Controller.php
  34. 0 {vendor → src}/Swiftlet/Interfaces/Library.php
  35. 0 {vendor → src}/Swiftlet/Interfaces/Model.php
  36. 0 {vendor → src}/Swiftlet/Interfaces/Plugin.php
  37. 0 {vendor → src}/Swiftlet/Interfaces/View.php
  38. +9 −0 src/Swiftlet/View.php
  39. +1 −14 {vendor → src}/Swiftlet/tests/AppTest.php
  40. +1 −2 {vendor → src}/Swiftlet/tests/ControllerTest.php
  41. +1 −2 {vendor → src}/Swiftlet/tests/LibraryTest.php
  42. +1 −2 {vendor → src}/Swiftlet/tests/PluginTest.php
  43. +1 −2 {vendor → src}/Swiftlet/tests/ViewTest.php
  44. 0 vendor/.gitkeep
  45. +0 −9 vendor/Mock/View.php
  46. +0 −12 vendor/Swiftlet/View.php
View
@@ -1,4 +1,9 @@
-log/exceptions.log
+composer.lock
+composer.phar
+log/*.log
tags
+vendor/*
.DS_Store
.idea/
+
+!.gitkeep
View
@@ -13,24 +13,24 @@ Buzzword compliance
✔ Micro-Framework
✔ Loosely coupled
-✔ Namespaced
✔ Unit tested
+✔ Namespaced
✔ Pluggable
-✔ [PSR-0](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md)
+✔ [Composer](https://getcomposer.org)
+✔ [PSR-4](http://www.php-fig.org/psr/psr-4)
✔ PHP5
✔ MVC
✔ OOP
✘ ORM
-Installation in three easy steps
---------------------------------
+Installation
+------------
-* Step 1: Clone (or download and extract) Swiftlet into a directory on your PHP
+* Clone (or download and extract) Swiftlet into a directory on your PHP
supported web server.
-* Step 2: Congratulations, Swiftlet is now up and running.
-* Step 3: There is no step 3.
+* Having [Composer](https://getcomposer.org) installed, run `composer dump-autoload`.
Getting started: controllers and views
@@ -41,7 +41,7 @@ Let's create a page. Each page consists of a controller and at least one view.
The controller does most of the work; views should be limited to simple
presentation logic (loops and switches).
-**Controller `vendor/HelloWorld/Controllers/Foo.php`**
+**Controller `src/HelloWorld/Controllers/Foo.php`**
```php
<?php
@@ -104,10 +104,10 @@ If the controller or action is not specified they default to `index` (`/`
will call `index()` on `HelloWorld\Controller\Index`).
Underscores in the controller name are translated to directory separators, so
-`/foo_bar` will point to `vendor/HelloWorld/Controllers/Foo/Bar.php`.
+`/foo_bar` will point to `src/HelloWorld/Controllers/Foo/Bar.php`.
Dashes in routes are ignored; `/foo-bar/baz-qux` calls `bazqux()` in
-`vendor/HelloWorld/Controllers/Foobar.php`.
+`src/HelloWorld/Controllers/Foobar.php`.
**Custom routes**
@@ -175,7 +175,7 @@ Models
Let's throw a model into the mix and update the controller.
-**Model `vendor/HelloWorld/Models/Foo.php`**
+**Model `src/HelloWorld/Models/Foo.php`**
```php
<?php
@@ -190,7 +190,7 @@ class Foo extends \Swiftlet\Abstracts\Model
}
```
-**Controller `vendor/HelloWorld/Controllers/Foo.php`**
+**Controller `src/HelloWorld/Controllers/Foo.php`**
```php
<?php
@@ -203,8 +203,8 @@ class Foo extends \Swiftlet\Abstracts\Controller
public function index()
{
// Get an instance of the Example class
- // See vendor/HelloWorld/Models/Example.php
- $example = $this->app->getModel('example');
+ // See src/HelloWorld/Models/Example.php
+ $example = \HelloWorld\Models\Example;
$this->view->helloWorld = $example->getHelloWorld();
}
@@ -216,7 +216,7 @@ object such as a user.
```php
<?php
-$user = $this->app->getModel('user');
+$user = \HelloWorld\Models\User;
$user->setEmail('example@example.com');
@@ -235,7 +235,7 @@ points for code that extends the application. Swiftlet has a few core hooks and
additiontal ones can be registered pretty much anywhere using
`$this->app->registerHook($hookName, $controller, $view)`.
-**Plugin `vendor/HelloWorld/Plugins/Foo.php`**
+**Plugin `src/HelloWorld/Plugins/Foo.php`**
```php
<?php
@@ -255,7 +255,7 @@ This plugin implements the core `actionAfter` hook and changes the view
variable `helloWorld` from our previous example to `Hi world!`.
Plugins don't need to be installed or activated, all files in the
-`vendor/HelloWorld/Plugins/` directory are automatically included and their classes
+`src/HelloWorld/Plugins/` directory are automatically included and their classes
instantiated. Plugins are hooked in alphabetical order.
The core hooks are:
@@ -275,7 +275,7 @@ should go in a separate library class.
```php
<?php
-$email = $this->app->getLibrary('email');
+$email = \HelloWorld\Libraries\Email
$email->send($to, $subject, $message);
```
@@ -315,12 +315,6 @@ Get a configuration value
* `App setConfig(string $variable, mixed $value)`
Set a configuration value
-* `Model getModel(string $modelName)`
-Get a model instance
-
-* `Library getLibrary(string $libraryName)`
-Get a library instance
-
* `App registerHook(string $hookName, array $params)`
Register a hook
View
@@ -14,15 +14,17 @@
"repositories": [
{
"type": "vcs",
- "url": "https://github.com/ElbertF/Swiftlet"
+ "url": "https://github.com/AliasIO/Swiftlet"
}
],
"require": {
"php": ">=5.3.3"
},
"autoload": {
- "psr-0": {
- "Swiftlet\\": "vendor/"
+ "psr-4": {
+ "HelloWorld\\": "src/HelloWorld",
+ "Mock\\": "src/Mock",
+ "Swiftlet\\": "src/Swiftlet"
}
}
}
View
@@ -1,5 +1,5 @@
<testsuites>
<testsuite name="Swiftlet">
- <directory>vendor/*/tests</directory>
+ <directory>src/*/tests</directory>
</testsuite>
</testsuites>
View
@@ -5,17 +5,13 @@
try {
chdir(dirname(__FILE__) . '/..');
- // Bootstrap the application
- require 'vendor/Swiftlet/App.php';
- require 'vendor/Swiftlet/View.php';
+ require 'vendor/autoload.php';
$app = new App(new View, 'HelloWorld');
// Convert errors to ErrorException instances
set_error_handler(array($app, 'error'), E_ALL | E_STRICT);
- spl_autoload_register(array($app, 'autoload'));
-
require 'config/main.php';
date_default_timezone_set('UTC');
@@ -37,7 +33,9 @@
$errorCode = substr(sha1(uniqid(mt_rand(), true)), 0, 5);
- file_put_contents('log/exceptions.log', $errorCode . date(' r ') . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine() . "\n", FILE_APPEND);
+ $errorMessage = $errorCode . date(' r ') . $e->getMessage() . ' in ' . $e->getFile() . ' on line ' . $e->getLine();
+
+ file_put_contents('log/exceptions.log', "\n" . $errorMessage . "\n" . $e->getTraceAsString() . "\n", FILE_APPEND);
exit('Exception: ' . $errorCode . '<br><br><small>The issue has been logged. Please contact the website administrator.</small>');
}
@@ -2,6 +2,8 @@
namespace HelloWorld\Controllers;
+use HelloWorld\Models\Example as ExampleModel;
+
/**
* Index controller
*/
@@ -19,8 +21,8 @@ class Index extends \Swiftlet\Abstracts\Controller
*/
public function index(array $args = array())
{
- // Create a model instance, see /HelloWorld/Models/Example.php
- $example = $this->app->getModel('example');
+ // Create a model instance, see /src/HelloWorld/Models/Example.php
+ $example = new ExampleModel;
// Get some data from the model and pass it to the view to display it
$this->view->helloWorld = $example->getHelloWorld();
@@ -4,6 +4,6 @@
<head>
<title><?= $this->pageTitle ?></title>
- <link type="text/css" rel="stylesheet" href="<?= $this->getRootPath() ?>css/layout.css">
+ <link rel="stylesheet" href="<?= $this->getRootPath() ?>css/layout.css">
</head>
<body>
File renamed without changes.
@@ -2,7 +2,7 @@
namespace Mock;
-require_once 'vendor/Swiftlet/Abstracts/App.php';
+require_once 'src/Swiftlet/Abstracts/App.php';
class App extends \Swiftlet\Abstracts\App
{ }
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
View
@@ -0,0 +1,9 @@
+<?php
+
+namespace Mock;
+
+require_once 'src/Swiftlet/Interfaces/View.php';
+require_once 'src/Swiftlet/Abstracts/View.php';
+
+class View extends \Swiftlet\Abstracts\View
+{ }
File renamed without changes.
@@ -2,11 +2,6 @@
namespace Swiftlet\Abstracts;
-require_once 'vendor/Swiftlet/Interfaces/App.php';
-require_once 'vendor/Swiftlet/Interfaces/Common.php';
-require_once 'vendor/Swiftlet/Abstracts/Common.php';
-require_once 'vendor/Swiftlet/Exception.php';
-
/**
* Application class
* @abstract
@@ -44,7 +39,7 @@
protected $hooks = array();
/**
- * vendor/Plugins
+ * Plugins
* @var array
*/
protected $plugins = array();
@@ -56,7 +51,7 @@
* @param string $vendorPath
* @return App
*/
- public function __construct(\Swiftlet\Interfaces\View $view, $vendor = null, $vendorPath = 'vendor/')
+ public function __construct(\Swiftlet\Interfaces\View $view, $vendor = null, $vendorPath = 'src/')
{
$this->view = $view;
@@ -201,6 +196,8 @@ public function loadPlugins()
if ( is_file($this->vendorPath . str_replace('\\', '/', $pluginClass) . '.php') ) {
$this->plugins[$pluginClass] = array();
+ $x = new \HelloWorld\Plugins\Example;
+
$reflection = new \ReflectionClass($pluginClass);
$parentClass = $reflection->getParentClass();
@@ -246,30 +243,6 @@ public function setConfig($variable, $value)
return $this;
}
- /**
- * Get a model instance
- * @param string $modelName
- * @return \Swiftlet\Interfaces\Model
- */
- public function getModel($modelName)
- {
- $modelClass = '\\' . $this->vendor . '\Models\\' . ucfirst($modelName);
-
- return new $modelClass;
- }
-
- /**
- * Get a library instance
- * @param string $libraryName
- * @return \Swiftlet\Interfaces\Library
- */
- public function getLibrary($libraryName)
- {
- $libraryClass = '\\' . $this->vendor . '\Libraries\\' . ucfirst($libraryName);
-
- return new $libraryClass($this);
- }
-
/**
* Register a hook for plugins to implement
* @param string $hookName
@@ -297,22 +270,6 @@ public function registerHook($hookName, \Swiftlet\Interfaces\Controller $control
return $this;
}
- /**
- * Class autoloader
- * @param string $className
- * @see https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-0.md
- */
- public function autoload($className)
- {
- preg_match('/(^.+\\\)?([^\\\]+)$/', ltrim($className, '\\'), $match);
-
- $file = $this->vendorPath . str_replace('\\', '/', $match[1]) . str_replace('_', '/', $match[2]) . '.php';
-
- if ( file_exists($file) ) {
- include $file;
- }
- }
-
/**
* Convert errors to \ErrorException instances
* @param int $number
@@ -15,12 +15,12 @@
* @param mixed $arguments
* @throws \Swiftlet\Exception
*/
- public function __call($property, $arguments)
+ public function __call($name, $arguments)
{
- $action = substr($property, 0, 3);
+ $action = substr($name, 0, 3);
if ( $action == 'get' || $action == 'set' ) {
- $property = lcfirst(substr($property, 3));
+ $property = lcfirst(substr($name, 3));
if ( property_exists($this, $property) ) {
$reflection = new \ReflectionObject($this);
@@ -37,6 +37,6 @@ public function __call($property, $arguments)
}
}
- throw new \Swiftlet\Exception('Not implemented: ' . get_called_class() . '::' . $property);
+ throw new \Swiftlet\Exception('Not implemented: ' . get_called_class() . '::' . $name);
}
}
@@ -18,7 +18,7 @@
* Vendor path
* @var string
*/
- public $vendorPath = 'vendor/';
+ public $vendorPath = 'src/';
/**
* View variables
@@ -2,8 +2,6 @@
namespace Swiftlet;
-require_once 'vendor/Swiftlet/Abstracts/App.php';
-
/**
* Application class
*/
File renamed without changes.
Oops, something went wrong.

0 comments on commit ad11bae

Please sign in to comment.