1414 * @author Robin McCorkell <robin@mccorkell.me.uk>
1515 * @author Roeland Jago Douma <roeland@famdouma.nl>
1616 * @author Roger Szabo <roger.szabo@web.de>
17+ * @author Carl Schwan <carl@carlschwan.eu>
1718 *
1819 * @license AGPL-3.0
1920 *
3233 */
3334namespace OCA \User_LDAP ;
3435
36+ use OCP \Profiler \IProfiler ;
3537use OC \ServerNotAvailableException ;
38+ use OCA \User_LDAP \DataCollector \LdapDataCollector ;
3639use OCA \User_LDAP \Exceptions \ConstraintViolationException ;
3740use OCA \User_LDAP \PagedResults \IAdapter ;
3841use OCA \User_LDAP \PagedResults \Php73 ;
@@ -45,9 +48,18 @@ class LDAP implements ILDAPWrapper {
4548 /** @var IAdapter */
4649 protected $ pagedResultsAdapter ;
4750
51+ private ?LdapDataCollector $ dataCollector = null ;
52+
4853 public function __construct (string $ logFile = '' ) {
4954 $ this ->pagedResultsAdapter = new Php73 ();
5055 $ this ->logFile = $ logFile ;
56+
57+ /** @var IProfiler $profiler */
58+ $ profiler = \OC ::$ server ->get (IProfiler::class);
59+ if ($ profiler ->isEnabled ()) {
60+ $ this ->dataCollector = new LdapDataCollector ();
61+ $ profiler ->add ($ this ->dataCollector );
62+ }
5163 }
5264
5365 /**
@@ -295,24 +307,26 @@ protected function invokeLDAPMethod() {
295307 if ($ this ->isResultFalse ($ result )) {
296308 $ this ->postFunctionCall ();
297309 }
310+ if ($ this ->dataCollector !== null ) {
311+ $ this ->dataCollector ->stopLastLdapRequest ();
312+ }
298313 return $ result ;
299314 }
300315 return null ;
301316 }
302317
303- /**
304- * @param string $functionName
305- * @param array $args
306- */
307- private function preFunctionCall ($ functionName , $ args ) {
318+ private function preFunctionCall (string $ functionName , array $ args ): void {
308319 $ this ->curFunc = $ functionName ;
309320 $ this ->curArgs = $ args ;
310321
322+ if ($ this ->dataCollector !== null ) {
323+ $ args = array_map (fn ($ item ) => (!$ this ->isResource ($ item ) ? $ item : '(resource) ' ), $ this ->curArgs );
324+
325+ $ this ->dataCollector ->startLdapRequest ($ this ->curFunc , $ args );
326+ }
327+
311328 if ($ this ->logFile !== '' && is_writable (dirname ($ this ->logFile )) && (!file_exists ($ this ->logFile ) || is_writable ($ this ->logFile ))) {
312- $ args = array_reduce ($ this ->curArgs , static function (array $ carry , $ item ): array {
313- $ carry [] = !is_resource ($ item ) ? $ item : '(resource) ' ;
314- return $ carry ;
315- }, []);
329+ $ args = array_map (fn ($ item ) => (!$ this ->isResource ($ item ) ? $ item : '(resource) ' ), $ this ->curArgs );
316330 file_put_contents (
317331 $ this ->logFile ,
318332 $ this ->curFunc . ':: ' . json_encode ($ args ) . "\n" ,
0 commit comments