ComposerScripts   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 177
Duplicated Lines 0 %

Importance

Changes 5
Bugs 1 Features 0
Metric Value
eloc 101
dl 0
loc 177
rs 10
c 5
b 1
f 0
wmc 11

5 Methods

Rating   Name   Duplication   Size   Complexity  
B post_autoload_dump() 0 100 5
A update_bootstrap() 0 9 2
A post_update() 0 3 1
A update_composer() 0 9 2
A post_install() 0 3 1
1
<?php
2
/**
3
 * The file that defines to modify files after install the project.
4
 *
5
 * @link       https://github.com/maab16
6
 * @since      1.0.0
7
 */
8
9
namespace CodexShaper\Composer;
10
11
use Composer\Script\Event;
12
use Illuminate\Filesystem\Filesystem;
13
14
/**
15
 * The composer script class.
16
 *
17
 * Here set all the logic that implement after install the project.
18
 *
19
 * @since      1.0.0
20
 *
21
 * @author     Md Abu Ahsan basir <[email protected]>
22
 */
23
class ComposerScripts
24
{
25
    /**
26
     * Handle the post-install Composer event.
27
     *
28
     * @param \Composer\Script\Event $event The composer event.
0 ignored issues
show
Bug introduced by
The type Composer\Script\Event was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
     *
30
     * @return void
31
     */
32
    public static function post_install(Event $event)
33
    {
34
        require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php';
35
    }
36
37
    /**
38
     * Handle the post-update Composer event.
39
     *
40
     * @param \Composer\Script\Event $event The composer event.
41
     *
42
     * @return void
43
     */
44
    public static function post_update(Event $event)
45
    {
46
        require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php';
47
    }
48
49
    /**
50
     * Handle the post-autoload-dump Composer event.
51
     *
52
     * @param \Composer\Script\Event $event The composer event.
53
     *
54
     * @return void
55
     */
56
    public static function post_autoload_dump(Event $event)
57
    {
58
        require_once $event->getComposer()->getConfig()->get('vendor-dir').'/autoload.php';
59
60
        $dir = $event->getComposer()->getConfig()->get('vendor-dir').'/../';
0 ignored issues
show
Unused Code introduced by
The assignment to $dir is dead and can be removed.
Loading history...
61
        $root = dirname($event->getComposer()->getConfig()->get('vendor-dir'));
62
63
        $vendor_name = strtolower(basename($root));
64
        $partials = explode('-', $vendor_name);
65
        $camel_case_partials = [];
66
        foreach ($partials as $partial) {
67
            $camel_case_partials[] = ucfirst(strtolower($partial));
68
        }
69
        $camel_case = implode('_', $camel_case_partials);
70
        $snake_case = implode('_', $partials);
71
72
        $files = [
73
            '/admin/class-wpb-admin.php',
74
            '/admin/class-wpb-admin-menu.php',
75
            '/admin/class-wpb-admin-submenu.php',
76
            '/admin/partials/wpb-admin-display.php',
77
            '/admin/css/wpb-admin.css',
78
            '/admin/js/wpb-admin.js',
79
            '/app/Exceptions/Handler.php',
80
            '/app/Http/Controllers/ProductController.php',
81
            '/app/Http/Middleware/AuthMiddleware.php',
82
            '/app/Http/Middleware/VerifyCsrfToken.php',
83
            '/app/Http/Kernel.php',
84
            '/app/User.php',
85
            '/app/Post.php',
86
            '/bootstrap/app.php',
87
            '/config/app.php',
88
            '/config/database.php',
89
            '/config/view.php',
90
            '/database/migrations/class-create-customers-table.php',
91
            '/database/seeds/class-customers-table.php',
92
            '/includes/class-wpb-activator.php',
93
            '/includes/class-wpb-deactivator.php',
94
            '/includes/class-wpb-i18n.php',
95
            '/includes/class-wpb-loader.php',
96
            '/includes/class-wpb.php',
97
            '/public/class-wpb-public.php',
98
            '/public/partials/wpb-public-display.php',
99
            '/public/css/wpb-public.css',
100
            '/public/js/wpb-public.js',
101
            '/resources/js/admin/main.js',
102
            '/resources/js/admin/router/index.js',
103
            '/resources/js/frontend/main.js',
104
            '/resources/js/spa/main.js',
105
            '/routes/web.php',
106
            '/routes/api.php',
107
            '/src/Contracts/Http/Kernel.php',
108
            '/src/Contracts/ExceptionHandler.php',
109
            '/src/Database/Eloquent/Scopes/PostAuthorScope.php',
110
            '/src/Database/Eloquent/Scopes/PostStatusScope.php',
111
            '/src/Database/Eloquent/Scopes/PostTypeScope.php',
112
            '/src/Database/DB.php',
113
            '/src/Exceptions/Handler.php',
114
            '/src/Http/Events/RequestHandled.php',
115
            '/src/Http/Kernel.php',
116
            '/src/helpers.php',
117
            '/src/Support/Facades/Config.php',
118
            '/src/Support/Facades/Route.php',
119
            '/src/Application.php',
120
            '/tests/Application.php',
121
            '/wpb.php',
122
        ];
123
124
        foreach ($files as $file) {
125
            $file = $root.$file;
126
            if (file_exists($file)) {
127
                $filesystem = new Filesystem();
128
129
                $contents = $filesystem->get($file);
130
                $contents = str_replace('wpb_', $snake_case.'_', $contents);
131
                $contents = str_replace('wpb', $vendor_name, $contents);
132
                $contents = str_replace('WPB_APP_ROOT', strtoupper($camel_case).'_APP_ROOT', $contents);
133
                $contents = str_replace('WPB_FILE', strtoupper($camel_case).'_FILE', $contents);
134
                $contents = str_replace('WPB_PATH', strtoupper($camel_case).'_PATH', $contents);
135
                $contents = str_replace('WPB_INCLUDES', strtoupper($camel_case).'_INCLUDES', $contents);
136
                $contents = str_replace('WPB_URL', strtoupper($camel_case).'_URL', $contents);
137
                $contents = str_replace('WPB_ASSETS', strtoupper($camel_case).'_ASSETS', $contents);
138
                $contents = str_replace('WPB_VERSION', strtoupper($camel_case).'_VERSION', $contents);
139
                $contents = str_replace('WPB', $camel_case, $contents);
140
                $filesystem->put(
141
                    $file,
142
                    $contents
143
                );
144
145
                $dir = dirname($file);
146
                $file_name = basename($file);
147
                $new_file_name = str_replace('wpb', $vendor_name, $file_name);
148
149
                if ($file_name !== $new_file_name) {
150
                    rename($file, $dir.'/'.$new_file_name);
151
                }
152
            }
153
        }
154
155
        static::update_composer($filesystem, $root, $camel_case);
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $filesystem does not seem to be defined for all execution paths leading up to this point.
Loading history...
156
    }
157
158
    /**
159
     * Replace bootstrap file.
160
     *
161
     * @since    1.0.0
162
     *
163
     * @param Illuminate\Filesystem\Filesystem $filesystem The illuminate filesystem.
0 ignored issues
show
Bug introduced by
The type CodexShaper\Composer\Ill...e\Filesystem\Filesystem was not found. Did you mean Illuminate\Filesystem\Filesystem? If so, make sure to prefix the type with \.
Loading history...
164
     * @param string                           $root       The string is unique root path for each plugin.
165
     * @param string                           $camel_case This string is camel case of project name.
166
     *
167
     * @return void
168
     */
169
    protected static function update_bootstrap($filesystem, $root, $camel_case)
170
    {
171
        $file = $root.'/bootstrap/app.php';
172
        if (file_exists($file)) {
173
            $contents = $filesystem->get($file);
174
            $contents = str_replace('WPB_APP_ROOT', strtoupper($camel_case).'_APP_ROOT', $contents);
175
            $filesystem->put(
176
                $file,
177
                $contents
178
            );
179
        }
180
    }
181
182
    /**
183
     * Update composer.
184
     *
185
     * @param Illuminate\Filesystem\Filesystem $filesystem The illuminate filesystem.
186
     * @param string                           $root       The app root.
187
     * @param string                           $camel_case The composer event.
188
     *
189
     * @return void
190
     */
191
    protected static function update_composer($filesystem, $root, $camel_case)
192
    {
193
        $file = $root.'/composer.json';
194
        if (file_exists($file)) {
195
            $contents = $filesystem->get($file);
196
            $contents = str_replace('WPB', $camel_case, $contents);
197
            $filesystem->put(
198
                $file,
199
                $contents
200
            );
201
        }
202
    }
203
}
204