Skip to content

Commit

Permalink
Moved code to lib/ folder, added optional PSR-0 support, added PSR-0 …
Browse files Browse the repository at this point in the history
…example and updated readme
  • Loading branch information
jbroadway committed Jan 11, 2012
1 parent 70aa86e commit 618b939
Show file tree
Hide file tree
Showing 33 changed files with 276 additions and 57 deletions.
32 changes: 27 additions & 5 deletions README.md
@@ -1,10 +1,10 @@
## Analog - PHP 5.3+ micro logging class
## Analog - PHP 5.3+ micro logging package

* Copyright: (c) 2012 Johnny Broadway
* License: http://www.opensource.org/licenses/mit-license.php

A short and simple logging class based on the idea of using closures for
configurability and extensibility. It functions as a static class, but you can
A [MicroPHP](http://microphp.org/) logging package based on the idea of using closures
for configurability and extensibility. It functions as a static class, but you can
completely control the writing of log messages through a closure function
(aka [anonymous functions](http://ca3.php.net/manual/en/functions.anonymous.php)).

Expand All @@ -28,8 +28,7 @@ with examples for each in the examples folder. These include:
* Syslog - Send messages to syslog
* Variable - Buffer messages to a variable reference.

So while it's a micro class (the core is 51 lines of code, plus the handler of your choice),
it's highly extensible and very capable out of the box too.
So while it's a micro class, it's highly extensible and very capable out of the box too.

### Rationale

Expand All @@ -51,6 +50,8 @@ flexibility.
### Usage

Standard usage:

```php
<?php

Expand All @@ -77,4 +78,25 @@ Analog::log ('Debugging info', Analog::DEBUG);
?>
```

PSR-0 usage:

```php
<?php

require_once ('SplClassLoader.php');

$loader = new SplClassLoader ('Analog', 'lib');
$loader->register ();

use \Analog\Analog;

Analog::log ('Log this error');

Analog::handler (\Analog\Handler\FirePHP::init ());

Analog::log ('Take me to your browser');

?>
```

For more examples, see the [examples](https://github.com/jbroadway/analog/tree/master/examples) folder.
7 changes: 6 additions & 1 deletion composer.json
Expand Up @@ -13,6 +13,11 @@
}
],
"require": {
"php": ">=5.3.0"
"php": ">=5.3.2"
},
"autoload": {
"psr-0": {
"Analog": "lib/"
}
}
}
136 changes: 136 additions & 0 deletions examples/SplClassLoader.php
@@ -0,0 +1,136 @@
<?php

/**
* SplClassLoader implementation that implements the technical interoperability
* standards for PHP 5.3 namespaces and class names.
*
* http://groups.google.com/group/php-standards/web/final-proposal
*
* // Example which loads classes for the Doctrine Common package in the
* // Doctrine\Common namespace.
* $classLoader = new SplClassLoader('Doctrine\Common', '/path/to/doctrine');
* $classLoader->register();
*
* @author Jonathan H. Wage <jonwage@gmail.com>
* @author Roman S. Borschel <roman@code-factory.org>
* @author Matthew Weier O'Phinney <matthew@zend.com>
* @author Kris Wallsmith <kris.wallsmith@gmail.com>
* @author Fabien Potencier <fabien.potencier@symfony-project.org>
*/
class SplClassLoader
{
private $_fileExtension = '.php';
private $_namespace;
private $_includePath;
private $_namespaceSeparator = '\\';

/**
* Creates a new <tt>SplClassLoader</tt> that loads classes of the
* specified namespace.
*
* @param string $ns The namespace to use.
*/
public function __construct($ns = null, $includePath = null)
{
$this->_namespace = $ns;
$this->_includePath = $includePath;
}

/**
* Sets the namespace separator used by classes in the namespace of this class loader.
*
* @param string $sep The separator to use.
*/
public function setNamespaceSeparator($sep)
{
$this->_namespaceSeparator = $sep;
}

/**
* Gets the namespace seperator used by classes in the namespace of this class loader.
*
* @return void
*/
public function getNamespaceSeparator()
{
return $this->_namespaceSeparator;
}

/**
* Sets the base include path for all class files in the namespace of this class loader.
*
* @param string $includePath
*/
public function setIncludePath($includePath)
{
$this->_includePath = $includePath;
}

/**
* Gets the base include path for all class files in the namespace of this class loader.
*
* @return string $includePath
*/
public function getIncludePath()
{
return $this->_includePath;
}

/**
* Sets the file extension of class files in the namespace of this class loader.
*
* @param string $fileExtension
*/
public function setFileExtension($fileExtension)
{
$this->_fileExtension = $fileExtension;
}

/**
* Gets the file extension of class files in the namespace of this class loader.
*
* @return string $fileExtension
*/
public function getFileExtension()
{
return $this->_fileExtension;
}

/**
* Installs this class loader on the SPL autoload stack.
*/
public function register()
{
spl_autoload_register(array($this, 'loadClass'));
}

/**
* Uninstalls this class loader from the SPL autoloader stack.
*/
public function unregister()
{
spl_autoload_unregister(array($this, 'loadClass'));
}

/**
* Loads the given class or interface.
*
* @param string $className The name of the class to load.
* @return void
*/
public function loadClass($className)
{
if (null === $this->_namespace || $this->_namespace.$this->_namespaceSeparator === substr($className, 0, strlen($this->_namespace.$this->_namespaceSeparator))) {
$fileName = '';
$namespace = '';
if (false !== ($lastNsPos = strripos($className, $this->_namespaceSeparator))) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace($this->_namespaceSeparator, DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . $this->_fileExtension;

require ($this->_includePath !== null ? $this->_includePath . DIRECTORY_SEPARATOR : '') . $fileName;
}
}
}
2 changes: 1 addition & 1 deletion examples/buffer.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

Analog::handler (Analog\Handler\Buffer::init (
Analog\Handler\Mail::init (
Expand Down
2 changes: 1 addition & 1 deletion examples/default.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

Analog::log ('foo');
Analog::log ('bar');
Expand Down
2 changes: 1 addition & 1 deletion examples/file.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

$log_file = 'log.txt';

Expand Down
2 changes: 1 addition & 1 deletion examples/firephp.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

Analog::handler (Analog\Handler\FirePHP::init ());

Expand Down
2 changes: 1 addition & 1 deletion examples/levelbuffer.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

Analog::handler (Analog\Handler\LevelBuffer::init (
Analog\Handler\Mail::init (
Expand Down
2 changes: 1 addition & 1 deletion examples/mail.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

Analog::handler (Analog\Handler\Mail::init (
'you@example.com',
Expand Down
2 changes: 1 addition & 1 deletion examples/mongo.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

Analog::handler (Analog\Handler\Mongo::init (
'localhost:27017',
Expand Down
2 changes: 1 addition & 1 deletion examples/multi.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

$errors = "Errors:\n";
$warnings = "Warnings:\n";
Expand Down
2 changes: 1 addition & 1 deletion examples/null.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

Analog::handler (Analog\Handler\Null::init ());

Expand Down
2 changes: 1 addition & 1 deletion examples/post.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

$log = '';

Expand Down
19 changes: 19 additions & 0 deletions examples/psr-0.php
@@ -0,0 +1,19 @@
<?php

require 'SplClassLoader.php';

$loader = new SplClassLoader ('Analog', '../lib');
$loader->register ();

use \Analog\Analog;

$log = '';

Analog::handler (\Analog\Handler\Variable::init ($log));

Analog::log ('Test one');
Analog::log ('Test two');

echo $log;

?>
2 changes: 1 addition & 1 deletion examples/stderr.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

Analog::handler (Analog\Handler\Stderr::init ());

Expand Down
2 changes: 1 addition & 1 deletion examples/syslog.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

Analog::handler (Analog\Handler\Syslog::init ('analog', 'user'));

Expand Down
2 changes: 1 addition & 1 deletion examples/variable.php
@@ -1,6 +1,6 @@
<?php

require '../Analog.php';
require '../lib/Analog.php';

$log = '';

Expand Down
34 changes: 34 additions & 0 deletions lib/Analog.php
@@ -0,0 +1,34 @@
<?php

/**
* Register a very simple autoloader for the pre-built handlers
* based on the current working directory.
*/
spl_autoload_register (function ($class) {
$file = str_replace ('\\', DIRECTORY_SEPARATOR, ltrim ($class, '\\')) . '.php';
if (file_exists (__DIR__ . DIRECTORY_SEPARATOR . $file)) {
require_once $file;
return true;
}
return false;
});

/**
* We simply extend the main class so that Analog is
* available as a global class. This saves us adding
* `use \Analog\Analog` at the top of every file,
* or worse, typeing `\Analog\Analog::log()` everywhere.
*/
class Analog extends \Analog\Analog {
/**
* We need to override format() to always write to
* the parent, since the pre-built handlers have to
* assume they're using the PSR-0 class.
*/
public static function format ($format = false) {
if ($format) {
\Analog\Analog::$format = $format;
}
return \Analog\Analog::$format;
}
}

0 comments on commit 618b939

Please sign in to comment.