Skip to content

Commit

Permalink
Hello world.
Browse files Browse the repository at this point in the history
  • Loading branch information
itsgoingd committed Jun 21, 2013
0 parents commit dc7b6e4
Show file tree
Hide file tree
Showing 18 changed files with 1,418 additions and 0 deletions.
18 changes: 18 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# osx noise
.DS_Store
profile

# xcode noise
build/*
*.mode1
*.mode1v3
*.mode2v3
*.perspective
*.perspectivev3
*.pbxuser
*.xcworkspace
xcuserdata

# svn & cvs
.svn
CVS
108 changes: 108 additions & 0 deletions Clockwork.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
namespace Clockwork;

use Clockwork\DataSource\DataSourceInterface;
use Clockwork\Request\Request;
use Clockwork\Storage\StorageInterface;

/**
* Main Clockwork class
*/
class Clockwork
{
/**
* Array of data sources, these objects provide data to be stored in a request object
*/
protected $dataSources = array();

/**
* Request object, data structure which stores data about current application request
*/
protected $request;

/**
* Storage object, provides implementation for storing and retrieving request objects
*/
protected $storage;

/**
* Create a new Clockwork instance with default request object
*/
public function __construct()
{
$this->request = new Request();
}

/**
* Add a new data source
*/
public function addDataSource(DataSourceInterface $dataSource)
{
$this->dataSources[] = $dataSource;

return $this;
}

/**
* Return array of all added data sources
*/
public function getDataSources()
{
return $this->dataSources;
}

/**
* Return the request object
*/
public function getRequest()
{
return $this->request;
}

/**
* Set a custom request object
*/
public function setRequest(Request $request)
{
$this->request = $request;

return $this;
}

/**
* Add data from all data soruces to request
*/
public function resolveRequest()
{
foreach ($this->dataSources as $dataSource)
$dataSource->resolve($this->request);

return $this;
}

/**
* Store request via storage object
*/
public function storeRequest()
{
return $this->storage->store($this->request);
}

/**
* Return the storage object
*/
public function getStorage()
{
return $this->storage;
}

/**
* Set a custom storage object
*/
public function setStorage(StorageInterface $storage)
{
$this->storage = $storage;

return $this;
}
}
47 changes: 47 additions & 0 deletions DataSource/DataSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php
namespace Clockwork\DataSource;

use Clockwork\Request\Request;

/**
* Base data source class
*/
class DataSource implements DataSourceInterface
{
/**
* Adds data to the request and returns it, custom implementation should be provided in child classes
*/
public function resolve(Request $request)
{
return $request;
}

/**
* Replaces unserializable items such as closures, resources and objects in an array with textual representation
*/
protected function replaceUnserializable(array $data)
{
foreach ($data as &$item) {
if ($item instanceof Closure)
$item = 'anonymous function';
elseif (is_resource($item))
$item = 'resource';
elseif (is_object($item))
$item = 'instance of ' . get_class($item);
}

return $data;
}

/**
* Censors passwords in an array, identified by key containing "pass" substring
*/
protected function removePasswords(array $data)
{
foreach ($data as $key => &$val)
if (strpos($key, 'pass') !== false)
$val = '*removed*';

return $data;
}
}
15 changes: 15 additions & 0 deletions DataSource/DataSourceInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace Clockwork\DataSource;

use Clockwork\Request\Request;

/**
* Data source interface, all data sources must implement this interface
*/
interface DataSourceInterface
{
/**
* Adds data to the request and returns it
*/
public function resolve(Request $request);
}
76 changes: 76 additions & 0 deletions DataSource/EloquentDataSource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php
namespace Clockwork\DataSource;

use Clockwork\Request\Request;

use Illuminate\Database\Connection;

/**
* Data source for Eloquent (Laravel 4 ORM), provides database queries
*/
class EloquentDataSource extends DataSource
{
/**
* Database connection for which the queries are retrieved
*/
protected $connection;

/**
* Create a new data source instance, takes a database connection as an argument
*/
public function __construct(Connection $connection)
{
$this->connection = $connection;
}

/**
* Adds ran database queries to the request
*/
public function resolve(Request $request)
{
$request->databaseQueries = array_merge($request->databaseQueries, $this->getDatabaseQueries());

return $request;
}

/**
* Takes a query and array of bindings as arguments, returns runnable query with upper-cased keywords
*/
protected function createRunnableQuery($query, $bindings)
{
# add bingins to query
$bindings = $this->connection->prepareBindings($bindings);

foreach ($bindings as $binding) {
$binding = $this->connection->getPdo()->quote($binding);

$query = preg_replace('/\?/', $binding, $query, 1);
}

# highlight keywords
$keywords = array('select', 'insert', 'update', 'delete', 'where', 'from', 'limit', 'is', 'null', 'having', 'group by', 'order by', 'asc', 'desc');
$regexp = '/\b' . implode('\b|\b', $keywords) . '\b/i';

$query = preg_replace_callback($regexp, function($match){
return strtoupper($match[0]);
}, $query);

return $query;
}

/**
* Returns an array of runnable queries and their durations from a database connection
*/
protected function getDatabaseQueries()
{
$queries = array();

foreach ($this->connection->getQueryLog() as $query)
$queries[] = array(
'query' => $this->createRunnableQuery($query['query'], $query['bindings']),
'duration' => $query['time'],
);

return $queries;
}
}
Loading

0 comments on commit dc7b6e4

Please sign in to comment.