jablonka.czprosek.czf

freenet-router

Subversion Repositories:
[/] [trunk/] [freenet-router/] [var/] [www/] [freenet-router/] [Framework/] [Libraries/] [UI/] [Model/] [Component.php] - Rev 2

Compare with Previous - Blame - Download


<?php

namespace Phem\Libraries\UI\Model;

use Doctrine\Common\Annotations\AnnotationReader;
use Phem\Core\Collection;
use Phem\Core\Object;
use Phem\Environment\EnvironmentManager;
use Phem\Libraries\UI\Annotations\ContentAttr;
use Phem\Libraries\UI\Annotations\DomAttr;
use Phem\Libraries\UI\Annotations\NoUpdateAttr;
use Phem\Libraries\UI\Annotations\StyleAttr;
use Phem\ToolSuite;
use Ratchet\Wamp\Exception;
use ReflectionProperty;

/**
 * @author Jakub PetrŞílka <petrzilka@czweb.net>
 */
abstract class Component extends Object
{
    abstract public function getTemplateName();
    
    public function getFullName()
    {
        $parent = $this;
        $nameArray = new Collection();

        while (!is_a($parent, '\Phem\Libraries\UI\Model\Workspace'))
        {
            $nameArray->add($parent->getName());
            $parent = $parent->getParent();
        }
        
        $nameArray->reverse(false);

        $name = "";
        foreach ($nameArray as $namePart)
        {
            if ($name != "")
            {
                $name .= "_";
            }
            $name .= $namePart;
        }
        return $name;
    }    
    
    public function getWorkspace()
    {
        $parent = $this;
        while (!is_a($parent, '\Phem\Libraries\UI\Model\Workspace'))
        {
            $parent = $parent->getParent();
        }
        return $parent;
    }    
    
    public function __call($method, $args)
    {
        if (!method_exists($this, $method))
        {
            return;
        }
        
        call_user_func_array(array($this, $method), $args);
        if ((ToolSuite::startsWith($method, 'set') !== false) && ($method != "setValue"))
        {
            $attrName = lcfirst(substr($method, 3));

            $reflectionProp = new ReflectionProperty($this, $attrName);

            $annotationReader = new AnnotationReader();
            $propAnnotations = $annotationReader
                    ->getPropertyAnnotations($reflectionProp);

            $foundAnnotations = new Collection();

            $path = "";
            foreach ($propAnnotations as $annotation)
            {
                if (($annotation instanceof NoUpdateAttr) || ($annotation instanceof DomAttr) || ($annotation instanceof ContentAttr) || ($annotation instanceof StyleAttr))
                {
                    $foundAnnotations->add($annotation);
                }
                else if (($annotation instanceof \Phem\Libraries\UI\Annotations\DomPath))
                {
                    $path = $annotation->path;
                }
            }
            
            $fullName = $this->getFullName();
            if ($path != "")
            {
                $fullName .= "_".$path;
            }            

            if ($foundAnnotations->count() > 1)
            {
                throw new Exception(get_class($this) . "::" . $method .
                "() - attribute must have either 0 or 1 annotation from  NoUpdateAttr, DomAttr, ContentAttr, StyleAttr");
            }
            else if ($foundAnnotations->count() == 1)
            {
                $annotation = $foundAnnotations->getFirst();

                if ($annotation instanceof NoUpdateAttr)
                {
                    return;
                }

                $change = new Change();
                $change->setAttrName($annotation->name);
                $change->setData($args[0]);
                
                $change->setObjectName($fullName);

                if ($annotation instanceof StyleAttr)
                {
                    $change->setType(ChangeType::CHANGE_STYLE);
                }
                else if ($annotation instanceof DomAttr)
                {

                    $change->setType(ChangeType::CHANGE_ATTR);
                }
                else if ($annotation instanceof ContentAttr)
                {
                    $change->setType(ChangeType::UPDATE_CONTENT);
                }
            }
            else
            {
                $twig = EnvironmentManager::getTwig();

                $data = $twig->render('@Phem/UI/' . $this->getTemplateName() . '.twig', array("component" => $this));

                $change = new Change();
                $change->setType(ChangeType::REPLACE);
                $change->setData($data);
                $change->setObjectName($fullName);
            }

            $this->getWorkspace()->getChanges()->set($this->getFullName() . "_" . $attrName, $change);
        }
    }
}


Powered by WebSVN 2.2.1