1212
1313defined ('ABSPATH ' ) || exit;
1414
15+ use Framework \Concerns \DiscoversClassFiles ;
1516use Framework \Contracts \Cacheable ;
1617use Framework \Contracts \Discoverable ;
1718use Framework \Supports \Facades \File ;
2324
2425class PolicyDiscovery implements Discoverable, Cacheable
2526{
27+ use DiscoversClassFiles;
28+
2629 /**
2730 * The discovered policies array.
2831 *
@@ -65,15 +68,13 @@ protected function create_cache_file()
6568 return $ this ;
6669 }
6770
68- $ policy_files = glob ($ policies_directory . ' /*.php ' );
71+ $ policy_files = $ this -> class_files ($ policies_directory );
6972
7073 if (empty ($ policy_files )) {
7174 return $ this ;
7275 }
7376
74- foreach ($ policy_files as $ policy_file ) {
75- $ policy_name = $ this ->filename ($ policy_file );
76-
77+ foreach ($ policy_files as $ policy_name ) {
7778 if (!$ this ->is_valid_policy_name ($ policy_name )) {
7879 continue ;
7980 }
@@ -87,20 +88,6 @@ protected function create_cache_file()
8788 return $ this ;
8889 }
8990
90- /**
91- * Get the filename of the policy.
92- *
93- * @param string $path The path.
94- *
95- * @return string
96- *
97- * @since 1.0.0
98- */
99- protected function filename (string $ path )
100- {
101- return basename ($ path , '.php ' );
102- }
103-
10491 /**
10592 * Add a policy to the policies array.
10693 *
@@ -148,6 +135,9 @@ protected function policy_class(string $policy_name)
148135 /**
149136 * Get the model class from the policy name.
150137 *
138+ * A policy in a subfolder maps to a model in the matching subfolder, so
139+ * `Policies\Shop\ProductPolicy` is paired with `Models\Shop\Product`.
140+ *
151141 * @param string $policy_name The policy name.
152142 *
153143 * @return string
0 commit comments