arse/includes/core/view.inc.php
Emmanuel BENOîT 29a026e71a Improved URL rewriting support
When this code was written, it did not include an internal URL mapper
and each page was loaded by a PHP script. The internal URL was a recent,
mostly unfinished addition.

Base URL is now supported:
 * for views, when they implement the BaseURLAware interface (a base
class that does what most views will do with that is provided -
BaseURLAwareView),
 * in the menu,
 * in form actions,
 * in boxes (for buttons, and for the contents if the inner view
implements BaseURLAware).
2012-02-05 17:42:53 +01:00

140 lines
2.2 KiB
PHP

<?php
interface View
{
public function render( );
}
interface BaseURLAware
{
public function setBaseURL( $baseURL );
}
abstract class BaseURLAwareView
implements View , BaseURLAware
{
protected $base;
public function setBaseURL( $baseURL )
{
$this->base = $baseURL;
}
}
interface TitleProvider
{
public function getTitle( );
}
final class HTML
{
protected $tag;
protected $attributes = array( );
protected $contents = array( );
protected $cached;
public function __construct( $tag )
{
$this->tag = $tag;
}
public function setAttribute( $attribute , $value )
{
if ( $value !== null ) {
$this->attributes[ $attribute ] = $value;
$this->cached = null;
}
return $this;
}
public function appendText( $text )
{
assert( is_scalar( $text ) );
array_push( $this->contents , HTML::from( $text ) );
$this->cached = null;
return $this;
}
public function appendElement( HTML $element )
{
array_push( $this->contents , $element );
$this->cached = null;
return $this;
}
public function appendRaw( $text )
{
assert( is_scalar( $text ) );
array_push( $this->contents , $text );
$this->cached = null;
return $this;
}
public function append( $auto )
{
if ( is_array( $auto ) ) {
foreach ( $auto as $element ) {
$this->append( $element );
}
} elseif ( is_scalar( $auto ) ) {
$this->appendRaw( $auto );
} else {
$this->appendElement( $auto );
}
return $this;
}
public function getCode( )
{
if ( $this->cached !== null ) {
return $this->cached;
}
$code = '<' . $this->tag;
if ( ! empty( $this->attributes ) ) {
$attrs = array( );
foreach ( $this->attributes as $name => $value ) {
array_push( $attrs , $name . '="' . $value . '"' );
}
$code .= ' ' . join( ' ' , $attrs );
}
if ( empty( $this->contents ) ) {
$code .= ' />';
} else {
$code .= '>';
foreach ( $this->contents as $item ) {
if ( is_scalar( $item ) ) {
$code .= $item;
} else {
$code .= $item->getCode( );
}
}
$code .= '</' . $this->tag . '>';
}
return ( $this->cached = $code );
}
public static function make( $tag )
{
return new HTML( $tag );
}
public static function from( $text )
{
return htmlentities( $text , ENT_COMPAT , 'UTF-8' );
}
}