@@ -69,6 +69,7 @@ public function __construct()
69
69
->addAdapter (new GnuFindAdapter ())
70
70
->addAdapter (new BsdFindAdapter ())
71
71
->addAdapter (new PhpAdapter (), -50 )
72
+ ->setAdapter ('php ' )
72
73
;
73
74
}
74
75
@@ -97,11 +98,45 @@ public function addAdapter(Adapter\AdapterInterface $adapter, $priority = 0)
97
98
$ this ->adapters [$ adapter ->getName ()] = array (
98
99
'adapter ' => $ adapter ,
99
100
'priority ' => $ priority ,
101
+ 'selected ' => false ,
100
102
);
101
103
102
104
return $ this ->sortAdapters ();
103
105
}
104
106
107
+ /**
108
+ * Sets the selected adapter to the best one according to the current platform the code is run on.
109
+ *
110
+ * @return Finder The current Finder instance
111
+ */
112
+ public function useBestAdapter ()
113
+ {
114
+ $ this ->resetAdapterSelection ();
115
+
116
+ return $ this ->sortAdapters ();
117
+ }
118
+
119
+ /**
120
+ * Selects the adapter to use.
121
+ *
122
+ * @param string $name
123
+ *
124
+ * @throws \InvalidArgumentException
125
+ *
126
+ * @return Finder The current Finder instance
127
+ */
128
+ public function setAdapter ($ name )
129
+ {
130
+ if (!isset ($ this ->adapters [$ name ])) {
131
+ throw new \InvalidArgumentException (sprintf ('Adapter "%s" does not exist. ' , $ name ));
132
+ }
133
+
134
+ $ this ->resetAdapterSelection ();
135
+ $ this ->adapters [$ name ]['selected ' ] = true ;
136
+
137
+ return $ this ->sortAdapters ();
138
+ }
139
+
105
140
/**
106
141
* Removes all adapters registered in the finder.
107
142
*
@@ -692,12 +727,16 @@ public function count()
692
727
return iterator_count ($ this ->getIterator ());
693
728
}
694
729
695
- /*
730
+ /**
696
731
* @return Finder The current Finder instance
697
732
*/
698
733
private function sortAdapters ()
699
734
{
700
735
uasort ($ this ->adapters , function (array $ a , array $ b ) {
736
+ if ($ a ['selected ' ] || $ b ['selected ' ]) {
737
+ return $ a ['selected ' ] ? -1 : 1 ;
738
+ }
739
+
701
740
return $ a ['priority ' ] > $ b ['priority ' ] ? -1 : 1 ;
702
741
});
703
742
@@ -757,4 +796,16 @@ private function buildAdapter(AdapterInterface $adapter)
757
796
->setPath ($ this ->paths )
758
797
->setNotPath ($ this ->notPaths );
759
798
}
799
+
800
+ /**
801
+ * Unselects all adapters.
802
+ */
803
+ private function resetAdapterSelection ()
804
+ {
805
+ $ this ->adapters = array_map (function (array $ properties ) {
806
+ $ properties ['selected ' ] = false ;
807
+
808
+ return $ properties ;
809
+ }, $ this ->adapters );
810
+ }
760
811
}
0 commit comments