Completed
Pull Request — master (#107)
by Simon
02:49
created

Annotatable::displayMessage()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 7
nc 3
nop 3
dl 0
loc 10
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SilverLeague\IDEAnnotator\Extensions;
4
5
use Psr\Container\NotFoundExceptionInterface;
6
use ReflectionException;
7
use SilverLeague\IDEAnnotator\DataObjectAnnotator;
8
use SilverLeague\IDEAnnotator\Helpers\AnnotatePermissionChecker;
9
use SilverStripe\Control\Director;
10
use SilverStripe\Core\Config\Config;
11
use SilverStripe\Core\Extension;
12
use SilverStripe\Core\Injector\Injector;
13
14
/**
15
 * Class Annotatable
16
 *
17
 * Annotate extension for the provided DataObjects for autocompletion purposes.
18
 * Start annotation, if skipannotation is not set and the annotator is enabled.
19
 *
20
 * @package IDEAnnotator/Extensions
21
 * @property \SilverStripe\Dev\DevBuildController|\SilverLeague\IDEAnnotator\Annotatable $owner
22
 */
23
class Annotatable extends Extension
24
{
25
26
    /**
27
     * Keep track ot the annotation actions for extensions
28
     * An Extension can belong to many DataObjects.
29
     * This prevents that an Extension is ran twice on dev/build
30
     * @var array
31
     */
32
    public static $annotated_extensions = [];
33
    /**
34
     * @var DataObjectAnnotator
35
     */
36
    protected $annotator;
37
    /**
38
     * @var AnnotatePermissionChecker
39
     */
40
    protected $permissionChecker;
41
42
    /**
43
     * Annotated Controllers and Extensions
44
     * @throws NotFoundExceptionInterface
45
     * @throws ReflectionException
46
     */
47
    public function afterCallActionHandler()
48
    {
49
        $envIsAllowed = Director::isDev() && DataObjectAnnotator::config()->get('enabled');
50
        $skipAnnotation = $this->owner->getRequest()->getVar('skipannotation');
51
52
        // Only instatiate things when we want to run it, this is for when the module is accidentally installed
53
        // on non-dev environments for example
54
        if ($skipAnnotation === null && $envIsAllowed) {
55
            $this->setUp();
56
57
            $this->displayMessage("Generating class docblocks", true, false);
58
59
            $modules = $this->permissionChecker->enabledModules();
60
            foreach ($modules as $module) {
61
                $this->annotator->annotateModule($module);
62
            }
63
64
            $this->displayMessage("Docblock generation finished!", true, true);
65
        }
66
    }
67
68
    /**
69
     * Annotatable setup.
70
     * This is theoretically a constructor, but to save memory we're using setup called from {@see afterCallActionHandler}
71
     * @throws NotFoundExceptionInterface
72
     */
73
    public function setUp()
74
    {
75
        $this->annotator = Injector::inst()->get(DataObjectAnnotator::class);
76
        $this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class);
77
    }
78
79
    /**
80
     * @param $message
81
     */
82
    public function displayMessage($message, $heading = false, $end = false)
83
    {
84
        if ($heading) {
85
            if (!$end) {
86
                echo Director::is_cli() ? strtoupper("\n" . $message . "\n\n") : "<div class='build'><p><b>$message</b><ul>";
87
            } else {
88
                echo Director::is_cli() ? strtoupper("\n" . $message) : "</ul><p><b>$message</b></b></div>";
89
            }
90
        } else {
91
            echo Director::is_cli() ? "\n" . $message . "\n\n" : "<li>$message</li>";
92
        }
93
    }
94
95
    /**
96
     * @return DataObjectAnnotator
97
     */
98
    public function getAnnotator()
99
    {
100
        return $this->annotator;
101
    }
102
103
    /**
104
     * @return AnnotatePermissionChecker
105
     */
106
    public function getPermissionChecker()
107
    {
108
        return $this->permissionChecker;
109
    }
110
}
111