AutoProtoClassBuilder.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: AutoProtoClassBuilder.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class AutoProtoClassBuilder extends BaseBuilder
00017     {
00018         public static function build(MetaClass $class)
00019         {
00020             $out = self::getHead();
00021             
00022             $parent = $class->getParent();
00023             
00024             if ($class->hasBuildableParent())
00025                 $parentName = 'Proto'.$parent->getName();
00026             else
00027                 $parentName = 'AbstractProtoClass';
00028             
00029             if ($class->hasBuildableParent() || $parent) {
00030                 $out .= <<<EOT
00031 abstract class AutoProto{$class->getName()} extends {$parentName}
00032 {
00036     public function makeForm()
00037     {
00038         \$form =
00039 
00040 EOT;
00041 
00042                 if ($parent->getPattern() instanceof InternalClassPattern) {
00043                     $out .= 'Form::create()';
00044                 } else {
00045                     $out .= 'parent::makeForm()';
00046                 }
00047                 
00048                 $redefined = array();
00049                 
00050                 // checking for redefined properties
00051                 foreach ($class->getParentsProperties() as $property) {
00052                     if (
00053                         $class->hasProperty($property->getName())
00054                         && (
00055                             !$property->getClass()->getPattern()
00056                                 instanceof InternalClassPattern
00057                         )
00058                     ) {
00059                         $redefined[] =
00060                             "/* {$property->getClass()->getName()} */ "
00061                             ."drop('{$property->getName()}')";
00062                     }
00063                 }
00064                 
00065                 if ($redefined)
00066                     $out .= "->\n".implode("->\n", $redefined);
00067             } else {
00068                 $out .= <<<EOT
00069 abstract class AutoProto{$class->getName()} extends {$parentName}
00070 {
00074     public function makeForm()
00075     {
00076         return
00077             Form::create()
00078 EOT;
00079             }
00080             
00081             // sort out for wise and common defaults
00082             $prms = array();
00083             
00084             foreach ($class->getWithInternalProperties() as $property) {
00085                 if ($primitive = $property->toPrimitive($class)) {
00086                     if (is_array($primitive))
00087                         $prms = array_merge($prms, $primitive);
00088                     else
00089                         $prms[] = $primitive;
00090                 }
00091             }
00092             
00093             if (count($prms)) {
00094                 $out .= "->\nadd(".implode(")->\nadd(", $prms).");";
00095             } else {
00096                 $out .= ";";
00097             }
00098             
00099             // parent's identificator should be concretized in final childs
00100             if ($parent) {
00101                 if (
00102                     ($class->getTypeId() != MetaClassType::CLASS_ABSTRACT)
00103                     && ($id = $class->getIdentifier())
00104                 ) {
00105                     $out .=
00106                         "\n\n"
00107                         ."\$form->\nget('{$id->getName()}')->"
00108                         ."of('{$class->getName()}');\n\n";
00109                 } else {
00110                     $out .= "\n\n";
00111                 }
00112                 
00113                 $out .= "return \$form;";
00114             }
00115             
00116             $classDump = self::dumpMetaClass($class);
00117             
00118             $out .= <<<EOT
00119 
00120     }
00121 
00122 {$classDump}
00123 }
00124 
00125 EOT;
00126 
00127             return $out.self::getHeel();
00128         }
00129         
00130         private static function dumpMetaClass(MetaClass $class)
00131         {
00132             $out = <<<EOT
00133     protected function makePropertyList()
00134     {
00135 
00136 EOT;
00137 
00138             if ($class->hasBuildableParent()) {
00139                 $out .= <<<EOT
00140         return
00141             array_merge(
00142                 parent::makePropertyList(),
00143                 array(
00144 EOT;
00145                 if ($class->getWithInternalProperties())
00146                     $out .= "\n";
00147             } else {
00148                 $out .= <<<EOT
00149         return array(
00150 
00151 EOT;
00152             }
00153 
00154             $list = array();
00155             
00156             foreach ($class->getWithInternalProperties() as $property) {
00157                 if (
00158                     !$property->getType()->isGeneric()
00159                     && $property->getType() instanceof ObjectType
00160                     && (
00161                         $property->getType()->getClass()->getPattern()
00162                             instanceof ValueObjectPattern
00163                     )
00164                 ) {
00165                     $remote = $property->getType()->getClass();
00166                     
00167                     foreach ($remote->getProperties() as $remoteProperty) {
00168                         $list[] =
00169                             "'{$remoteProperty->getName()}' => "
00170                             .$remoteProperty->toLightProperty()->toString();
00171                     }
00172                 } else {
00173                     $list[] =
00174                         "'{$property->getName()}' => "
00175                         .$property->toLightProperty()->toString();
00176                 }
00177             }
00178             
00179             $out .= implode(",\n", $list);
00180             
00181             if ($class->hasBuildableParent()) {
00182                 $out .= "\n)";
00183             }
00184             
00185             $out .= <<<EOT
00186 
00187         );
00188     }
00189 EOT;
00190             return $out;
00191         }
00192     }
00193 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4