Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding data format support for blocks.

  • Loading branch information...
commit 3c0d2865e5189efd3160ad4289614a99da531ce4 1 parent 32363fc
@zyxist zyxist authored
Showing with 102 additions and 38 deletions.
  1. +75 −0 lib/Opt/Format/Block.php
  2. +27 −38 lib/Opt/Instruction/Block.php
View
75 lib/Opt/Format/Block.php
@@ -0,0 +1,75 @@
+<?php
+/*
+ * OPEN POWER LIBS <http://www.invenzzia.org>
+ *
+ * This file is subject to the new BSD license that is bundled
+ * with this package in the file LICENSE. It is also available through
+ * WWW at this URL: <http://www.invenzzia.org/license/new-bsd>
+ *
+ * Copyright (c) Invenzzia Group <http://www.invenzzia.org>
+ * and other contributors. See website for details.
+ */
+
+/**
+ * The data format for blocks.
+ *
+ * @author Tomasz Jędrzejewski
+ * @copyright Invenzzia Group <http://www.invenzzia.org/> and contributors.
+ * @license http://www.invenzzia.org/license/new-bsd New BSD License
+ * @package Formats
+ */
+class Opt_Format_Block extends Opt_Format_Abstract
+{
+ /**
+ * The list of supported hook types.
+ * @var array
+ */
+ protected $_supports = array(
+ 'block'
+ );
+
+ /**
+ * Build a PHP code for the specified hook name.
+ *
+ * @internal
+ * @param string $hookName The hook name
+ * @return string The output PHP code
+ */
+ protected function _build($hookName)
+ {
+ switch($hookName)
+ {
+ case 'block:init':
+ return ' '.$this->_getVar('variable').'->setView($this); ';
+ case 'block:done':
+ return '';
+ case 'block:valid':
+ return ' '.$this->_getVar('variable').' instanceof Opt_Block_Interface';
+ case 'block:build':
+ return ' '.$this->_getVar('variable').' = new '.$this->_getVar('className').'; ';
+ case 'block:on-single':
+ $argList = 'array( ';
+ foreach($this->_getVar('arguments') as $name=>$value)
+ {
+ $argList .= '\''.$name.'\' => '.$value.', ';
+ }
+ $argList .= ')';
+ return ' '.$this->_getVar('variable').'->onSingle('.$argList.'); ';
+ case 'block:on-open':
+ $argList = 'array( ';
+ foreach($this->_getVar('arguments') as $name=>$value)
+ {
+ $argList .= '\''.$name.'\' => '.$value.', ';
+ }
+ $argList .= ')';
+ return ' if('.$this->_getVar('variable').'->onOpen('.$argList.')){ ';
+ case 'block:on-close':
+ return ' } '.$this->_getVar('variable').'->onClose(); ';
+ case 'block:set':
+ return ' '.$this->_getVar('variable').'->__set('.$this->_getVar('name').', '.$this->_getVar('value').') ';
+ case 'block:get':
+ return ' '.$this->_getVar('variable').'->__get('.$this->_getVar('name').') ';
+ }
+ } // end _build();
+
+} // end Opt_Format_Block;
View
65 lib/Opt/Instruction/Block.php
@@ -77,16 +77,20 @@ public function processNode(Opt_Xml_Node $node)
// Undefined block processing
$params = array(
'from' => array(self::REQUIRED, self::EXPRESSION, null),
+ 'id' => array(self::OPTIONAL, self::STRING, null),
'__UNKNOWN__' => array(self::OPTIONAL, self::EXPRESSION, null)
);
$vars = $this->_extractAttributes($node, $params);
- $this->_stack->push($params['from']);
- $mainCode = ' if(is_object('.$params['from'].') && '.$params['from'].' instanceof Opt_Block_Interface){ '.$params['from'].'->setView($this); ';
- $mainCode .= $this->_commonProcessing($node, $params['from'], $vars);
+ $format = $this->_compiler->getFormat('block#'.$params['id'], false, $this->_tpl->blockFormat);
+ $format->assign('variable', $params['from']);
+ $this->_stack->push(array($params['from'], $format));
+
+ $mainCode = ' if(is_object('.$params['from'].') && '.$format->get('block:valid').'){ '.$format->get('block:init');
+ $mainCode .= $this->_commonProcessing($node, $params['from'], $vars, $format);
$node->addBefore(Opt_Xml_Buffer::TAG_BEFORE, $mainCode);
- $node->addAfter(Opt_Xml_Buffer::TAG_AFTER, ' } ');
+ $node->addAfter(Opt_Xml_Buffer::TAG_AFTER, $format->get('block:done').' } ');
$node->set('postprocess', true);
} // end processNode();
@@ -111,36 +115,25 @@ public function processBlock(Opt_Xml_Element $node)
{
// Defined block processing
$params = array(
+ 'id' => array(self::OPTIONAL, self::STRING, null),
'__UNKNOWN__' => array(self::OPTIONAL, self::EXPRESSION, null)
);
$vars = $this->_extractAttributes($node, $params);
- // Get the real class name
- $cn = '$_block_'.($this->_unique++);
- $this->_stack->push($cn);
-
- $class = $this->_compiler->block($node->getXmlName());
- // Check, if there are any conversions that may take control over initializing
- // the component object. We are allowed to capture only particular component
- // creation or all of them.
- if((($to = $this->_compiler->convert('##block_'.$class)) != '##block_'.$class))
- {
- $ccode = str_replace(array('%CLASS%', '%TAG%'), array($class, $node->getXmlName()), $to);
- }
- elseif((($to = $this->_compiler->convert('##block')) != '##block'))
- {
- $ccode = str_replace(array('%CLASS%', '%TAG%'), array($class, $node->getXmlName()), $to);
- }
- else
- {
- $ccode = 'new '.$class;
- }
+ // Initialize block structures
+ $cn = '$_block_'.($this->_unique++);
+ $format = $this->_compiler->getFormat('block#'.$params['id'], false, $this->_tpl->blockFormat);
+ $format->assign('variable', $cn);
+ $this->_stack->push(array($cn, $format));
- $mainCode = $cn.' = '.$ccode.'; '.$cn.'->setView($this); ';
+ // Generate the initialization code
+ $format->assign('className', $this->_compiler->block($node->getXmlName()));
+ $mainCode = $format->get('block:build').$format->get('block:init');
- $this->_commonProcessing($node, $cn, $vars);
+ $this->_commonProcessing($node, $cn, $vars, $format);
$node->addBefore(Opt_Xml_Buffer::TAG_BEFORE, $mainCode);
+ $node->addAfter(Opt_Xml_Buffer::TAG_AFTER, $format->get('block:done'));
} // end processBlock();
/**
@@ -167,24 +160,18 @@ public function postprocessBlock(Opt_Xml_Node $node)
* @param array $args The array of custom block attributes.
* @return string
*/
- private function _commonProcessing(Opt_Xml_Element $node, $blockVariable, array $args)
+ private function _commonProcessing(Opt_Xml_Element $node, $blockVariable, array $args, Opt_Format_Abstract $format)
{
// Common part of the component processing
- $argList = 'array( ';
- foreach($args as $name=>$value)
- {
- $argList .= '\''.$name.'\' => '.$value.', ';
- }
- $argList .= ')';
-
+ $format->assign('arguments', $args);
if($node->get('single'))
{
- $node->addAfter(Opt_Xml_Buffer::TAG_SINGLE_BEFORE, $blockVariable.'->onSingle('.$argList.'); ');
+ $node->addAfter(Opt_Xml_Buffer::TAG_SINGLE_BEFORE, $format->get('block:on-single'));
}
else
{
- $node->addAfter(Opt_Xml_Buffer::TAG_BEFORE, ' if('.$blockVariable.'->onOpen('.$argList.')){ ');
- $node->addBefore(Opt_Xml_Buffer::TAG_AFTER, ' } '.$blockVariable.'->onClose(); ');
+ $node->addAfter(Opt_Xml_Buffer::TAG_BEFORE, $format->get('block:on-open'));
+ $node->addBefore(Opt_Xml_Buffer::TAG_AFTER, $format->get('block:on-close'));
}
$this->_process($node);
@@ -204,6 +191,8 @@ public function processSystemVar($opt)
{
throw new Opt_Instruction_Exception('opt:block error: cannot process $'.implode('.',$opt).': no blocks active.');
}
- return $this->_stack->top().'->get(\''.$opt[2].'\')';
+ list($variable, $format) = $this->_stack->top();
+ $format->assign('name', $opt[2]);
+ return $format->get('block:get');
} // end processSystemVar();
} // end Opt_Instruction_Block;
Please sign in to comment.
Something went wrong with that request. Please try again.