/**
  * Compiles method definition
  *
  * @param DrupalFileBuilder $builder
  * @param DrupalCodeWriter $writer
  */
 protected function compileMethodDefinitions(DrupalFileBuilder $builder, DrupalCodeWriter $writer)
 {
     foreach ($builder->getMethods() as $method) {
         /* @var MethodBuilder $method */
         $writer->writeLine('/**');
         $comments = $method->getComments();
         if (count($comments)) {
             foreach ($comments as $comment) {
                 $comment = trim(ltrim($comment, '>'));
                 $writer->write(' * ')->writeLine($comment);
             }
             $writer->write(' * ')->writeLine();
         }
         $arguments = $method->getArguments();
         $argumentTypeMaxLen = 0;
         if (count($arguments)) {
             foreach ($arguments as $argument) {
                 $argumentTypeMaxLen = max($argumentTypeMaxLen, strlen($argument[1]));
             }
         }
         foreach ($arguments as $argument) {
             $writer->write(' * @param ' . sprintf('%-' . $argumentTypeMaxLen . 's', $argument[1]))->writeLine(' $' . $argument[0]);
         }
         if ($method->getType()) {
             $writer->writeLine(' *');
             $writer->writeLine(' * @return ' . $method->getType());
         }
         $writer->writeLine(' */');
         $writer->write('function ')->write($method->getName())->write('(');
         $args = array();
         foreach ($arguments as $argument) {
             $item = '';
             $item .= '$' . $argument[0];
             $args[] = $item;
         }
         if (count($args)) {
             $writer->write(implode(', ', $args));
         }
         $writer->writeLine(') {');
         $writer->writeLine();
         foreach ($method->getCodeLines() as $line) {
             $writer->indent()->writeLine($line);
         }
         $writer->writeLine()->writeLine('}')->writeLine();
     }
 }
Esempio n. 2
0
 private function writeObjectLiteralRecursion($value, DrupalCodeWriter $writer, $curried = 1)
 {
     switch (gettype($value)) {
         case 'int':
         case 'double':
             $writer->write($value);
             break;
         case 'bool':
             if ($value) {
                 $writer->write("true");
             } else {
                 $writer->write("false");
             }
             break;
         case 'string':
             $writer->write("'" . $value . "'");
             break;
         case 'array':
             $writer->writeLine('array(');
             $is_assoc = $this->isAssociative($value);
             foreach ($value as $key => $val) {
                 $writer->curriedIndent($curried);
                 if (!$is_assoc) {
                     $this->writeObjectLiteralRecursion($val, $writer, $curried);
                     $writer->writeLine(",");
                 } else {
                     $writer->write("'" . $key . "' => ");
                     $this->writeObjectLiteralRecursion($val, $writer, $curried + 1);
                     $writer->writeLine(",");
                 }
             }
             $writer->curriedIndent($curried - 1)->write(')');
             break;
         default:
             return null;
     }
 }