Ejemplo n.º 1
0
 public function getFunctions()
 {
     return [new ExpressionFunction('substr', function ($str, $start, $length = null) {
         return sprintf('substr(%s, %d, %d)', $str, $start, $length);
     }, function (array $values, $str, $start, $length = null) {
         return is_int($length) ? substr($str, $start, $length) : substr($str, $start);
     }), new ExpressionFunction('strpos', function ($haystack, $needle, $offset = 0) {
         return sprintf('strpos(%s, %s, %d)', $haystack, $needle, $offset);
     }, function (array $values, $haystack, $needle, $offset = 0) {
         return strpos($haystack, $needle, $offset);
     }), new ExpressionFunction('strrpos', function ($haystack, $needle, $offset = 0) {
         return sprintf('strrpos(%s, %s, %d)', $haystack, $needle, $offset);
     }, function (array $values, $haystack, $needle, $offset = 0) {
         return strrpos($haystack, $needle, $offset);
     }), new ExpressionFunction('ucfirst', function ($str) {
         return sprintf('ucfirst(%s)', $str);
     }, function (array $values, $str) {
         return ucfirst($str);
     }), new ExpressionFunction('lowercase', function ($str) {
         return sprintf('strtolower(%s)', $str);
     }, function (array $values, $str) {
         return strtolower($str);
     }), new ExpressionFunction('uppercase', function ($str) {
         return sprintf('strtoupper(%s)', $str);
     }, function (array $values, $str) {
         return strtoupper($str);
     }), new ExpressionFunction('replace', function ($search, $replace, $subject) {
         return sprintf('str_replace(%s, %s, %s)', $search, $replace, $subject);
     }, function (array $values, $search, $replace, $subject) {
         return str_replace($search, $replace, $subject);
     }), new ExpressionFunction('preg_replace', function ($search, $replace, $subject) {
         return sprintf('preg_replace(%s, %s, %s)', $search, $replace, $subject);
     }, function (array $values, $search, $replace, $subject) {
         return preg_replace($search, $replace, $subject);
     }), new ExpressionFunction('underscore', function ($input) {
         return sprintf('\\Rollerworks\\Tools\\SkeletonDancer\\StringUtil::underscore(%s)', $input);
     }, function (array $values, $input) {
         return StringUtil::underscore($input);
     }), new ExpressionFunction('camelize', function ($input) {
         return sprintf('\\Rollerworks\\Tools\\SkeletonDancer\\StringUtil::camelize(%s)', $input);
     }, function (array $values, $input) {
         return StringUtil::camelize($input);
     }), new ExpressionFunction('humanize', function ($input) {
         return sprintf('\\Rollerworks\\Tools\\SkeletonDancer\\StringUtil::humanize(%s)', $input);
     }, function (array $values, $input) {
         return StringUtil::humanize($input);
     }), new ExpressionFunction('vendor_namespace', function ($input) {
         return sprintf('\\Rollerworks\\Tools\\SkeletonDancer\\StringUtil::vendorNamespace(%s)', $input);
     }, function (array $values, $input) {
         return StringUtil::vendorNamespace($input);
     }), new ExpressionFunction('nth_dirname', function ($path, $index = 0, $default = '') {
         return sprintf('\\Rollerworks\\Tools\\SkeletonDancer\\StringUtil::getNthDirname(%s, $d, $s)', $path, $index, $default);
     }, function (array $values, $path, $index = 0, $default = '') {
         return StringUtil::getNthDirname($path, $index, $default);
     }), new ExpressionFunction('getenv', function ($key) {
         return sprintf('getenv(%s)', $key);
     }, function (array $values, $path) {
         return getenv($path);
     })];
 }
 public function interact(QuestionsSet $questions)
 {
     $questions->communicate('name', Question::ask('Name'));
     $questions->communicate('package_name', Question::ask('Package name (<vendor>/<name>)', function (array $config) {
         if ('' !== (string) $config['name'] && preg_match('/^(?P<vendor>[a-z0-9_.-]+)\\s+(?P<name>[a-z0-9_.-]+)$/i', $config['name'], $regs)) {
             return strtolower(StringUtil::humanize($regs[1]) . '/' . StringUtil::humanize($regs[2]));
         }
     }, function ($name) {
         if (!preg_match('{^[a-z0-9_.-]+/[a-z0-9_.-]+$}', $name)) {
             throw new \InvalidArgumentException('The package name ' . $name . ' is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+');
         }
         return $name;
     }));
     $questions->communicate('namespace', Question::ask('PHP Namespace', null, function ($namespace) {
         $namespace = trim(str_replace('/', '\\', $namespace), '\\');
         if (!preg_match('/^(?:[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*\\\\?)+$/', $namespace)) {
             throw new \InvalidArgumentException('The namespace contains invalid characters.');
         }
         // validate reserved keywords
         $reserved = self::getReservedWords();
         foreach (explode('\\', $namespace) as $word) {
             if (in_array(strtolower($word), $reserved, true)) {
                 throw new \InvalidArgumentException(sprintf('The namespace cannot contain PHP reserved words ("%s").', $word));
             }
         }
         return $namespace;
     }));
     $questions->communicate('author_name', Question::ask('Author name', function () {
         return $this->git->getGitConfig('user.name', 'global');
     }));
     $questions->communicate('author_email', Question::ask('Author e-mail', function () {
         return $this->git->getGitConfig('user.email', 'global');
     }));
     $questions->communicate('php_min', Question::ask('Php-min', substr(PHP_VERSION, 0, 3)));
     $questions->communicate('src_dir', Question::ask('PHP source directory', 'src', function ($value) {
         return trim($value, '/');
     })->markOptional('.'));
 }