Welcome to mirror list, hosted at ThFree Co, Russian Federation.

GenerateAngularDirective.php « Commands « CoreConsole « plugins - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d312cd995f6144b9d8a9777a9471cb8fd1e1637a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link http://piwik.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 *
 */

namespace Piwik\Plugins\CoreConsole\Commands;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 */
class GenerateAngularDirective extends GeneratePluginBase
{
    protected function configure()
    {
        $this->setName('generate:angular-directive')
             ->setDescription('Generates a template for an AngularJS directive')
             ->addOption('pluginname', null, InputOption::VALUE_REQUIRED, 'The name of an existing plugin')
             ->addOption('directive', null, InputOption::VALUE_REQUIRED, 'The name of the directive you want to create.');
    }

    protected function execute(InputInterface $input, OutputInterface $output)
    {
        $pluginName = $this->getPluginName($input, $output);
        $directive  = $this->getDirectiveName($input, $output);
        $pluginPath = $this->getPluginPath($pluginName);

        $directiveLower = $this->getDirectiveComponentName($directive);

        $targetDir = $pluginPath . '/angularjs/' . $directiveLower;

        if (is_dir($targetDir) || file_exists($targetDir)) {
            throw new \Exception('The AngularJS directive ' . $directiveLower . ' already exists in plugin ' . $pluginName);
        }

        $exampleFolder = PIWIK_INCLUDE_PATH . '/plugins/ExamplePlugin';
        $replace       = array(
            'ExamplePlugin'       => $pluginName,
            'directive-component' => $directiveLower,
            'componentClass'      => lcfirst($directive),
            'componentAs'         => lcfirst($directive),
            'component'           => $directiveLower,
            'Component'           => $directive
         );

        $componentPath = '/angularjs/directive-component';

        $whitelistFiles = array(
            '/angularjs',
            $componentPath,
            $componentPath . '/component.controller.js',
            $componentPath . '/component.directive.html',
            $componentPath . '/component.directive.js',
            $componentPath . '/component.directive.less',
        );

        $this->copyTemplateToPlugin($exampleFolder, $pluginName, $replace, $whitelistFiles);

        $replacedBasePath = '/angularjs/' . $directiveLower . '/' . $directiveLower;
        $js1   = $replacedBasePath . '.controller.js';
        $js2   = $replacedBasePath . '.directive.js';
        $less1 = $replacedBasePath . '.directive.less';

        $this->writeSuccessMessage($output, array(
            sprintf('AngularJS directive "%s" for plugin "%s" in "%s" generated', $directive, $pluginName, $targetDir),
            sprintf('In <comment>%1$s/%2$s.php</comment> you should now require the JS files', $pluginPath, $pluginName),
            sprintf('<comment>%1$s%2$s</comment>, <comment>%1$s%3$s</comment>', $pluginPath, $js1, $js2),
            sprintf('and the less file <comment>%1$s%2$s</comment>.', $pluginPath, $less1),
            'If you are not familiar with this have a look at <comment>http://developer.piwik.org/guides/working-with-piwiks-ui</comment>'
        ));
    }

    /**
     * Convert MyComponentName => my-component-name
     * @param  string $directiveCamelCase
     * @return string
     */
    protected function getDirectiveComponentName($directiveCamelCase)
    {
        return strtolower(preg_replace('/([a-zA-Z])(?=[A-Z])/', '$1-', $directiveCamelCase));
    }

    /**
     * @param InputInterface $input
     * @param OutputInterface $output
     * @return string
     * @throws \RuntimeException
     */
    private function getDirectiveName(InputInterface $input, OutputInterface $output)
    {
        $testname = $input->getOption('directive');

        $validate = function ($testname) {
            if (empty($testname)) {
                throw new \InvalidArgumentException('You have to enter a name for the directive');
            }

            if (!ctype_alnum($testname)) {
                throw new \InvalidArgumentException('Only alphanumeric characters are allowed as a directive name. Use CamelCase if the name of your directive contains multiple words.');
            }

            return $testname;
        };

        if (empty($testname)) {
            $dialog   = $this->getHelperSet()->get('dialog');
            $testname = $dialog->askAndValidate($output, 'Enter the name of the directive you want to create: ', $validate);
        } else {
            $validate($testname);
        }

        $testname = ucfirst($testname);

        return $testname;
    }

    protected function getPluginName(InputInterface $input, OutputInterface $output)
    {
        $pluginNames = $this->getPluginNames();
        $invalidName = 'You have to enter the name of an existing plugin';

        return $this->askPluginNameAndValidate($input, $output, $pluginNames, $invalidName);
    }
}