Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 Copyright (c) 2009 hamcrest.org
5
 */
6
 
7
abstract class FactoryFile
8
{
9
    /**
10
     * Hamcrest standard is two spaces for each level of indentation.
11
     *
12
     * @var string
13
     */
14
    const INDENT = '    ';
15
 
16
    private $indent;
17
 
18
    private $file;
19
 
20
    private $code;
21
 
22
    public function __construct($file, $indent)
23
    {
24
        $this->file = $file;
25
        $this->indent = $indent;
26
    }
27
 
28
    abstract public function addCall(FactoryCall $call);
29
 
30
    abstract public function build();
31
 
32
    public function addFileHeader()
33
    {
34
        $this->code = '';
35
        $this->addPart('file_header');
36
    }
37
 
38
    public function addPart($name)
39
    {
40
        $this->addCode($this->readPart($name));
41
    }
42
 
43
    public function addCode($code)
44
    {
45
        $this->code .= $code;
46
    }
47
 
48
    public function readPart($name)
49
    {
50
        return file_get_contents(__DIR__ . "/parts/$name.txt");
51
    }
52
 
53
    public function generateFactoryCall(FactoryCall $call)
54
    {
55
        $method = $call->getMethod();
56
        $code = $method->getComment($this->indent) . "\n";
57
        $code .= $this->generateDeclaration($call->getName(), $method);
58
        $code .= $this->generateCall($method);
59
        $code .= $this->generateClosing();
60
        return $code;
61
    }
62
 
63
    public function generateDeclaration($name, FactoryMethod $method)
64
    {
65
        $code = $this->indent . $this->getDeclarationModifiers()
66
            . 'function ' . $name . '('
67
            . $this->generateDeclarationArguments($method)
68
            . ')' . "\n" . $this->indent . '{' . "\n";
69
        return $code;
70
    }
71
 
72
    public function getDeclarationModifiers()
73
    {
74
        return '';
75
    }
76
 
77
    public function generateDeclarationArguments(FactoryMethod $method)
78
    {
79
        if ($method->acceptsVariableArguments()) {
80
            return '/* args... */';
81
        } else {
82
            return $method->getParameterDeclarations();
83
        }
84
    }
85
 
86
    public function generateImport(FactoryMethod $method)
87
    {
88
        return $this->indent . self::INDENT . "require_once '" . $method->getClass()->getFile() . "';" . "\n";
89
    }
90
 
91
    public function generateCall(FactoryMethod $method)
92
    {
93
        $code = '';
94
        if ($method->acceptsVariableArguments()) {
95
            $code .= $this->indent . self::INDENT . '$args = func_get_args();' . "\n";
96
        }
97
 
98
        $code .= $this->indent . self::INDENT . 'return ';
99
        if ($method->acceptsVariableArguments()) {
100
            $code .= 'call_user_func_array(array(\''
101
                . '\\' . $method->getClassName() . '\', \''
102
                . $method->getName() . '\'), $args);' . "\n";
103
        } else {
104
            $code .= '\\' . $method->getClassName() . '::'
105
                . $method->getName() . '('
106
                . $method->getParameterInvocations() . ');' . "\n";
107
        }
108
 
109
        return $code;
110
    }
111
 
112
    public function generateClosing()
113
    {
114
        return $this->indent . '}' . "\n";
115
    }
116
 
117
    public function write()
118
    {
119
        file_put_contents($this->file, $this->code);
120
    }
121
}