Permalink
Browse files

Nouveau système url

  • Loading branch information...
Sylvain Frigui Sylvain Frigui
Sylvain Frigui authored and Sylvain Frigui committed May 7, 2014
1 parent b780769 commit f33c1c444490c34ee4fc5fb6ba4bc43259abcfb7
@@ -8,7 +8,7 @@
* @access public
* @link http://www.cafecentral.fr/fr/wiki
*/
-class app
+abstract class _apps
{
protected $env;
protected $key;
@@ -27,25 +27,27 @@ class app
* @param string only site
* @access public
*/
- public function __construct($key, $template = 'default', $params = null, $env = env)
+ public function __construct($template = 'default', $params = null, $env = env)
{
- $this->key = (!empty($key)) ? $key : trigger_error('Your <strong>$key param</strong> is empty, new app() will not work', E_USER_WARNING);
- $this->template = $template;
+ $this->key = mb_substr(mb_strtolower(get_called_class()), 3);
+
+ // $this->key = (!empty($key)) ? $key : trigger_error('Your <strong>$key param</strong> is empty, app() will not work', E_USER_WARNING);
+ $this->template = (mb_strpos($template, '/') === 0) ? $template : '/'.$template;
$this->env = $env;
- $this->system_root = ADMIN_ROOT.'/'.$key.'/'.boot::app_system_dir.'/';
- $this->system_url = '/'.boot::admin_dir.'/'.$key.'/'.boot::app_system_dir.'/';
+ $this->system_root = ADMIN_ROOT.'/'.$this->key.'/'.boot::app_system_dir;
+ $this->system_url = '/'.boot::admin_dir.'/'.$this->key.'/'.boot::app_system_dir;
- $this->template_root['admin'] = ADMIN_ROOT.'/'.$key.'/'.boot::app_template_dir.'/';
- $this->template_root['site'] = SITE_ROOT.'/'.$key.'/';
+ $this->template_root['admin'] = ADMIN_ROOT.'/'.$this->key.'/'.boot::app_template_dir;
+ $this->template_root['site'] = SITE_ROOT.'/'.$this->key;
- $this->template_url['admin'] = '/'.boot::admin_dir.'/'.$key.'/'.boot::app_template_dir.'/';
- $this->template_url['site'] = '/'.boot::site_dir.'/'.SITE_KEY.'/'.$key.'/';
+ $this->template_url['admin'] = '/'.boot::admin_dir.'/'.$this->key.'/'.boot::app_template_dir;
+ $this->template_url['site'] = '/'.boot::site_dir.'/'.SITE_KEY.'/'.$this->key;
$this->get_ini();
$this->set_default_param();
$this->set_param($params);
- }
+ }
/**
*
*
@@ -182,9 +184,9 @@ public function __tostring()
$_APP = &$this;
// HACK pour test des fichiers d'initialisation des paramtres de l'app
// pour exemple voir le fichier launcher de l'app form
- if (is_file($this->get_systemroot().'/app.prepare.php'))
+ if (method_exists($this, 'prepare'))
{
- include($this->get_systemroot().'/app.prepare.php');
+ $this->prepare();
}
$_PARAM = &$this->param;
// on prépare la vue
@@ -216,7 +218,7 @@ public function load()
{
foreach ($this->ini['dependencies']['app'] as $key)
{
- $app = new app($key);
+ $app = app($key);
$app->load();
}
}
@@ -258,8 +260,8 @@ public function bind_snippet($zone, $snippet_key)
{
$_APP = $this;
// print'<pre>';print_r($data);print'</pre>';
- $routine = $this->get_templateroot().$snippet_key.'.php';
- $template = $this->get_templateroot().$snippet_key.'.snippet.php';
+ $routine = $this->get_templateroot().'/'.$snippet_key.'.php';
+ $template = $this->get_templateroot().'/'.$snippet_key.'.snippet.php';
// on ouvre le tampon
ob_start();
// on charge les données à afficher
@@ -281,6 +283,7 @@ public function bind_file($zone, $file, $system = false)
{
if (filter_var($file, FILTER_VALIDATE_URL) === false)
{
+ $file = (mb_strpos($file, '/') === 0) ? $file : '/'.$file;
if ($system === false)
{
$app = $this->get_templateurl();
@@ -396,5 +399,73 @@ public function get_templates($type = null, $env = null)
$templates = $filterFile($this->get_templateroot($env));
return (empty($templates)) ? false : $templates;
}
+
+/**
+ * Routine de préparation des apps
+ *
+ * @param mixed la valeur à lire dans le registre. Vous pouvez mettre autant d'arguments que vous le souhaitez.
+ * @access protected
+ */
+ public static function register()
+ {
+ $cache = new app('cache');
+ $fileCache = $cache->get_templateroot().'/registry/'.md5('app');
+ // dans le cache
+ if (is_file($fileCache) && filemtime(ADMIN_ROOT) < filemtime($fileCache))
+ {
+ // print'<pre>';print_r('dans le cache des apps');print'</pre>';
+ $datas = unserialize(file_get_contents($fileCache));
+ }
+ // création du cache
+ else
+ {
+ //print'<pre>';print_r('génération du cache des apps');print'</pre>';
+ $root = new dir(ADMIN_ROOT);
+
+ $root->get();
+ $classes = array();
+
+ foreach ($root as $dir)
+ {
+ $key = $dir->get_key();
+ $app = new app($key);
+ $files = $app->get_ini('system');
+ // on charge automatiquement les librairies
+ if (isset($files['lib']))
+ {
+ foreach ($files['lib'] as $file)
+ {
+ $libs[] = $app->get_systemroot().$file;
+ }
+ }
+ // préparation du tableau pour la mise en registre des classes pour l'autoloader
+ if (isset($files['class']))
+ {
+ foreach ($files['class'] as $file)
+ {
+ preg_match('/([a-z0-9A-Z_-]*).php/u', $file, $class);
+ if (isset($class[1])) $classes[$class[1]] = $app->get_key();
+ }
+ }
+ // préparation du tableau pour la mise en registre des apps
+ $apps[$key] = $app;
+ }
+
+ $datas[registry::app_index] = $apps;
+ $datas[registry::class_index] = $classes;
+ $datas['libraries'] = $libs;
+ // mise en cache
+ file_put_contents($fileCache, serialize($datas));
+ }
+ // mise en registre des apps
+ registry::set(registry::app_index, $datas[registry::app_index]);
+ // mise en registre du nom des classes pour l'autload
+ registry::set(registry::class_index, $datas[registry::class_index]);
+ // chargement des librairies
+ foreach ($datas['libraries'] as $file)
+ {
+ require_once($file);
+ }
+ }
}
?>
@@ -0,0 +1,41 @@
+<?php
+/**
+ * App handling
+ *
+ * @package Core
+ * @author Michaël V. Dandrieux <mvd@cafecentral.fr>
+ * @author Sylvain Frigui <sf@cafecentral.fr>
+ * @access public
+ * @link http://www.cafecentral.fr/fr/wiki
+ */
+class app extends _apps
+{
+
+/**
+ * Class constructor (Don't forget it is an abstract class)
+ *
+ * @param mixed une id, une clé ou un tableau array('id' => 2)
+ * @param string only site
+ * @access public
+ */
+ public function __construct($key, $template = 'default', $params = null, $env = env)
+ {
+ $this->key = (!empty($key)) ? $key : trigger_error('Your <strong>$key param</strong> is empty, app() will not work', E_USER_WARNING);
+ $this->template = (mb_strpos($template, '/') === 0) ? $template : '/'.$template;
+ $this->env = $env;
+
+ $this->system_root = ADMIN_ROOT.'/'.$this->key.'/'.boot::app_system_dir;
+ $this->system_url = '/'.boot::admin_dir.'/'.$this->key.'/'.boot::app_system_dir;
+
+ $this->template_root['admin'] = ADMIN_ROOT.'/'.$this->key.'/'.boot::app_template_dir;
+ $this->template_root['site'] = SITE_ROOT.'/'.$this->key;
+
+ $this->template_url['admin'] = '/'.boot::admin_dir.'/'.$this->key.'/'.boot::app_template_dir;
+ $this->template_url['site'] = '/'.boot::site_dir.'/'.SITE_KEY.'/'.$this->key;
+
+ $this->get_ini();
+ $this->set_default_param();
+ $this->set_param($params);
+ }
+}
+?>
@@ -30,7 +30,7 @@ public function set($data)
*/
public function get()
{
- if (empty($this->data)) $this->data = $_SESSION['user']['id'];
+ if (empty($this->data)) $this->data = $_SESSION['user']['id']->get();
return $this->data;
}
/**
@@ -10,7 +10,6 @@
class attrRel extends _attrs implements ArrayAccess, Iterator
{
const table = '_rel';
- protected $item;
protected $data = array();
/**
* Set array attribute
@@ -92,7 +91,9 @@ public function delete($rel)
*/
public function attach(_items $item)
{
- $this->item = $item;
+ $this->params['env'] = $item->get_env();
+ $this->params['table'] = $item->get_table();
+ $this->params['id'] = $item['id']->get();
}
/**
* Set attribute
@@ -150,7 +151,7 @@ public function __tostring()
*/
public function unfold($params = null)
{
- $bunch = new bunch(null, null, $this->item->get_env());
+ $bunch = new bunch(null, null, $this->params['env']);
$bunch->get_by_nickname($this->data, $params);
@@ -44,15 +44,30 @@ public function set($data)
$this->data = (string) $data;
return $this;
}
+/**
+ * xxxx
+ *
+ * @param string la variable
+ * @return string une string
+ * @access public
+ */
+ public function set_reader($value)
+ {
+ $this->params['reader'] = $value;
+ $this->params['table'] = registry::get(registry::reader_index, $value, 'url');
+ }
/**
* Get complete item url
*
* @return string url
* @access public
*/
- public function attach(_items $item)
+ public function attach(_items &$item)
{
- $this->item = $item;
+ $this->params['table'] = $item->get_table();
+ $this->params['env'] = $item->get_env();
+ $this->params['version'] = (isset($item['version']) && !$item['version']->is_empty()) ? $item['version'] : null;
+ // print'<pre>';print_r(registry::get_constants());print'</pre>';
}
/**
* php http_build_query() on url
@@ -77,9 +92,31 @@ public function args($arg)
*/
public function __tostring()
{
- $readerUrl = ($this->item->get_table() == 'page') ? '' : registry::get(registry::reader_index, $this->item->get_table(), 'url');
- $readerUrl = ($readerUrl == '/') ? '' : $readerUrl;
- return constant(mb_strtoupper($this->item->get_env()).'_URL').$readerUrl.$this->get();
+ $url = '';
+ // version url
+ if (is_null($this->params['version']))
+ {
+ $url = cc($this->params['env'], current)['version']->get_url();
+ }
+ else
+ {
+ $version = constant(mb_strtoupper($this->params['env']).'_VERSION');
+ $url = constant('VERSION_'.mb_strtoupper($version)).$url;
+ }
+ // reader
+ if ($this->params['table'] != 'page')
+ {
+ foreach (registry::get(registry::reader_index) as $page => $tables)
+ {
+ if (in_array($this->params['table'], $tables))
+ {
+ $url = registry::get(registry::url_index, $page);
+ break;
+ }
+ }
+ }
+ // retunr
+ return $url.$this->get();
}
/**
* Definition mysql
@@ -10,14 +10,16 @@
class attrVersion extends _attrs
{
/**
- * Get complete item url
+ * Set attribute
*
- * @return string url
+ * @param stringd la variable
+ * @return string une string
* @access public
*/
- public function attach(_items $item)
+ public function unfold()
{
- $this->item = $item;
+ $v = cc('version', $this->data);
+ return $v;
}
/**
* Set attribute
@@ -42,7 +44,7 @@ public function database_get()
{
if (empty($this->data))
{
- $this->data = cc($this->item->get_env(), current)['version'];
+ $this->data = cc($this->item->get_env(), current)['version']->get();
}
return $this->data;
}
@@ -52,18 +52,21 @@ private function _set_key()
public function get($recursive = false)
{
$dir = dir($this->root);
- while($entry = $dir->read())
+ if (!empty($dir))
{
- $type = filetype($this->root.'/'.$entry);
- if (!in_array($entry, self::$nogood))
+ while($entry = $dir->read())
{
- $path = $this->root.'/'.$entry;
- $obj = ($type === 'dir') ? new dir($path) : new file($path);
- if ($recursive === true && get_class($obj) === __CLASS__) $obj->get($recursive);
- $this->data[$obj->get_key()] = $obj;
+ $type = filetype($this->root.'/'.$entry);
+ if (!in_array($entry, self::$nogood))
+ {
+ $path = $this->root.'/'.$entry;
+ $obj = ($type === 'dir') ? new dir($path) : new file($path);
+ if ($recursive === true && get_class($obj) === __CLASS__) $obj->get($recursive);
+ $this->data[$obj->get_key()] = $obj;
+ }
}
+ $dir->close();
}
- $dir->close();
return $this->data;
}
@@ -191,5 +191,15 @@ public function get_ip()
$this->ip = getenv('REMOTE_ADDR');
}
}
+/**
+ * Check whether an item exists in base or not
+ *
+ * @return bool true ou false
+ * @access public
+ */
+ public function exists()
+ {
+ return (!$this->data['id']->is_empty() && $this->data['key']->get() != 'anonymous') ? true : false;
+ }
}
?>
Oops, something went wrong.

0 comments on commit f33c1c4

Please sign in to comment.