AutoClassBuilder.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: AutoClassBuilder.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class AutoClassBuilder extends BaseBuilder
00017     {
00018         public static function build(MetaClass $class)
00019         {
00020             $out = self::getHead();
00021             
00022             $out .= "abstract class Auto{$class->getName()}";
00023             
00024             $isNamed = false;
00025             
00026             if ($parent = $class->getParent())
00027                 $out .= " extends {$parent->getName()}";
00028             elseif (
00029                 $class->getPattern() instanceof DictionaryClassPattern
00030                 && $class->hasProperty('name')
00031             ) {
00032                 $out .= " extends NamedObject";
00033                 $isNamed = true;
00034             } elseif (!$class->getPattern() instanceof ValueObjectPattern)
00035                 $out .= " extends IdentifiableObject";
00036             
00037             if ($interfaces = $class->getInterfaces())
00038                 $out .= ' implements '.implode(', ', $interfaces);
00039             
00040             $out .= "\n{\n";
00041             
00042             foreach ($class->getProperties() as $property) {
00043                 if (!self::doPropertyBuild($property, $isNamed))
00044                     continue;
00045                 
00046                 $out .=
00047                     "protected \${$property->getName()} = "
00048                     ."{$property->getType()->getDeclaration()};\n";
00049                 
00050                 if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
00051                     $out .=
00052                         "protected \${$property->getName()}Id = null;\n";
00053                 }
00054             }
00055             
00056             $valueObjects = array();
00057             
00058             foreach ($class->getProperties() as $property) {
00059                 if (
00060                     $property->getType() instanceof ObjectType
00061                     && !$property->getType()->isGeneric()
00062                     && $property->getType()->getClass()->getPattern()
00063                         instanceof ValueObjectPattern
00064                 ) {
00065                     $valueObjects[$property->getName()] =
00066                         $property->getType()->getClassName();
00067                 }
00068             }
00069             
00070             if ($valueObjects) {
00071                 $out .= <<<EOT
00072 
00073 public function __construct()
00074 {
00075 
00076 EOT;
00077                 foreach ($valueObjects as $propertyName => $className) {
00078                     $out .= "\$this->{$propertyName} = new {$className}();\n";
00079                 }
00080                 
00081                 $out .= "}\n";
00082             }
00083             
00084             $out .= self::buildSerializers($class);
00085             
00086             foreach ($class->getProperties() as $property) {
00087                 if (!self::doPropertyBuild($property, $isNamed))
00088                     continue;
00089                 
00090                 $out .= $property->toMethods($class);
00091             }
00092             
00093             $out .= "}\n";
00094             $out .= self::getHeel();
00095             
00096             return $out;
00097         }
00098         
00099         private static function buildSerializers(MetaClass $class)
00100         {
00101             $slackers = array();
00102             
00103             foreach ($class->getProperties() as $property) {
00104                 if ($property->getFetchStrategyId() == FetchStrategy::LAZY) {
00105                     $slackers[] = $property;
00106                 }
00107             }
00108             
00109             if (!$slackers)
00110                 return null;
00111             
00112             $out = <<<EOT
00113 
00114 public function __sleep()
00115 {
00116     \$properties = get_object_vars(\$this);
00117     
00118     unset(
00119 
00120 EOT;
00121             $unsetters = array();
00122             
00123             foreach ($slackers as $property) {
00124                 $unsetters[] = "\$properties['{$property->getName()}']";
00125             }
00126             
00127             $out .= implode(",\n", $unsetters);
00128             
00129             $out .= <<<EOT
00130 
00131     );
00132     
00133     return array_keys(\$properties);
00134 }
00135 
00136 EOT;
00137             return $out;
00138         }
00139         
00140         private static function doPropertyBuild(
00141             MetaClassProperty $property,
00142             $isNamed
00143         )
00144         {
00145             if ($isNamed && $property->getName() == 'name')
00146                 return false;
00147             
00148             if (
00149                 ($property->getName() == 'id')
00150                 && !$property->getClass()->getParent()
00151             )
00152                 return false;
00153             
00154             // do not redefine parent's properties
00155             if (
00156                 $property->getClass()->getParent()
00157                 && array_key_exists(
00158                     $property->getName(),
00159                     $property->getClass()->getParentsProperties()
00160                 )
00161             )
00162                 return false;
00163             
00164             return true;
00165         }
00166     }
00167 ?>

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