1 <?php
2 3 4 5 6 7
8
9 defined('FOF_INCLUDED') or die();
10
11 12 13 14 15 16 17 18 19 20
21 class FOFAutoloaderComponent
22 {
23 24 25 26 27
28 public static $autoloader = null;
29
30 31 32 33 34
35 public static $fofPath = null;
36
37 38 39 40 41
42 protected static $fofComponents = array();
43
44 45 46 47 48
49 public static function init()
50 {
51 if (self::$autoloader == null)
52 {
53 self::$autoloader = new self;
54 }
55
56 return self::$autoloader;
57 }
58
59 60 61
62 public function __construct()
63 {
64 self::$fofPath = realpath(__DIR__ . '/../');
65
66 spl_autoload_register(array($this,'autoload_fof_controller'));
67 spl_autoload_register(array($this,'autoload_fof_model'));
68 spl_autoload_register(array($this,'autoload_fof_view'));
69 spl_autoload_register(array($this,'autoload_fof_table'));
70 spl_autoload_register(array($this,'autoload_fof_helper'));
71 spl_autoload_register(array($this,'autoload_fof_toolbar'));
72 spl_autoload_register(array($this,'autoload_fof_field'));
73 }
74
75 76 77 78 79 80 81 82
83 public function isFOFComponent($component)
84 {
85 if (!isset($fofComponents[$component]))
86 {
87 $componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($component);
88 $fofComponents[$component] = file_exists($componentPaths['admin'] . '/fof.xml');
89 }
90
91 return $fofComponents[$component];
92 }
93
94 95 96 97 98 99 100 101 102 103 104 105 106
107 private function class_alias($original, $alias, $autoload = true)
108 {
109 static $hasEval = null;
110
111 if (is_null($hasEval))
112 {
113 $hasEval = false;
114
115 if (function_exists('ini_get'))
116 {
117 $disabled_functions = ini_get('disabled_functions');
118
119 if (!is_string($disabled_functions))
120 {
121 $hasEval = true;
122 }
123 else
124 {
125 $disabled_functions = explode(',', $disabled_functions);
126 $hasEval = !in_array('eval', $disabled_functions);
127 }
128 }
129 }
130
131 if (!class_exists($original, $autoload))
132 {
133 return;
134 }
135
136 if ($hasEval)
137 {
138 $phpCode = "class $alias extends $original {}";
139 eval($phpCode);
140 }
141 else
142 {
143 class_alias($original, $alias, $autoload);
144 }
145 }
146
147 148 149 150 151 152 153
154 public function autoload_fof_controller($class_name)
155 {
156 FOFPlatform::getInstance()->logDebug(__METHOD__ . "() autoloading $class_name");
157
158 static $isCli = null, $isAdmin = null;
159
160 if (is_null($isCli) && is_null($isAdmin))
161 {
162 list($isCli, $isAdmin) = FOFDispatcher::isCliAdmin();
163 }
164
165 if (strpos($class_name, 'Controller') === false)
166 {
167 return;
168 }
169
170
171 $class_modified = preg_replace('/(\s)+/', '_', $class_name);
172 $class_modified = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $class_modified));
173 $parts = explode('_', $class_modified);
174
175
176 if (count($parts) != 3)
177 {
178 return;
179 }
180
181
182 if ($parts[1] != 'controller')
183 {
184 return;
185 }
186
187
188 $component_raw = $parts[0];
189 $component = 'com_' . $parts[0];
190 $view = $parts[2];
191
192
193 if (!$this->isFOFComponent($component))
194 {
195 return;
196 }
197
198
199 $alt_view = FOFInflector::isSingular($view) ? FOFInflector::pluralize($view) : FOFInflector::singularize($view);
200 $alt_class = FOFInflector::camelize($component_raw . '_controller_' . $alt_view);
201
202
203 $componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($component);
204
205
206 $file = "/controllers/$view.php";
207 $altFile = "/controllers/$alt_view.php";
208 $path = $componentPaths['main'];
209 $altPath = $componentPaths['alt'];
210
211
212 if (file_exists($path . $file))
213 {
214 @include_once $path . $file;
215 }
216
217
218 if (!class_exists($class_name) && file_exists($altPath . $file))
219 {
220 @include_once $altPath . $file;
221 }
222
223
224 if (!class_exists($alt_class) && file_exists($path . $altFile))
225 {
226 @include_once $path . $altFile;
227 }
228
229
230 if (!class_exists($alt_class) && file_exists($altPath . $altFile))
231 {
232 @include_once $altPath . $altFile;
233 }
234
235
236 if (!class_exists($class_name) && class_exists($alt_class))
237 {
238 $this->class_alias($alt_class, $class_name);
239 }
240
241
242 elseif (!class_exists($class_name))
243 {
244 if ($view != 'default')
245 {
246 $defaultClass = FOFInflector::camelize($component_raw . '_controller_default');
247 $this->class_alias($defaultClass, $class_name);
248 }
249 else
250 {
251 $this->class_alias('FOFController', $class_name);
252 }
253 }
254 }
255
256 257 258 259 260 261 262
263 public function autoload_fof_model($class_name)
264 {
265 FOFPlatform::getInstance()->logDebug(__METHOD__ . "() autoloading $class_name");
266
267 static $isCli = null, $isAdmin = null;
268
269 if (is_null($isCli) && is_null($isAdmin))
270 {
271 list($isCli, $isAdmin) = FOFDispatcher::isCliAdmin();
272 }
273
274 if (strpos($class_name, 'Model') === false)
275 {
276 return;
277 }
278
279
280 $class_modified = preg_replace('/(\s)+/', '_', $class_name);
281 $class_modified = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $class_modified));
282 $parts = explode('_', $class_modified);
283
284
285 if (count($parts) != 3)
286 {
287 return;
288 }
289
290
291 if ($parts[1] != 'model')
292 {
293 return;
294 }
295
296
297 $component_raw = $parts[0];
298 $component = 'com_' . $parts[0];
299 $view = $parts[2];
300
301
302 if (!$this->isFOFComponent($component))
303 {
304 return;
305 }
306
307
308 $alt_view = FOFInflector::isSingular($view) ? FOFInflector::pluralize($view) : FOFInflector::singularize($view);
309 $alt_class = FOFInflector::camelize($component_raw . '_model_' . $alt_view);
310
311
312 $componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($component);
313
314 $file = "/models/$view.php";
315 $altFile = "/models/$alt_view.php";
316 $path = $componentPaths['main'];
317 $altPath = $componentPaths['alt'];
318
319
320 if (file_exists($path . $file))
321 {
322 @include_once $path . $file;
323 }
324
325
326 if (!class_exists($class_name) && file_exists($altPath . $file))
327 {
328 @include_once $altPath . $file;
329 }
330
331
332 if (!class_exists($alt_class) && file_exists($path . $altFile))
333 {
334 @include_once $path . $altFile;
335 }
336
337
338 if (!class_exists($alt_class) && file_exists($altPath . $altFile))
339 {
340 @include_once $altPath . $altFile;
341 }
342
343
344 if (!class_exists($class_name) && class_exists($alt_class))
345 {
346 $this->class_alias($alt_class, $class_name);
347 }
348
349
350 elseif (!class_exists($class_name))
351 {
352 if ($view != 'default')
353 {
354 $defaultClass = FOFInflector::camelize($component_raw . '_model_default');
355 $this->class_alias($defaultClass, $class_name);
356 }
357 else
358 {
359 $this->class_alias('FOFModel', $class_name, true);
360 }
361 }
362 }
363
364 365 366 367 368 369 370
371 public function autoload_fof_view($class_name)
372 {
373 FOFPlatform::getInstance()->logDebug(__METHOD__ . "() autoloading $class_name");
374
375 static $isCli = null, $isAdmin = null;
376
377 if (is_null($isCli) && is_null($isAdmin))
378 {
379 list($isCli, $isAdmin) = FOFDispatcher::isCliAdmin();
380 }
381
382 if (strpos($class_name, 'View') === false)
383 {
384 return;
385 }
386
387
388 $class_modified = preg_replace('/(\s)+/', '_', $class_name);
389 $class_modified = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $class_modified));
390 $parts = explode('_', $class_modified);
391
392
393
394 if (count($parts) < 3)
395 {
396 return;
397 }
398
399
400
401 if ($parts[1] != 'view')
402 {
403 return;
404 }
405
406
407 $component_raw = $parts[0];
408 $component = 'com_' . $parts[0];
409 $view = $parts[2];
410
411 if (count($parts) > 3)
412 {
413 $format = $parts[3];
414 }
415 else
416 {
417 $input = new FOFInput;
418 $format = $input->getCmd('format', 'html', 'cmd');
419 }
420
421
422 if (!$this->isFOFComponent($component))
423 {
424 return;
425 }
426
427
428 $alt_view = FOFInflector::isSingular($view) ? FOFInflector::pluralize($view) : FOFInflector::singularize($view);
429 $alt_class = FOFInflector::camelize($component_raw . '_view_' . $alt_view);
430
431
432 $componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($component);
433
434 $protoFile = "/models/$view";
435 $protoAltFile = "/models/$alt_view";
436 $path = $componentPaths['main'];
437 $altPath = $componentPaths['alt'];
438
439 $formats = array($format);
440
441 if ($format != 'html')
442 {
443 $formats[] = 'raw';
444 }
445
446 foreach ($formats as $currentFormat)
447 {
448 $file = $protoFile . '.' . $currentFormat . '.php';
449 $altFile = $protoAltFile . '.' . $currentFormat . '.php';
450
451
452 if (!class_exists($class_name) && file_exists($path . $file))
453 {
454 @include_once $path . $file;
455 }
456
457
458 if (!class_exists($class_name) && file_exists($altPath . $file))
459 {
460 @include_once $altPath . $file;
461 }
462
463
464 if (!class_exists($alt_class) && file_exists($path . $altFile))
465 {
466 @include_once $path . $altFile;
467 }
468
469
470 if (!class_exists($alt_class) && file_exists($altPath . $altFile))
471 {
472 @include_once $altPath . $altFile;
473 }
474 }
475
476
477 if (!class_exists($class_name) && class_exists($alt_class))
478 {
479 $this->class_alias($alt_class, $class_name);
480 }
481
482
483 elseif (!class_exists($class_name))
484 {
485 if ($view != 'default')
486 {
487 $defaultClass = FOFInflector::camelize($component_raw . '_view_default');
488 $this->class_alias($defaultClass, $class_name);
489 }
490 else
491 {
492 if (!file_exists(self::$fofPath . '/view/' . $format . '.php'))
493 {
494 $default_class = 'FOFView';
495 }
496 else
497 {
498 $default_class = 'FOFView' . ucfirst($format);
499 }
500
501 $this->class_alias($default_class, $class_name, true);
502 }
503 }
504 }
505
506 507 508 509 510 511 512
513 public function autoload_fof_table($class_name)
514 {
515 FOFPlatform::getInstance()->logDebug(__METHOD__ . "() autoloading $class_name");
516
517 static $isCli = null, $isAdmin = null;
518
519 if (is_null($isCli) && is_null($isAdmin))
520 {
521 list($isCli, $isAdmin) = FOFDispatcher::isCliAdmin();
522 }
523
524 if (strpos($class_name, 'Table') === false)
525 {
526 return;
527 }
528
529
530 $class_modified = preg_replace('/(\s)+/', '_', $class_name);
531 $class_modified = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $class_modified));
532 $parts = explode('_', $class_modified);
533
534
535
536 if (count($parts) != 3)
537 {
538 return;
539 }
540
541
542 if ($parts[1] != 'table')
543 {
544 return;
545 }
546
547
548 $component_raw = $parts[0];
549 $component = 'com_' . $parts[0];
550 $view = $parts[2];
551
552
553 if (!$this->isFOFComponent($component))
554 {
555 return;
556 }
557
558
559 $alt_view = FOFInflector::isSingular($view) ? FOFInflector::pluralize($view) : FOFInflector::singularize($view);
560 $alt_class = FOFInflector::camelize($component_raw . '_table_' . $alt_view);
561
562
563 $componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($component);
564
565 $file = "/tables/$view.php";
566 $altFile = "/tables/$alt_view.php";
567 $path = $componentPaths['admin'];
568
569
570 if (file_exists($path . $file))
571 {
572 @include_once $path . $file;
573 }
574
575
576 if (!class_exists($alt_class) && file_exists($path . $altFile))
577 {
578 @include_once $path . $altFile;
579 }
580
581
582 if (!class_exists($class_name) && class_exists($alt_class))
583 {
584 $this->class_alias($alt_class, $class_name);
585 }
586
587
588 elseif (!class_exists($class_name))
589 {
590 if ($view != 'default')
591 {
592 $defaultClass = FOFInflector::camelize($component_raw . '_table_default');
593 $this->class_alias($defaultClass, $class_name);
594 }
595 else
596 {
597 $this->class_alias('FOFTable', $class_name, true);
598 }
599 }
600 }
601
602 603 604 605 606 607 608
609 public function autoload_fof_helper($class_name)
610 {
611 FOFPlatform::getInstance()->logDebug(__METHOD__ . "() autoloading $class_name");
612
613 static $isCli = null, $isAdmin = null;
614
615 if (is_null($isCli) && is_null($isAdmin))
616 {
617 list($isCli, $isAdmin) = FOFDispatcher::isCliAdmin();
618 }
619
620 if (strpos($class_name, 'Helper') === false)
621 {
622 return;
623 }
624
625
626 $class_modified = preg_replace('/(\s)+/', '_', $class_name);
627 $class_modified = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $class_modified));
628 $parts = explode('_', $class_modified);
629
630
631 if (count($parts) != 3)
632 {
633 return;
634 }
635
636
637 if ($parts[1] != 'helper')
638 {
639 return;
640 }
641
642
643 $component_raw = $parts[0];
644 $component = 'com_' . $parts[0];
645 $view = $parts[2];
646
647
648 if (!$this->isFOFComponent($component))
649 {
650 return;
651 }
652
653
654 $alt_view = FOFInflector::isSingular($view) ? FOFInflector::pluralize($view) : FOFInflector::singularize($view);
655 $alt_class = FOFInflector::camelize($component_raw . '_helper_' . $alt_view);
656
657
658 $componentPaths = FOFPlatform::getInstance()->getComponentBaseDirs($component);
659
660 $file = "/helpers/$view.php";
661 $altFile = "/helpers/$alt_view.php";
662 $path = $componentPaths['main'];
663 $altPath = $componentPaths['alt'];
664
665
666 if (file_exists($path . $file))
667 {
668 @include_once $path . $file;
669 }
670
671
672 if (!class_exists($class_name) && file_exists($altPath . $file))
673 {
674 @include_once $altPath . $file;
675 }
676
677
678 if (!class_exists($alt_class) && file_exists($path . $altFile))
679 {
680 @include_once $path . $altFile;
681 }
682
683
684 if (!class_exists($alt_class) && file_exists($altPath . $altFile))
685 {
686 @include_once $altPath . $altFile;
687 }
688
689
690 if (!class_exists($class_name) && class_exists($alt_class))
691 {
692 $this->class_alias($alt_class, $class_name);
693 }
694 }
695
696 697 698 699 700 701 702
703 public function autoload_fof_toolbar($class_name)
704 {
705 FOFPlatform::getInstance()->logDebug(__METHOD__ . "() autoloading $class_name");
706
707 static $isCli = null, $isAdmin = null;
708
709 if (is_null($isCli) && is_null($isAdmin))
710 {
711 list($isCli, $isAdmin) = FOFDispatcher::isCliAdmin();
712 }
713
714 if (strpos($class_name, 'Toolbar') === false)
715 {
716 return;
717 }
718
719
720 $class_modified = preg_replace('/(\s)+/', '_', $class_name);
721 $class_modified = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $class_modified));
722 $parts = explode('_', $class_modified);
723
724
725 if (count($parts) != 2)
726 {
727 return;
728 }
729
730
731 if ($parts[1] != 'toolbar')
732 {
733 return;
734 }
735
736
737 $component_raw = $parts[0];
738 $component = 'com_' . $parts[0];
739
740 $platformDirs = FOFPlatform::getInstance()->getPlatformBaseDirs();
741
742
743 $file = "/components/$component/toolbar.php";
744 $path = ($isAdmin || $isCli) ? $platformDirs['admin'] : $platformDirs['public'];
745 $altPath = ($isAdmin || $isCli) ? $platformDirs['public'] : $platformDirs['admin'];
746
747
748
749 if (file_exists($path . $file))
750 {
751 @include_once $path . $file;
752 }
753
754
755 if (!class_exists($class_name) && file_exists($altPath . $file))
756 {
757 @include_once $altPath . $file;
758 }
759
760
761 if (!class_exists($class_name))
762 {
763 $this->class_alias('FOFToolbar', $class_name, true);
764 }
765 }
766
767 768 769 770 771 772 773
774 public function autoload_fof_field($class_name)
775 {
776 FOFPlatform::getInstance()->logDebug(__METHOD__ . "() autoloading $class_name");
777
778
779 }
780 }
781