1+ <?php
2+ namespace Webman \ThinkCache ;
3+
4+ class Install
5+ {
6+ const WEBMAN_PLUGIN = true ;
7+
8+ /**
9+ * @var array
10+ */
11+ protected static $ pathRelation = [
12+ 'config/thinkcache.php ' => 'config/thinkcache.php '
13+ ];
14+
15+ /**
16+ * Install
17+ * @return void
18+ */
19+ public static function install ()
20+ {
21+ $ config_file = config_path () . '/bootstrap.php ' ;
22+ $ config = include $ config_file ;
23+ if (!in_array (ThinkCache::class, $ config ?? [])) {
24+ $ config_file_content = file_get_contents ($ config_file );
25+ $ config_file_content = preg_replace ('/\];/ ' , " Webman\ThinkCache\ThinkCache::class, \n]; " , $ config_file_content );
26+ file_put_contents ($ config_file , $ config_file_content );
27+ }
28+ /*$thinkcache_file = config_path() . '/thinkcache.php';
29+ if (!is_file($thinkcache_file)) {
30+ copy(__DIR__ . '/config/thinkcache.php', $thinkcache_file);
31+ }*/
32+ static ::installByRelation ();
33+ }
34+
35+ /**
36+ * Uninstall
37+ * @return void
38+ */
39+ public static function uninstall ()
40+ {
41+ $ config_file = config_path () . '/bootstrap.php ' ;
42+ $ config = include $ config_file ;
43+ if (in_array (ThinkCache::class, $ config ?? [])) {
44+ $ config_file = config_path () . '/bootstrap.php ' ;
45+ $ config_file_content = file_get_contents ($ config_file );
46+ $ config_file_content = preg_replace ('/ {0,4}Webman \\\\ThinkCache \\\\ThinkCache::class,?\r?\n?/ ' , '' , $ config_file_content );
47+ file_put_contents ($ config_file , $ config_file_content );
48+ }
49+ self ::uninstallByRelation ();
50+ }
51+
52+ /**
53+ * installByRelation
54+ * @return void
55+ */
56+ public static function installByRelation ()
57+ {
58+ foreach (static ::$ pathRelation as $ source => $ dest ) {
59+ if ($ pos = strrpos ($ dest , '/ ' )) {
60+ $ parent_dir = base_path ().'/ ' .substr ($ dest , 0 , $ pos );
61+ if (!is_dir ($ parent_dir )) {
62+ mkdir ($ parent_dir , 0777 , true );
63+ }
64+ }
65+ //symlink(__DIR__ . "/$source", base_path()."/$dest");
66+ copy_dir (__DIR__ . "/ $ source " , base_path ()."/ $ dest " );
67+ echo "Create $ dest
68+ " ;
69+ }
70+ }
71+
72+ /**
73+ * uninstallByRelation
74+ * @return void
75+ */
76+ public static function uninstallByRelation ()
77+ {
78+ foreach (static ::$ pathRelation as $ source => $ dest ) {
79+ $ path = base_path ()."/ $ dest " ;
80+ if (!is_dir ($ path ) && !is_file ($ path )) {
81+ continue ;
82+ }
83+ echo "Remove $ dest
84+ " ;
85+ if (is_file ($ path ) || is_link ($ path )) {
86+ unlink ($ path );
87+ continue ;
88+ }
89+ remove_dir ($ path );
90+ }
91+ }
92+
93+ }
0 commit comments