SitePoint Sponsor |
|
User Tag List
Results 1 to 5 of 5
Thread: Opinions on Datastore class
-
Jun 1, 2009, 21:56 #1
- Join Date
- Jan 2008
- Location
- Knoxville TN
- Posts
- 2,053
- Mentioned
- 66 Post(s)
- Tagged
- 1 Thread(s)
Opinions on Datastore class
Submitted for comment is a datastoring class. As my framework has grown I've been looking for ways to speed it up. One is in how class and template discover is handled. This class assembles the collection of paths for the autoload function and for the framework's custom template loading function. Then if the caching system is turned on (set by a constant defined in the configuration of the site) it writes the cache file. Hence in a production environment this class isn't called at all.
PHP Code:<?php
/**
* Datastore class. Creates the datastore - cache of class
* and template paths and then writes the file to cache.
*/
class PAMWF_Datastore {
public function __construct() {
$this->setAllPaths();
if (PAMWF_CACHE) {
$this->cache();
}
}
/**
* Set All Path collections
*/
protected function setAllPaths() {
$this->setPaths('classPaths',
PAMWF_ROOT.DIRECTORY_SEPARATOR.'classes',
PAMWF_PROJECT.DIRECTORY_SEPARATOR.'classes' );
$this->setPaths('templatePaths',
PAMWF_ROOT.DIRECTORY_SEPARATOR.'templates',
PAMWF_PROJECT.DIRECTORY_SEPARATOR.'templates' );
}
/**
* Setup the path collections. For now this is for
* classes and template libraries.
*
* @param string $path
*/
protected function setPaths( $collection ) {
$ext = ($collection == 'templatePaths') ? 'phtml' : 'php';
foreach ( func_get_args() as $path ) {
if ($path == $collection) {
continue;
}
if (is_array($path)) {
$this->setPaths( $collection, $path );
}
else {
if (!is_dir($path)) {
throw new PAMWF_FatalException('Invalid Path: ' . $path );
}
if ( $files = glob($path . "/*.$ext")) {
foreach ($files as $file) {
if (is_dir($file)) {
continue;
}
PAMWF::${$collection}[pathinfo($file, PATHINFO_FILENAME)] = $file;
}
}
if ( $paths = glob($path . "/*", GLOB_ONLYDIR) ) {
foreach ($paths as $dir) {
$this->setPaths( $collection, $dir );
}
}
}
}
}
/**
* Cache the discovered collection of paths for later use.
*/
protected function cache() {
$output = '<?php PAMWF::$classPaths = array ('."\n";
foreach (PAMWF::$classPaths as $key => $value) {
$output .= "'$key' => '$value',\n";
}
$output .= ");\n\n".'PAMWF::$templatePaths = array('."\n";
foreach (PAMWF::$templatePaths as $key => $value) {
$output .= "'$key' => '$value',\n";
}
$output .= '); ?>';
file_put_contents(PAMWF_PROJECT.'/cache/datastore.php', $output);
}
}
?>
I considered removing this overwriting feature from the class area entirely but left it in in the event it might be useful.
I know the cache function can be refactored but other than that I'm out of ideas on how to tighten this beyond the fact that I can avoid it's use entirely in production via the cache it creates.
For reference here are the autoload and template functions that make use of the output of this class -- note that they are in two different classes.
PHP Code:<?php
/* Taken from class PAMWF */
public static function autoload( $className ) {
if (array_key_exists($className, PAMWF::$classPaths)) {
require_once(PAMWF::$classPaths[$className]);
return true;
}
else {
return false;
}
}
/* Taken from class PAMWF_Controller */
protected function findTemplate($template) {
if (array_key_exists($template, PAMWF::$templatePaths)) {
return PAMWF::$templatePaths[$template];
}
else {
throw new PAMWF_Exception('Unable to find template '.$template);
}
}
/* Example of a call to the findTemplate function */
include( $this->findTemplate('myTemplate'));
?>
-
Jun 2, 2009, 00:24 #2
- Join Date
- Mar 2007
- Location
- Czech Republic
- Posts
- 375
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
PHP Code:$output = '<?php PAMWF::$classPaths = array ('."\n";
....
-
Jun 2, 2009, 00:50 #3
- Join Date
- Jan 2008
- Location
- Knoxville TN
- Posts
- 2,053
- Mentioned
- 66 Post(s)
- Tagged
- 1 Thread(s)
Simpler? yes. Faster? no. As executable PHP code the datastore can be memcached after first eval. Speed is the first consideration in this particular code block.
-
Jun 2, 2009, 01:18 #4
- Join Date
- Mar 2007
- Location
- Czech Republic
- Posts
- 375
- Mentioned
- 0 Post(s)
- Tagged
- 0 Thread(s)
According to manual are arrays stored in memcache in serialized form ...
http://www.php.net/manual/en/function.memcache-add.php
-
Jun 2, 2009, 01:21 #5
- Join Date
- May 2006
- Location
- Lancaster University, UK
- Posts
- 7,062
- Mentioned
- 2 Post(s)
- Tagged
- 0 Thread(s)
Well, in that case why not use a function made for this: Var_Export():
PHP Code:<?php
protected function cache() {
File_Put_Contents(PAMWF_PROJECT.'/cache/datastore.php', SPrintF('<?php
PAMWF::$classPaths = %s;
PAMWF::$templatePaths = %s;
?>', Var_Export(PAMWF::$classPaths, true), Var_Export(PAMWF::$templatePaths, true)));
}Jake Arkinstall
"Sometimes you don't need to reinvent the wheel;
Sometimes its enough to make that wheel more rounded"-Molona
Bookmarks