Completed
Pull Request — master (#107)
by Simon
01:47
created

Annotatable::afterCallActionHandler()   B

Complexity

Conditions 5
Paths 6

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 9
nc 6
nop 0
dl 0
loc 18
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\Extension;
11
use SilverStripe\Core\Injector\Injector;
12
13
/**
14
 * Class Annotatable
15
 *
16
 * Annotate extension for the provided DataObjects for autocompletion purposes.
17
 * Start annotation, if skipannotation is not set and the annotator is enabled.
18
 *
19
 * @package IDEAnnotator/Extensions
20
 * @property \SilverStripe\Dev\DevBuildController|\SilverLeague\IDEAnnotator\Annotatable $owner
21
 */
22
class Annotatable extends Extension
23
{
24
25
    /**
26
     * Keep track ot the annotation actions for extensions
27
     * An Extension can belong to many DataObjects.
28
     * This prevents that an Extension is ran twice on dev/build
29
     * @var array
30
     */
31
    public static $annotated_extensions = [];
32
    /**
33
     * @var DataObjectAnnotator
34
     */
35
    protected $annotator;
36
    /**
37
     * @var AnnotatePermissionChecker
38
     */
39
    protected $permissionChecker;
40
41
    /**
42
     * Annotated Controllers and Extensions
43
     * @return bool // Return a boolean for test-purposes
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
        return false;
70
    }
71
72
    /**
73
     * Annotatable setup.
74
     * This is theoretically a constructor, but to save memory we're using setup
75
     * called from {@see afterCallActionHandler}
76
     * @throws NotFoundExceptionInterface
77
     */
78
    public function setUp()
79
    {
80
        $this->annotator = Injector::inst()->get(DataObjectAnnotator::class);
81
        $this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class);
82
    }
83
84
    /**
85
     * @param string $message
86
     * @param bool $heading
87
     * @param bool $end
88
     */
89
    public function displayMessage($message, $heading = false, $end = false)
90
    {
91
        if ($heading) {
92
            if (!$end) {
93
                echo Director::is_cli() ?
94
                    strtoupper("\n$message\n\n") :
95
                    "<div class='build'><p><b>$message</b><ul>";
96
            } else {
97
                echo Director::is_cli() ? strtoupper("\n" . $message) : "</ul><p><b>$message</b></b></div>";
98
            }
99
        } else {
100
            echo Director::is_cli() ? "\n$message\n\n" : "<li>$message</li>";
101
        }
102
    }
103
104
    /**
105
     * @return DataObjectAnnotator
106
     */
107
    public function getAnnotator()
108
    {
109
        return $this->annotator;
110
    }
111
112
    /**
113
     * @return AnnotatePermissionChecker
114
     */
115
    public function getPermissionChecker()
116
    {
117
        return $this->permissionChecker;
118
    }
119
}
120