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

Annotatable::displayMessage()   B

Complexity

Conditions 6
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 9
nc 3
nop 3
dl 0
loc 12
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
            return true;
67
        }
68
    }
69
70
    /**
71
     * Annotatable setup.
72
     * This is theoretically a constructor, but to save memory we're using setup
73
     * called from {@see afterCallActionHandler}
74
     * @throws NotFoundExceptionInterface
75
     */
76
    public function setUp()
77
    {
78
        $this->annotator = Injector::inst()->get(DataObjectAnnotator::class);
79
        $this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class);
80
    }
81
82
    /**
83
     * @param $message
84
     */
85
    public function displayMessage($message, $heading = false, $end = false)
86
    {
87
        if ($heading) {
88
            if (!$end) {
89
                echo Director::is_cli() ?
90
                    strtoupper("\n$message\n\n") :
91
                    "<div class='build'><p><b>$message</b><ul>";
92
            } else {
93
                echo Director::is_cli() ? strtoupper("\n" . $message) : "</ul><p><b>$message</b></b></div>";
94
            }
95
        } else {
96
            echo Director::is_cli() ? "\n$message\n\n" : "<li>$message</li>";
97
        }
98
    }
99
100
    /**
101
     * @return DataObjectAnnotator
102
     */
103
    public function getAnnotator()
104
    {
105
        return $this->annotator;
106
    }
107
108
    /**
109
     * @return AnnotatePermissionChecker
110
     */
111
    public function getPermissionChecker()
112
    {
113
        return $this->permissionChecker;
114
    }
115
}
116