Skip to content

Commit 50ea1e7

Browse files
committed
feat: 改為 autoload
1 parent a31d058 commit 50ea1e7

File tree

12 files changed

+209
-197
lines changed

12 files changed

+209
-197
lines changed

.prettierrc.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@
3535
}
3636
}
3737
]
38-
}
38+
}

composer.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717
"url": "https://wpackagist.org"
1818
}
1919
],
20+
"autoload": {
21+
"psr-4": {
22+
"J7\\WpReactPlugin\\": "inc/classes/"
23+
}
24+
},
2025
"config": {
2126
"allow-plugins": {
2227
"dealerdirect/phpcodesniffer-composer-installer": true,

inc/class/admin/index.php

Lines changed: 0 additions & 2 deletions
This file was deleted.

inc/class/front-end/index.php

Lines changed: 0 additions & 2 deletions
This file was deleted.

inc/class/utils/index.php

Lines changed: 0 additions & 2 deletions
This file was deleted.

inc/class/admin/class-cpt.php renamed to inc/classes/Admin/CPT.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77

88
namespace J7\WpReactPlugin\Admin;
99

10-
use J7\WpReactPlugin\Utils\Base;
1110
use J7\WpReactPlugin\Plugin;
1211

12+
if (class_exists('J7\WpReactPlugin\Admin\CPT')) {
13+
return;
14+
}
1315
/**
1416
* Class CPT
1517
*/
@@ -283,5 +285,3 @@ public function load_custom_template( $template ) {
283285
return $template;
284286
}
285287
}
286-
287-
CPT::instance();

inc/class/class-bootstrap.php renamed to inc/classes/Bootstrap.php

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,22 +10,30 @@
1010
use J7\WpReactPlugin\Utils\Base;
1111
use Kucrut\Vite;
1212

13+
if ( class_exists( 'J7\WpReactPlugin\Bootstrap' ) ) {
14+
return;
15+
}
1316
/**
1417
* Class Bootstrap
1518
*/
1619
final class Bootstrap {
1720
use \J7\WpUtils\Traits\SingletonTrait;
1821

22+
/**
23+
* @var array
24+
* Store instances of classes
25+
*/
26+
public $settings = [];
27+
1928
/**
2029
* Constructor
2130
*/
2231
public function __construct() {
23-
require_once __DIR__ . '/utils/index.php';
24-
require_once __DIR__ . '/admin/index.php';
25-
require_once __DIR__ . '/front-end/index.php';
32+
$this->settings['FrontEnd\Entry'] = FrontEnd\Entry::instance();
33+
$this->settings['Admin\CPT'] = Admin\CPT::instance();
2634

27-
\add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_script' ], 99 );
28-
\add_action( 'wp_enqueue_scripts', [ $this, 'frontend_enqueue_script' ], 99 );
35+
\add_action( 'admin_enqueue_scripts', [ __CLASS__, 'admin_enqueue_script' ], 99 );
36+
\add_action( 'wp_enqueue_scripts', [ __CLASS__, 'frontend_enqueue_script' ], 99 );
2937
}
3038

3139
/**
@@ -36,8 +44,8 @@ public function __construct() {
3644
*
3745
* @return void
3846
*/
39-
public function admin_enqueue_script( $hook ): void {
40-
$this->enqueue_script();
47+
public static function admin_enqueue_script( $hook ): void {
48+
self::enqueue_script();
4149
}
4250

4351

@@ -47,8 +55,8 @@ public function admin_enqueue_script( $hook ): void {
4755
*
4856
* @return void
4957
*/
50-
public function frontend_enqueue_script(): void {
51-
$this->enqueue_script();
58+
public static function frontend_enqueue_script(): void {
59+
self::enqueue_script();
5260
}
5361

5462
/**
@@ -57,7 +65,7 @@ public function frontend_enqueue_script(): void {
5765
*
5866
* @return void
5967
*/
60-
public function enqueue_script(): void {
68+
public static function enqueue_script(): void {
6169

6270
Vite\enqueue_asset(
6371
Plugin::$dir . '/js/dist',

inc/class/front-end/class-entry.php renamed to inc/classes/FrontEnd/Entry.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
namespace J7\WpReactPlugin\FrontEnd;
99

10-
use J7\WpReactPlugin\Utils\Base;
10+
if (class_exists('J7\WpReactPlugin\FrontEnd\Entry')) {
11+
return;
12+
}
1113
/**
1214
* Class Entry
1315
*/
@@ -18,16 +20,14 @@ final class Entry {
1820
* Constructor
1921
*/
2022
public function __construct() {
21-
\add_action( 'wp_footer', [ $this, 'render_app' ] );
23+
\add_action( 'wp_footer', [ __CLASS__, 'render_app' ] );
2224
}
2325

2426
/**
2527
* Render application's markup
2628
*/
27-
public function render_app(): void {
29+
public static function render_app(): void {
2830
// phpcs:ignore
2931
echo '<div id="my_app"></div>';
3032
}
3133
}
32-
33-
Entry::instance();

inc/class/utils/class-base.php renamed to inc/classes/Utils/Base.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
namespace J7\WpReactPlugin\Utils;
99

10+
if (class_exists('J7\WpReactPlugin\Utils\Base')) {
11+
return;
12+
}
1013
/**
1114
* Class Base
1215
*/

js/dist/vite-dev-server.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"base":"/","origin":"http://localhost:5173","port":5173,"plugins":["vite:react-refresh"]}

0 commit comments

Comments
 (0)