Skip to content

Commit

Permalink
initial project state
Browse files Browse the repository at this point in the history
  • Loading branch information
Mark Guinn committed Nov 10, 2014
0 parents commit 699d897
Show file tree
Hide file tree
Showing 10 changed files with 375 additions and 0 deletions.
20 changes: 20 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
License (MIT)
-------------
Copyright (c) 2014 Mark Guinn

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so, subject
to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
64 changes: 64 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
SilverStripe Clockwork Integration
==================================

Integrates the wonderful Clockwork Chrome extension into Silverstripe. Out of
the box queries and controller events will be logged. You can also log your
own events on the timeline.

* Clockwork: https://github.com/itsgoingd/clockwork
* Chrome Extension: https://github.com/itsgoingd/clockwork-chrome

NOTE: This extension is only active when your site is in dev mode. There should
be no slowdown in live mode because the database proxy adapter is not installed
and clockwork is never activated.


Usage
-----
Install the chrome extension. Install via composer. Basic timing and query
logging work out of the box. To add your own events to the timeline use:

```
Injector::inst()->get('ClockworkTimeline')->startEvent('myevent1', 'Description of the event');
// ... do the things ... ///
// this will happen automatically when the request ends
Injector::inst()->get('ClockworkTimeline')->endEvent('myevent1');
```


Todo List
---------
- Integrate SS_Log for logging (Debug::log is too janky at this point)
- Integrate the embeddable web version for non-chrome-users: https://github.com/itsgoingd/clockwork-web


Developer(s)
------------
- Mark Guinn <[email protected]>

Contributions welcome by pull request and/or bug report.
Please follow Silverstripe code standards.


License (MIT)
-------------
Copyright (c) 2014 Mark Guinn

Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
Software, and to permit persons to whom the Software is furnished to do so, subject
to the following conditions:

The above copyright notice and this permission notice shall be included in all copies
or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
1 change: 1 addition & 0 deletions _config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?php
21 changes: 21 additions & 0 deletions _config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
Name: clockworkconfig
---
Injector:
ClockworkFilter:
class: 'Clockwork\Support\Silverstripe\RequestFilter'
ClockworkTimeline:
class: 'Clockwork\Request\Timeline'
RequestProcessor:
class: RequestProcessor
properties:
filters:
- '%$ClockworkFilter'

Controller:
extensions:
- 'Clockwork\Support\Silverstripe\ClockworkControllerExtension'

Director:
rules:
'__clockwork': 'Clockwork\Support\Silverstripe\ClockworkController'
23 changes: 23 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"name": "markguinn/silverstripe-clockwork",
"description": "Silverstripe extension integrating the Clockwork Chrome extension",
"type": "silverstripe-module",
"keywords": ["silverstripe", "clockwork", "debugging", "logging", "queries", "timeline", "profiling"],
"authors": [
{
"name": "Mark Guinn",
"homepage": "http://github.com/markguinn",
"role": "Developer"
}
],
"require": {
"silverstripe/framework": "~3.1",
"itsgoingd/clockwork": "~1.6"
},
"extra": {
"installer-name": "clockwork",
"branch-alias": {
"dev-master": "1.0.x-dev"
}
}
}
28 changes: 28 additions & 0 deletions src/ClockworkController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php
/**
* This is the endpoint that serves json to the extension
*
* @author Mark Guinn <[email protected]>
* @date 11.07.2014
* @package clockwork
*/

namespace Clockwork\Support\Silverstripe;
use Clockwork\Storage\FileStorage;
use Controller;
use SS_HTTPRequest;

class ClockworkController extends Controller
{
private static $allowed_actions = array('retrieve');
private static $url_handlers = array('$ID!' => 'retrieve');

public function retrieve(SS_HTTPRequest $request) {
$storage = new FileStorage(TEMP_FOLDER . '/clockwork');
$data = $storage->retrieve($request->param('ID'));
$response = $this->getResponse();
$response->addHeader('Content-type', 'application/json');
$response->setBody($data->toJson());
return $response;
}
}
38 changes: 38 additions & 0 deletions src/ClockworkControllerExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* Hooks into any controller to provide some amount of timeline data.
*
* @author Mark Guinn <[email protected]>
* @date 11.07.2014
* @package salveo
* @subpackage clockwork
*/

namespace Clockwork\Support\Silverstripe;

use Extension;
use Injector;
use Director;

class ClockworkControllerExtension extends Extension
{
public function onBeforeInit() {
if (Director::isDev()) {
Injector::inst()->get('ClockworkTimeline')->startEvent(
get_class($this->owner) . '_init',
get_class($this->owner) . ' initialization'
);
}
}

public function onAfterInit() {
if (Director::isDev()) {
$injector = Injector::inst();
$injector->get('ClockworkTimeline')->endEvent(get_class($this->owner) . '_init');
$injector->get('ClockworkTimeline')->startEvent(
get_class($this->owner) . '_action',
get_class($this->owner) . ' action ' . $this->owner->getRequest()->param('Action')
);
}
}
}
68 changes: 68 additions & 0 deletions src/DatabaseProxy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php
/**
* Wraps the real database adapter, passing on most function calls
* and logging queries for sending along to Clockwork
*
* @author Mark Guinn <[email protected]>
* @date 11.07.2014
* @package clockwork
*/

namespace Clockwork\Support\Silverstripe;

use SS_Query;
use SS_Database;

class DatabaseProxy
{
/** @var SS_Database */
protected $realConn;

/** @var array */
protected $queries;


/**
* @param SS_Database $realConn
*/
public function __construct($realConn) {
$this->realConn = $realConn;
$this->queries = array();
}


/**
* Execute the given SQL query.
* This function must be defined by subclasses as part of the actual implementation.
* It should return a subclass of SS_Query as the result.
* @param string $sql The SQL query to execute
* @param int $errorLevel The level of error reporting to enable for the query
* @return SS_Query
*/
public function query($sql, $errorLevel = E_USER_ERROR) {
$starttime = microtime(true);
$handle = $this->realConn->query($sql, $errorLevel);
$endtime = microtime(true);
$this->queries[] = array('query' => $sql, 'duration' => round(($endtime - $starttime) * 1000.0, 2));
return $handle;
}


/**
* @return array
*/
public function getQueries() {
return $this->queries;
}


/**
* @param string $name
* @param array $arguments
* @return mixed
*/
public function __call($name, $arguments) {
return call_user_func_array(array($this->realConn, $name), $arguments);
}

}
69 changes: 69 additions & 0 deletions src/RequestFilter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
<?php
/**
* Initializes Clockwork at the beginning of a request and
* finalizes it at the end, adding the correct headers to
* the response.
*
* @author Mark Guinn <[email protected]>
* @date 11.07.2014
* @package clockwork
*/
namespace Clockwork\Support\Silverstripe;

use Clockwork\Clockwork;
use Clockwork\DataSource\PhpDataSource;
use Clockwork\Storage\FileStorage;
use DataModel;
use Session;
use SS_HTTPRequest;
use SS_HTTPResponse;
use Director;
use DB;

class RequestFilter implements \RequestFilter
{
protected $clockwork;

/**
* Filter executed before a request processes
*
* @param SS_HTTPRequest $request Request container object
* @param Session $session Request session
* @param DataModel $model Current DataModel
* @return boolean Whether to continue processing other filters. Null or true will continue processing (optional)
*/
public function preRequest(SS_HTTPRequest $request, Session $session, DataModel $model) {
if (Director::isDev()) {
$this->clockwork = new Clockwork();

// Wrap the current database adapter in a proxy object so we can log queries
DB::setConn(new DatabaseProxy(DB::getConn()));
$this->clockwork->addDataSource(new SilverstripeDataSource());

// Attach a default datasource that comes with
// the Clockwork library (grabs session info, etc)
$this->clockwork->addDataSource(new PhpDataSource());

// Give it a place to store data
$this->clockwork->setStorage(new FileStorage(TEMP_FOLDER . '/clockwork'));
}
}


/**
* Filter executed AFTER a request
*
* @param SS_HTTPRequest $request Request container object
* @param SS_HTTPResponse $response Response output object
* @param DataModel $model Current DataModel
* @return boolean Whether to continue processing other filters. Null or true will continue processing (optional)
*/
public function postRequest(SS_HTTPRequest $request, SS_HTTPResponse $response, DataModel $model) {
if (Director::isDev()) {
$response->addHeader("X-Clockwork-Id", $this->clockwork->getRequest()->id);
$response->addHeader("X-Clockwork-Version", Clockwork::VERSION);
$this->clockwork->resolveRequest();
$this->clockwork->storeRequest();
}
}
}
43 changes: 43 additions & 0 deletions src/SilverstripeDataSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/**
* Adds timing and query information from silverstripe to the request.
* One might add more information here in the future. I could imagine
* it being helpful to enumerate config data or caching or routing or
* tap into the silverstripe session/input classes instead of using
* the default phpDataSource.
*
* @author Mark Guinn <[email protected]>
* @date 11.08.2014
* @package clockwork
*/

namespace Clockwork\Support\Silverstripe;

use Clockwork\DataSource\DataSource;
use Clockwork\Request\Request;
use DB;
use Injector;

class SilverstripeDataSource extends DataSource
{
/**
* The entry-point. called by Clockwork itself.
* @param Request $request
* @return \Clockwork\Request\Request
*/
function resolve(Request $request)
{
// Retrieve the timeline
$timeline = Injector::inst()->get('ClockworkTimeline');
$timeline->finalize();
$request->timelineData = $timeline->toArray();

// Retrieve the query log
$db = DB::getConn();
if ($db instanceof DatabaseProxy) {
$request->databaseQueries = $db->getQueries();
}

return $request;
}
}

0 comments on commit 699d897

Please sign in to comment.