Skip to content

Commit

Permalink
Created an example project. Updated README to reflect the recent
Browse files Browse the repository at this point in the history
namespacing changes and updated installation instructions as well. Fixed
a problem in `ProfileLogRoute` if there is no db connection available.
  • Loading branch information
shiki committed Nov 15, 2013
1 parent e3fde40 commit 4fac0d4
Show file tree
Hide file tree
Showing 20 changed files with 1,186 additions and 46 deletions.
124 changes: 79 additions & 45 deletions README.md
@@ -1,67 +1,80 @@
FirePHP extension for Yii Framework
============================================

This extension contains 2 log route classes. The first,
[SFirePHPLogRoute](https://github.com/shiki/yii-firephp/blob/master/SFirePHPLogRoute.php) processes
standard Yii log messages. The second,
[SFirePHPProfileLogRoute](https://github.com/shiki/yii-firephp/blob/master/SFirePHPProfileLogRoute.php)
processes profile summaries. Both classes send all output to FirePHP. The classes work similarly
to [CWebLogRoute](http://www.yiiframework.com/doc/api/1.1/CWebLogRoute)
and [CProfileLogRoute](http://www.yiiframework.com/doc/api/1.1/CProfileLogRoute).
# FirePHP extension for Yii Framework

This extension contains 2 log route classes. The first, `SK\Yii\FirePHP\LogRoute`, processes
standard Yii log messages. The second, `SK\Yii\FirePHP\ProfileLogRoute`, processes profile summaries.
Both classes send all output to [FirePHP](http://www.firephp.org/). The classes work similarly
to [`CWebLogRoute`](http://www.yiiframework.com/doc/api/1.1/CWebLogRoute)
and [`CProfileLogRoute`](http://www.yiiframework.com/doc/api/1.1/CProfileLogRoute).
The only major difference is the target output.

An advantage of using this extension is that logging and profiling work even through AJAX
requests. Logging of arrays is also possible.
An advantage of using this extension is that logging and profiling work even through AJAX requests.

![](http://shiki.me/public/github/yii-firephp/example.png)

## Requirements

* PHP 5.4+
* A Yii Framework `1.1.14+` project.
* Firebug and FirePHP plugins for Firefox. See http://firephp.org. Firebug's _Console_ and
_Net_ tabs have to be enabled for this to work.
* Set `output_buffering` setting to true in `php.ini`. You might also want to increase the buffer
size to allow large log sizes.

This extension currently supports FirePHPCore 0.3.2. Support for FirePHP 1.0 is planned.
## Installation

Requirements
------------
The only supported installation method for now is using [Composer](http://getcomposer.org/).

* A Yii Framework project
* Install Firebug and FirePHP plugins for Firefox. See http://firephp.org.
* Set output_buffering setting to true in php.ini.
1. Put this in your `composer.json` and run `composer update` to install it:

Installation
------------
{
"repositories": [
{ "type": "vcs", "url": "https://github.com/shiki/yii-firephp.git" }
],
"require": {
"shiki/yii-firephp": "dev-master"
}
}

1. Download and extract the contents to a folder under your extensions directory.
This can be "/protected/extensions/firephp".
2. Modify your config file to include the log route classes.

##### config file code (i.e. /protected/config/main.php)
....
This will also automatically install the dependency `firephp/firephp-core`.

'log' => array(
'class' => 'CLogRouter',
'routes' => array(
2. Make sure you have loaded the Composer autoload file (`vendor/autoload.php`) so the libraries
can be accessed in your Yii config file. See the `main.php` config file in the `example` project
on how this can be done.

3. Modify your config file (e.g. `protected/config/main.php`) to include the log route classes.

....

'log' => array(
'class' => 'CLogRouter',
'routes' => array(
// the default (file logger)
array(
'class' => 'CFileLogRoute',
'levels' => 'error, warning',
'class' => 'CFileLogRoute',
'levels' => 'error, warning',
),
// standard log route
array(
'class' => 'ext.firephp.SFirePHPLogRoute',
'levels' => 'error, warning, info, trace',
'class' => '\\SK\\Yii\\FirePHP\\LogRoute',
'levels' => 'error, warning, info, trace',
),
// profile log route
array(
'class' => 'ext.firephp.SFirePHPProfileLogRoute',
'report' => 'summary' // or "callstack"
'class' => '\\SK\\Yii\\FirePHP\\ProfileLogRoute',
'report' => 'summary', // or "callstack"
),
),
),
),

....
....

Standard logging
-----
## Standard logging

Once you've got the extension setup in the config, you can use Yii's logging methods to log messages to FirePHP.

// logging an INFO message (arrays will work and looks awesome in FirePHP)
Yii::log(array('username' => 'Shiki', 'profiles' => array('twidl', 'twitter', 'facebook')), CLogger::LEVEL_INFO);
// logging an INFO message
Yii::log('This is an info message.', CLogger::LEVEL_INFO);

// logging a WARNING message
Yii::log("You didn't setup a profile, are you really a person?", CLogger::LEVEL_WARNING);
Expand All @@ -73,21 +86,24 @@ Once you've got the extension setup in the config, you can use Yii's logging met
Yii::trace('Loading application.user.profiles.ninja', 'application.user.profiles');

// logging an ERROR
Yii::log('We have successfully determined that you are not a person', CLogger::LEVEL_ERROR, 'Any category/label will work');
Yii::log('We have successfully determined that you are not a person',
CLogger::LEVEL_ERROR, 'Any category/label will work');

// If you need to log an array, you can use FirePHP's core methods
FB::warn(array('a' => 'b', 'c' => 'd'), 'an.array.warning');

See more about logging [here](http://www.yiiframework.com/doc/guide/1.1/en/topics.logging).


Profiling
-----
## Profiling

Profiling works by simply using Yii's profiling methods.

Yii::beginProfile('a somewhat slow method');

...
// some function calls here
// more function calls
// more function calls

Yii::beginProfile('nested profile');
// you can also nest profile calls
Expand All @@ -98,4 +114,22 @@ Profiling works by simply using Yii's profiling methods.
You can also profile SQL executions. See more about that and profiling in general
[here](http://www.yiiframework.com/doc/guide/1.1/en/topics.logging#performance-profiling).

-end-

## Example

To try all these out, there's an example project in the `example` folder. To run it:

1. Install the required libraries using Composer.

$ cd example
$ composer install

2. Run with the PHP [built-in webserver](http://php.net/manual/en/features.commandline.webserver.php)

$ cd example/webroot
$ php -S localhost:8000

3. Browse [http://localhost:8000](http://localhost:8000) in Firefox. Make sure first that Firebug is opened and the
Console and Net tabs are enabled. You should be able to see the FirePHP logs in Firebug's console.
If you don't, try refreshing first.

3 changes: 3 additions & 0 deletions example/.gitignore
@@ -0,0 +1,3 @@
vendor
webroot/assets
application/runtime
2 changes: 2 additions & 0 deletions example/Makefile
@@ -0,0 +1,2 @@
run:
cd webroot && php -S localhost:8000
23 changes: 23 additions & 0 deletions example/application/components/Controller.php
@@ -0,0 +1,23 @@
<?php
/**
* Controller is the customized base controller class.
* All controller classes for this application should extend from this base class.
*/
class Controller extends CController
{
/**
* @var string the default layout for the controller view. Defaults to '//layouts/column1',
* meaning using a single column layout. See 'protected/views/layouts/column1.php'.
*/
public $layout='//layouts/column1';
/**
* @var array context menu items. This property will be assigned to {@link CMenu::items}.
*/
public $menu=array();
/**
* @var array the breadcrumbs of the current page. The value of this property will
* be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
* for more details on how to specify this property.
*/
public $breadcrumbs=array();
}
44 changes: 44 additions & 0 deletions example/application/config/main.php
@@ -0,0 +1,44 @@
<?php

require(dirname(__FILE__) . '/../../vendor/autoload.php');

return array(
'basePath' => dirname(__FILE__).DIRECTORY_SEPARATOR.'..',
'name' => 'Yii FirePHP Example',

'preload' => array('log'),

// autoloading model and component classes
'import' => array(
'application.models.*',
'application.components.*',
),

'modules'=>array(

),

// application components
'components' => array(
'log' => array(
'class' => 'CLogRouter',
'routes' => array(
// the default (file logger)
array(
'class' => 'CFileLogRoute',
'levels' => 'error, warning',
),
// standard log route
array(
'class' => '\\SK\\Yii\\FirePHP\\LogRoute',
'levels' => 'error, warning, info, trace',
),
// profile log route
array(
'class' => '\\SK\\Yii\\FirePHP\\ProfileLogRoute',
'report' => 'summary', // or "callstack"
),
),
),
),
);
45 changes: 45 additions & 0 deletions example/application/controllers/SiteController.php
@@ -0,0 +1,45 @@
<?php

class SiteController extends Controller
{
public function actionIndex()
{
// logging an INFO message
Yii::log('This is an info message.', CLogger::LEVEL_INFO);

// logging a WARNING message
Yii::log("You didn't setup a profile, are you really a person?", CLogger::LEVEL_WARNING);

// logging with a CATEGORY (categories are displayed as "labels" in FirePHP -- just an additional info text)
Yii::log('Profile successfully created', CLogger::LEVEL_INFO, 'application.user.profiles');

// tracing simple text
Yii::trace('Loading application.user.profiles.ninja', 'application.user.profiles');

// logging an ERROR
Yii::log('We have successfully determined that you are not a person',
CLogger::LEVEL_ERROR, 'Any category/label will work');

// If you need to log an array, you can use FirePHP's core methods
FB::warn(array('a' => 'b', 'c' => 'd'), 'an.array.warning');

// Profiling

Yii::beginProfile('rendering');

for ($i = 0; $i < 30; $i++)
$this->runProfilingSampleLoop();

$this->render('index');
Yii::endProfile('rendering');
}

private function runProfilingSampleLoop()
{
Yii::beginProfile('dummy method');
for ($i = 0; $i < 100; $i++) {
$a = 1;
}
Yii::endProfile('dummy method');
}
}
Empty file.
6 changes: 6 additions & 0 deletions example/application/views/layouts/column1.php
@@ -0,0 +1,6 @@
<?php /* @var $this Controller */ ?>
<?php $this->beginContent('//layouts/main'); ?>
<div id="content">
<?php echo $content; ?>
</div><!-- content -->
<?php $this->endContent(); ?>
55 changes: 55 additions & 0 deletions example/application/views/layouts/main.php
@@ -0,0 +1,55 @@
<?php /* @var $this Controller */ ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />

<!-- blueprint CSS framework -->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/screen.css" media="screen, projection" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
<![endif]-->

<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/form.css" />

<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>

<body>

<div class="container" id="page">

<div id="header">
<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>
</div><!-- header -->

<div id="mainmenu">
<?php $this->widget('zii.widgets.CMenu', array(
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
),
)); ?>
</div><!-- mainmenu -->
<?php if(isset($this->breadcrumbs)):?>
<?php $this->widget('zii.widgets.CBreadcrumbs', array(
'links' => $this->breadcrumbs,
)); ?><!-- breadcrumbs -->
<?php endif?>

<?php echo $content; ?>

<div class="clear"></div>

<div id="footer">
Copyright &copy; <?php echo date('Y'); ?> by Shiki.<br/>
All Rights Reserved.<br/>
<?php echo Yii::powered(); ?>
</div><!-- footer -->

</div><!-- page -->

</body>
</html>
20 changes: 20 additions & 0 deletions example/application/views/site/index.php
@@ -0,0 +1,20 @@
<?php
/* @var $this SiteController */

$this->pageTitle=Yii::app()->name;
?>

<h1>Welcome to <i><?php echo CHtml::encode(Yii::app()->name); ?></i></h1>

<p>Congratulations! You have successfully created your Yii application.</p>

<p>You may change the content of this page by modifying the following two files:</p>
<ul>
<li>View file: <code><?php echo __FILE__; ?></code></li>
<li>Layout file: <code><?php echo $this->getLayoutFile('main'); ?></code></li>
</ul>

<p>For more details on how to further develop this application, please read
the <a href="http://www.yiiframework.com/doc/">documentation</a>.
Feel free to ask in the <a href="http://www.yiiframework.com/forum/">forum</a>,
should you have any questions.</p>
9 changes: 9 additions & 0 deletions example/composer.json
@@ -0,0 +1,9 @@
{
"repositories": [
{ "type": "vcs", "url": "https://github.com/shiki/yii-firephp.git" }
],
"require": {
"yiisoft/yii": ">= 1.1.14",
"shiki/yii-firephp": "dev-master"
}
}

0 comments on commit 4fac0d4

Please sign in to comment.