/solar/source/miku/Miku/Emerge.php
PHP | 384 lines | 297 code | 47 blank | 40 comment | 28 complexity | fede5aabfe4443e63fe722dd07903025 MD5 | raw file
Possible License(s): MIT
- <?php
- /**
- *
- * Miku_Emerge class.
- *
- * @category Miku_Emerge
- *
- * @package Miku_Emerge General-purpose component helpers.
- *
- * @author
- *
- * @license http://opensource.org/licenses/bsd-license.php BSD
- *
- * @version $Id: Emerge.php
- *
- */
- abstract class Miku_Emerge extends Solar_Base
- {
- protected $_Miku_Emerge = array(
- 'cache' => array(
- 'adapter' => 'Solar_Cache_Adapter_None',
- ),
- 'catalog' => array(
- 'classes' => array('Miku_Self', 'Miku_Service'),
- ),
- 'related' => array(),
- );
-
- public $name = null;
-
- protected $_related = array();
- protected $_default = array();
- protected $_type = null;
- protected $_model;
- protected $_catalog;
-
- protected $_factor = null;
- protected $_factor_class = 'Miku_Emerge_Factor';
-
- protected $_cache;
- protected $_cache_key = null;
- protected $_cache_class = 'Miku_Emerge_Cache';
-
- protected $_view;
- protected $_view_object;
- protected $_tpl = null;
- protected $_view_class = 'Solar_View';
- protected $_helper_class = array();
-
- protected $_errors = array();
- protected $_output = null;
-
- protected function _postConstruct()
- {
- parent::_postConstruct();
-
- $this->_fixCache();
- $this->_cache = Solar::factory($this->_cache_class, array(
- 'cache' => $this->_config['cache'],
- ));
- $this->_cache->setSense($this);
- }
-
-
- public function __set($key, $val)
- {
- throw $this->_exception('ERR_NO_SUCH_PROPERTY', array(
- 'class' => get_class($this),
- 'property' => $key,
- ));
- }
-
- public function __get($key)
- {
- throw $this->_exception('ERR_NO_SUCH_PROPERTY', array(
- 'class' => get_class($this),
- 'property' => $key,
- ));
- }
- /*
- * $sense string
- * $spec array
- */
- public function express($sense,$spec = null)
- {
- if (in_array($sense, $this->_default)) {
- $entry = array();
- $entry['dependency'] = "emerge/" . $sense;
- $key = $this->_cache->entry($entry);
- $result = $this->_cache->fetch($key);
- if ($result !== false) {
- // found some data!
- return $result;
- }
- }
-
- $this->_fixFactor();
- $this->_fixName();
-
- if(! $this->_factor[$sense]) {
- throw $this->_exception('ERR_EXPRESS_NOT_FOUND', array(
- 'class' => get_class($this),
- 'sense' => $sense,
- ));
-
- }
-
- if($spec !== null){
- //only load keys that already exist in factor data
- $this->_factor->load(array($sense => $spec));
- }
- if(isset($this->_factor[$sense]['view'])){
- $this->_view = $this->_factor[$sense]['view'];
- unset($this->_factor[$sense]['view']);
- }else {
- $this->_view = $sense;
- }
-
- $this->_express($spec);
-
- if(is_null($this->_output) && $this->_view){
- $this->_render();
- }
- return $this->_output;
- }
-
- /*
- * $sense string
- */
- protected function _express($spec)
- {
- $factor = $this->_factor[$this->_view];
- if($factor['cache_key'] || $factor['dependency'] ) {
- $this->_cache_key = $this->_cache->entry($factor);
- $result = $this->_cache->fetch($this->_cache_key);
- if ($result !== false) {
- // found some data!
- return $result;
- }
- }
- $this->_setup();
- $method = str_replace('-', ' ', $this->_view);
- $method = ucwords(trim($method));
- $method = 'as' . str_replace(' ', '', $method);
- if (method_exists($this, $method)) {
- $this->_preExpress();
- call_user_func_array(
- array($this, $method),
- $factor
- );
- $this->_postExpress();
- }else {
- return false;
- }
- }
-
- protected function _setup()
- {
- $this->_catalog = Solar::dependency(
- 'Miku_Emerge_Catalog',
- $this->_config['catalog']
- );
- $this->_model = Solar_Registry::get("model_catalog");
- $this->related = $this->_config['related'];
- try {
- $type = Solar_Registry::get('mimic_type');
- }catch (Exception $e) {
- $type = Solar_Config::get('Solar','visit_type');
- Solar_Registry::set('mimic_type', $type);
- }
- $this->_type = $type;
- }
-
- protected function _preExpress()
- {
- }
-
- protected function _render()
- {
- // set up a view object, its template paths,
- // need actions registed view
- if(! $this->_view_object) {
- try {
- $this->_view_object = Solar_Registry::get('view');
- }catch (Exception $e) {
- $this->_view_object = Solar::factory($this->_view_class);
- $this->_addViewHelpers();
- Solar_Registry::set('view', $this->_view_object);
- $this->_view_object->visitor = $this->_catalog->visitor;
- }
- }
- $this->_addViewTemplates();
- $class = get_class($this);
- $this->_view_object->getHelper('getTextRaw')->setClass($class);
- $this->_view_object->assign($this);
- $this->_view_object->errors = $this->_errors;
- $this->_renderView();
- if (!is_null($this->_output) && !in_array($this->_view, $this->_default)) {
- $this->_cache->add($this->_cache_key ,$this->_output);
- }
- }
-
- protected function _addViewTemplates()
- {
- if($this->_type) {
- $type = ucfirst($this->_type);
- }
- // get the parents of the current class, including self
- $stack = array_reverse(Solar_Class::parents($this, true));
-
- // remove Solar_Base
- array_pop($stack);
-
- // convert underscores to slashes, and add /View
- foreach ($stack as $key => $val) {
- $stack[$key] = str_replace('_', '/', $val)
- . '/View'
- . $type === "Normal" ? null : "/$type";
- }
-
- // done, add the stack
- $this->_view_object->addTemplatePath($stack);
- }
-
- protected function _addViewHelpers()
- {
- // start with requested helper classes
- $stack = $this->_helper_class;
-
- // find vendors, disregarding Solar itself (since Solar_View will
- // add that anyway)
- $vendors = Solar_Class::vendors($this);
- array_shift($vendors);
-
- // add each vendor to the stack in turn
- foreach ($vendors as $vendor) {
- $stack[] = "{$vendor}_View_Helper";
- }
-
- // set the helper classes on the view object
- $this->_view_object->addHelperClass($stack);
- }
-
- protected function _renderView()
- {
- if(in_array('sketch', $this->_related)) {
- $this->_catalog->sketch->express('tpl', $this, $this->_view);
- }
- $tpl = $this->_view . ".php";
- try {
- $this->_output = $this->_view_object->fetch($tpl);
- } catch (Solar_View_Exception_TemplateNotFound $e) {
- throw $this->_exception('ERR_VIEW_NOT_FOUND', array(
- 'path' => $e->getInfo('path'),
- 'name' => $e->getInfo('name'),
- ));
- }
- }
-
- protected function _postExpress()
- {
- if($this->_view && in_array($this->_view, $this->_default)) {
- $entry = array();
- $entry['dependency'] = "emerge/" . $this->_view;
- $key = $this->_cache->entry($entry);
- $this->_cache->add($key ,$this->_output);
- }
- }
-
- protected function _sketch()
- {
-
- }
-
- protected function _fixFactor()
- {
- if (empty($this->_factor_class)) {
- $class = $this->_stack->load('Factor', false);
- if (! $class) {
- $class = 'Miku_Emerge_Factor';
- }
- $this->_factor_class = $class;
- }
- $this->_factor = Solar::factory($this->_factor_class);
- }
-
- protected function _fixName()
- {
- if(isset($this->_factor->sense_name)){
- $this->name = $this->_factor->sense_name;
- }else {
- $class = get_class($this);
- $pos = strrpos($class, '_');
- $this->name = substr($class, $pos + 1);
- $this->name = preg_replace(
- '/([a-z])([A-Z])/',
- '$1-$2',
- $this->name
- );
- $this->name = strtolower($this->sense_name);
- }
- }
-
- protected function _fixCache()
- {
- // make sure we have a cache class
- if (empty($this->_cache_class)) {
- $class = $this->_stack->load('Cache', false);
- if (! $class) {
- $class = 'Miku_Emerge_Cache';
- }
- $this->_cache_class = $class;
- }
- }
-
- protected function _fire($signal)
- {
- //$singal began with ":" or will be pushed with "Miku:"
- if($signal[0] == ':') {
- $event = get_class($this) . $signal;
- }else {
- $event = 'Miku:' . $signal;
- }
- Miku::fireEvent($event);
- }
-
- //help method to attach
- //$callback :singal, $this, someMethod OR signal, $this, someMethod
- protected function _attach($callback)
- {
- settype($callback, 'array');
- $signal = array_shift($callback);
- if($signal[0] == ':') {
- $event = get_class($this) . $signal;
- }else {
- $event = 'Miku:' . $signal;
- }
- Miku::attachEvent($event, $callback);
- }
-
- protected function _error($key, $replace = null)
- {
- $this->_errors[] = $this->locale($key, 1, $replace);
- $this->asError();
- }
-
- public function asError()
- {
- }
-
- public function getFactor()
- {
- return $this->_factor;
- }
-
- public function getRelated($alias)
- {
- if($this->_related[$alias]) {
- $this->_related[$alias] = $this->_catalog->{$alias};
- return $this->_related[$alias];
- }
- }
-
- public function getCache()
- {
- return $this->_cache;
- }
-
- public function getCacheKeyPrefix()
- {
- return Solar_Class::vendor($this);
- }
-
- public function output()
- {
- return $this->_output;
- }
-
- public function setView($view)
- {
- $this->_view_object = $view;
- }
-
- }