-
Notifications
You must be signed in to change notification settings - Fork 2
/
ayuco
120 lines (100 loc) · 2.52 KB
/
ayuco
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env php
<?php
/**
* Commands for composer.
*/
if ( empty( $argv ) || count($argv) <= 1 ) { print( 'No command given.' ); die; }
$function = $argv[1];
if ( ! function_exists( $function ) ) { print( $function . ' command not found.' ); die; }
unset( $argv[0] );
unset( $argv[1] );
call_user_func_array( $function, $argv );
/**
* Setups project.
*/
function setup()
{
echo '------------------------------';
echo "\n";
echo 'Wordpress Plugin (AYUCO) Setup';
echo "\n";
echo '------------------------------';
echo "\n";
echo 'Enter your plugin namespace (example: "MyPlugin" ):';
echo "\n";
$handle = fopen( 'php://stdin', 'r' );
$namespace = trim( fgets( $handle ) );
$namespace = empty( $namespace )
? 'MyPlugin'
: str_replace( ' ', '', ucwords( strtolower( $namespace ) ) );
setname( $namespace );
echo sprintf( 'Your plugin namespace is "%s"', $namespace );
echo "\n";
echo 'Setup completed!';
exit;
}
/**
* Renames plugin package.
*/
function setname ()
{
$name = func_get_args()[0];
if ( empty( $name ) ) { print( 'Rename command is expenctig a name.' ); die; }
try {
$config = require_once __DIR__ . '/config/plugin.php' ;
$currentname = $config['namespace'];
replace_in_file( $currentname, $name, __DIR__.'/config/plugin.php' );
replace_in_file(
'namespace ' . $currentname,
'namespace ' . $name
, __DIR__.'/plugin/Main.php'
);
foreach ( scandir( __DIR__ . '/plugin/models' ) as $filename ) {
replace_in_file(
'namespace ' . $currentname,
'namespace ' . $name,
__DIR__ . '/plugin/models/' . $filename
);
}
foreach ( scandir( $config['paths']['controllers'] ) as $filename ) {
replace_in_file(
'namespace ' . $currentname,
'namespace ' . $name,
$config['paths']['controllers'] . $filename
);
replace_in_file(
'use ' . $currentname,
'use ' . $name,
$config['paths']['controllers'] . $filename
);
}
replace_in_file(
'"' . $currentname,
'"' . $name,
__DIR__ . '/composer.json'
);
exec( 'composer dump-autoload' );
} catch ( Exception $e ) {
print( $e->getMessage() );
}
}
/**
* Replaces needle in file.
*
* @param string $needle Needle to replace with.
* @param string $replace What to replace with.
* @param string $filename
*/
function replace_in_file( $needle, $replace, $filename )
{
if ( $filename == '.' || $filename == '..' ) return;
file_put_contents(
$filename,
preg_replace(
'/' . $needle . '/',
$replace,
file_get_contents( $filename )
)
);
}
exit;