Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

GeneratorBundle/Helper/GeneratorUtils.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace Kunstmaan\GeneratorBundle\Helper;
4
5
use Doctrine\ORM\Mapping\ClassMetadata;
6
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
7
use Symfony\Component\Console\Input\InputInterface;
8
use Symfony\Component\Console\Output\OutputInterface;
9
use Symfony\Component\DependencyInjection\ContainerInterface;
10
use Symfony\Component\HttpKernel\Kernel;
11
12
/**
13
 * GeneratorUtils
14
 */
15
class GeneratorUtils
16
{
17
18
    /**
19
     * Cleans the prefix. Prevents a double underscore from happening.
20
     *
21
     * @param $prefixString
22
     * @return string
23
     */
24
    public static function cleanPrefix($prefixString)
25
    {
26
        $prefixString = trim($prefixString);
27
        if (empty($prefixString)) {
28
            return null;
29
        }
30
31
        $result = preg_replace('/_*$/i', '', strtolower($prefixString)) . '_';
32
33
        if ($result == '_') {
34
            return null;
35
        }
36
37
        return $result;
38
    }
39
40
    /**
41
     * Returns an array of fields. Fields can be both column fields and
42
     * association fields.
43
     *
44
     * @param ClassMetadata $metadata
45
     *
46
     * @return array $fields
47
     */
48
    public static function getFieldsFromMetadata(ClassMetadata $metadata)
49
    {
50
        $fields = (array) $metadata->fieldNames;
51
52
        // Remove the primary key field if it's not managed manually
53
        if (!$metadata->isIdentifierNatural()) {
54
            $fields = array_diff($fields, $metadata->identifier);
55
        }
56
57
        foreach ($metadata->associationMappings as $fieldName => $relation) {
58
            if ($relation['type'] !== ClassMetadata::ONE_TO_MANY) {
59
                $fields[] = $fieldName;
60
            }
61
        }
62
63
        return $fields;
64
    }
65
66
    /**
67
     * Prepend the string in the file
68
     *
69
     * @param string $string   Text to be added in front of the file
70
     * @param string $filename File to prepend in
71
     */
72 View Code Duplication
    public static function prepend($string, $filename)
73
    {
74
        $context = stream_context_create();
75
        $fp = fopen($filename, 'r', 1, $context);
76
        $tmpname = md5($string);
77
        file_put_contents($tmpname, $string);
78
        file_put_contents($tmpname, $fp, FILE_APPEND);
79
        fclose($fp);
80
        unlink($filename);
81
        rename($tmpname, $filename);
82
    }
83
84
    /**
85
     * Append the string in the file
86
     *
87
     * @param string $string   Text to be added in front of the file
88
     * @param string $filename File to prepend in
89
     */
90 View Code Duplication
    public static function append($string, $filename)
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
91
    {
92
        $context = stream_context_create();
93
        $fp = fopen($filename, 'r', 1, $context);
94
        $tmpname = md5($string);
95
        file_put_contents($tmpname, $fp);
96
        file_put_contents($tmpname, $string, FILE_APPEND);
97
        fclose($fp);
98
        unlink($filename);
99
        rename($tmpname, $filename);
100
    }
101
102
    /**
103
     * Find and replace the string in the file
104
     *
105
     * @param string $toReplace   Text to be replaced
106
     * @param string $replaceText Text as replacement
107
     * @param string $filename    File to replace in
108
     */
109
    public static function replace($toReplace, $replaceText, $filename)
110
    {
111
        $content = file_get_contents($filename);
112
        if ($content) {
113
            $content = str_replace($toReplace, $replaceText, $content);
114
            file_put_contents($filename, $content);
115
        }
116
117
    }
118
119
120
    public static function getFullSkeletonPath($pathInSkeleton)
121
    {
122
        $pathInSkeleton = trim($pathInSkeleton);
123
124
        // pathInSkeleton needs to be prepended by a /
125
        if (substr($pathInSkeleton, 0, 1) !== '/') {
126
            $pathInSkeleton = '/' . $pathInSkeleton;
127
        }
128
129
        // Can't have a / at the end.
130
        if (substr($pathInSkeleton, -1) == '/') {
131
            $pathInSkeleton = rtrim($pathInSkeleton, '/');
132
        }
133
134
        return __DIR__ . '/../Resources/SensioGeneratorBundle/skeleton' . $pathInSkeleton;
135
    }
136
137
    public static function ensureOptionsProvided(InputInterface $input, array $options)
138
    {
139
        foreach ($options as $option) {
140
            if (null === $input->getOption($option)) {
141
                throw new \RuntimeException(sprintf('The "%s" option must be provided.', $option));
142
            }
143
        }
144
    }
145
146
    /**
147
     * Replaces '\' with '/'.
148
     *
149
     * @param $namespace
150
     * @return string
151
     */
152
    public static function fixNamespace($namespace)
153
    {
154
        return str_replace('\\', '/', $namespace);
155
    }
156
157
    /**
158
     * Returns an inputAssistant.
159
     *
160
     * This probably isn't the cleanest way. It'd be nicer if we could make a KunstmaanGenerator class
161
     * which all generators inherit from. It then provides a bunch of helper functions and a uniform manner
162
     * in which the input options are handled.
163
     *
164
     * @param InputInterface     $input
165
     * @param OutputInterface    $output
166
     * @param QuestionHelper     $questionHelper
167
     * @param Kernel             $kernel
168
     * @param ContainerInterface $container
169
     *
170
     * @return InputAssistant
171
     */
172
    public static function getInputAssistant(InputInterface &$input, OutputInterface $output, QuestionHelper $questionHelper, Kernel $kernel, ContainerInterface $container)
173
    {
174
	return new InputAssistant($input, $output, $questionHelper, $kernel, $container);
175
    }
176
}
177