Greeble has many built-in features which allow rapid, easy development of web-apps from scratch.
Autoloading of Classes and Modules
One of the features of PHP that makes it so easy to use is the availability of all built-in features without the need to worry about any dependencies (once the server is set up properly).
This breaks down when in a large complex webapp a single script depends on several class definitions which must be explicitly included. It can be difficult to keep track of all the classes a script might possibly need and put their include statements at the top. Fortunately, PHP provides a mechanism for getting around this: __autoload.
Greeble defines __autoload, so that whenever a class that has not been defined in the current script is used the file in which that class is defined is automagically loaded and the script execution continues as though that class had been available all along. When loading a class from a new module, Greeble also automatically loads the appropriate language for that modle (see "Automatic Language Selection and Loading" below).
The downside to this system is that it is now difficult to see at a glance all the files that a particular script depends on. However, this is in most cases an acceptible sacrifice, and when a script is missing a dependency, Greeble fails gracefully, displaying a message to the user and logging the specific missing dependency.
Automatic Language Selection and Loading
Greeble uses a combination of methods to decide which language to display to the user.
First and foremost, it tries to load the language the current user has chosen to view the site in the, which is stored in the session variables if the user is logged in and will otherwise be stored in cookies if the user has previously been loggen in.
If that language is not available or is not specified, Greeble will look at the HTTP Accept Language header, loading the first language available in the most preferred by the user's browser.
If no language has been loaded yet, Greeble will fall back on its default language which should be necessarily available for all modules.
If the default language is missing, Greeble will then display and log an error.
Localization files in Greeble by convention are a list of constants prefixed by an underscore and then the name of the module they belong to. Each constant defines a string that provides some information relevant to the module in the specified language.
This convention is not strictly necessary- language files can define the strings in whichever way the programmer decides is most useful.
Automatic Error Logging
Normally when an error occurs an error message is displayed to the user and not much else happens. Greeble seeks to improve the usefulness of Exceptions by defining a new Exception class, GException, which automatically writes the problem to an error log.
It is a very straightforward mechanism, but it proves to be extremely useful in the website context, because without automatic logging the programmer has to worry about logging his/her own errors and possibly missing some error that a user experiences.
Database-Managed Sessions
PHP normally tracks sessions by writing text to a file named by the session ID, stored in a temporary location. In Greeble this location has become a database table and the file a row in this table (Greeble currently uses MySQL for this, but this could easily be extended to a different database type).
This has several benefits; the most obvious of which is the ability to query the database for information about the currently active users, including those who aren't currently logged in.
Database Abstraction
The heart of Greeble is the DBRow class, whose implementation in this version is MySQL (but it can be modified for other database types). The DBRow class provides several methods that abstract away common database queries and handles errors arising from them (which usually occurs under heavy load).
The best way to show the usefulness of DBRow is to provide an example:
class Post extends DBRow { const TABLE='posts'; }
$p1 = new
Post(1); //fetches post #1 from the database
$p1->field('title','This is my new title'); //sets the title field on the object
$p1->update(); //updates the database row to reflect the change
$p2 = new Post();
$p2->field('title','This is a new post');
$p2->insert(); //inserts a new row into the database
$posts = Post::fetchs("`title` LIKE 'This'"); //creates an array of post objects
foreach($posts as $p) {
echo $p->field('title') . '<br>';
}
$deleteme = new Post(2);
$deleteme->delete(); //removes a row from the database
HTML Templates
Greeble's goal is to separate content, logic, and display. Unfortunately, while it provides templating abstraction to some extent, the html templates are far more coupled than is desireable. Better separation of control and display is planned for Greeble 3.4.
Form Generation and Validation
One of the biggest pains in creating a website which allows users to upload content is validating and sanitizing the users' input, to prevent them from trying to do something they can't or shouldn't do. Greeble provides a simple solution: the Form, Input, and Constraint classes. The programmer creates a Form object and adds multiple types of Inputs to it, and adds multiple Constraints to those Inputs which describe what the user can and can't enter in that field. Constraints are evaluated server-side and client-side with Javascript, so the user can instantly see when an input is invalid. The programmer need only use a single method call, Form::isValid(), to be sure that all the input given meets the expected Constraints.
Tree-row Class
The DBTreeRow class is an extremely powerful tool for managing hierarchical content. It extends DBRow's insert and delete methods to allow nodes to be inserted and deleted from arbitrary positions in trees. It also provides some new update and fetch methods which operate on a node's parent, ancestors, children, and/or descendants.
URL Rewrites
URL rewrites will make the URL for a page even cleaner, going from http://greeble.net/Article/?action=viewArticle&aid=3 to http://greeble.net/Article/3.
URL rewrites are planned for 3.4
Web-Based Installation and Configuration
This feature will allow site administrators to install and configure their website to their liking without ever having to open a PHP file.
Web-Based Installation and Configuration is planned for 3.4
Greeble's power comes from its many modules, which perform the actual content management. Without any modules, Greeble is nothing more than a framework for templating websites.
|