Completed
Push — master ( ec1aed...07def2 )
by Robbie
13s
created

Annotatable::getAnnotator()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 3
rs 10
1
<?php
2
3
namespace SilverLeague\IDEAnnotator;
4
5
use Psr\Container\NotFoundExceptionInterface;
6
use SilverStripe\Control\Director;
7
use SilverStripe\Core\Config\Config;
8
use SilverStripe\Core\Extension;
9
use SilverStripe\Core\Injector\Injector;
10
11
/**
12
 * Class Annotatable
13
 *
14
 * Annotate extension for the provided DataObjects for autocompletion purposes.
15
 * Start annotation, if skipannotation is not set and the annotator is enabled.
16
 *
17
 * @package IDEAnnotator/Extensions
18
 * @property \SilverStripe\Dev\DevBuildController|\SilverLeague\IDEAnnotator\Annotatable $owner
19
 */
20
class Annotatable extends Extension
21
{
22
23
    /**
24
     * Keep track ot the annotation actions for extensions
25
     * An Extension can belong to many DataObjects.
26
     * This prevents that an Extension is ran twice on dev/build
27
     * @var array
28
     */
29
    public static $annotated_extensions = [];
30
    /**
31
     * @var DataObjectAnnotator
32
     */
33
    protected $annotator;
34
    /**
35
     * @var AnnotatePermissionChecker
36
     */
37
    protected $permissionChecker;
38
39
    /**
40
     * Annotated Controllers and Extensions
41
     * @throws NotFoundExceptionInterface
42
     */
43
    public function afterCallActionHandler()
44
    {
45
        $envIsAllowed = Director::isDev() && Config::inst()->get(DataObjectAnnotator::class, 'enabled');
46
        $skipAnnotation = $this->owner->getRequest()->getVar('skipannotation');
0 ignored issues
show
Bug introduced by
The method getRequest() does not exist on SilverLeague\IDEAnnotator\Annotatable. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

46
        $skipAnnotation = $this->owner->/** @scrutinizer ignore-call */ getRequest()->getVar('skipannotation');

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
47
        
48
        // Only instatiate things when we want to run it, this is for when the module is accidentally installed
49
        // on non-dev environments for example
50
        if ($skipAnnotation === null && $envIsAllowed) {
51
            $this->setUp();
52
53
            $this->displayMessage("<div class='build'><p><b>Generating class docblocks</b></p><ul>\n\n");
54
55
            $modules = $this->permissionChecker->enabledModules();
56
            foreach ($modules as $module) {
57
                $this->annotator->annotateModule($module);
58
            }
59
60
            $this->displayMessage("</ul>\n<p><b>Docblock generation finished!</b></p></div>");
61
        }
62
    }
63
64
    /**
65
     * Annotatable setup.
66
     * This is theoretically a constructor, but to save memory we're using setup called from {@see afterCallActionHandler}
67
     * @throws NotFoundExceptionInterface
68
     */
69
    public function setUp()
70
    {
71
        $this->annotator = Injector::inst()->get(DataObjectAnnotator::class);
72
        $this->permissionChecker = Injector::inst()->get(AnnotatePermissionChecker::class);
73
    }
74
75
    /**
76
     * @param $message
77
     */
78
    public function displayMessage($message)
79
    {
80
        echo Director::is_cli() ? "\n" . $message . "\n\n" : "<p><b>$message</b></p>";
81
    }
82
83
    /**
84
     * @return DataObjectAnnotator
85
     */
86
    public function getAnnotator()
87
    {
88
        return $this->annotator;
89
    }
90
91
    /**
92
     * @return AnnotatePermissionChecker
93
     */
94
    public function getPermissionChecker()
95
    {
96
        return $this->permissionChecker;
97
    }
98
}
99