Example #1
0
 /**
  * creates path/url generators for each model
  */
 public function initialize()
 {
     foreach (glob('app/models/*.php') as $model) {
         preg_match('/app\\/models\\/(.+).php/', $model, $model);
         $model = strtolower(array_pop($model));
         $models = $this->word->pluralize($model);
         // add_task_path()
         $this->twig->addFunction($this->urlGenerator(Crud::ADD_ACTION, $model, $models));
         // edit_task_path(task)
         $this->twig->addFunction($this->urlGenerator(Crud::EDIT_ACTION, $model, $models, true));
         // create_task_path
         $this->twig->addFunction($this->urlGenerator(Crud::CREATE_ACTION, $model, $models));
         // update_task_path(task)
         $this->twig->addFunction($this->urlGenerator(Crud::UPDATE_ACTION, $model, $models, true));
         // delete_task_path(task)
         $this->twig->addFunction($this->urlGenerator(Crud::DELETE_ACTION, $model, $models, true));
         // tasks_path
         $this->twig->addFunction(new Twig_SimpleFunction(sprintf('%s_path', $models), function () use($models) {
             return sprintf('/%s', $models);
         }));
     }
 }
Example #2
0
 protected function resource($resource)
 {
     $key = 0;
     $base_class_name = function ($full_class) {
         $parts = explode('\\', $full_class);
         return array_pop($parts);
     };
     if ($resource instanceof Collection) {
         $word = new Word();
         $model = $base_class_name($resource->collectionOf());
         $key = strtolower($word->pluralize($model));
     } elseif ($resource instanceof Model) {
         $model = $base_class_name(get_class($resource));
         $key = strtolower($model);
     }
     $this->resource = [$key => $resource];
 }
Example #3
0
 /**
  * returns model's table name
  * @return string
  */
 protected static function getTableName()
 {
     $class = get_called_class();
     $class = explode('\\', $class);
     $class = array_pop($class);
     $word = new Word();
     return $word->pluralize($word->propertyCase($class));
 }
Example #4
0
 public function initialize()
 {
     $conf =& $this->conf;
     // yaml route shortcut
     // #resource Task (all actions)
     // #resource Task index
     // #resource Task index add edit (etc.)
     $conf->registerMacroPreParser('/#resource (.+)/', function ($matches, $raw) {
         $word = new Word();
         $merger = new Merger();
         $models = (array) array_pop($matches);
         $macros = (array) array_pop($matches);
         foreach ($models as $index => $resource) {
             $resource = preg_replace('/\\s+/', ' ', $resource);
             $parts = explode(' ', $resource);
             $resource = array_shift($parts);
             $actions = count($parts) ? $parts : static::$default_actions;
             $templates = [];
             $model = $word->pluralize($resource);
             $model = strtolower($model);
             $clazz = ucwords($model);
             foreach ($actions as $action) {
                 $templates[] = static::${"action_{$action}_template"};
             }
             $template = $merger->merge(implode(PHP_EOL . PHP_EOL, $templates), ['model' => $model, 'clazz' => $clazz, 'resource' => $resource], false);
             $raw = str_replace($macros[$index], $template, $raw);
         }
         return $raw;
     });
     // yaml import file comments
     $conf->registerMacroPreParser('/#import (.+)/', function ($matches, $raw) {
         $confs = [];
         foreach ($matches[0] as $index => $macro) {
             $import = explode(' ', $matches[1][$index], 2);
             if (count($import) > 1) {
                 $data = explode(',', $import[1]);
                 $import = $import[0];
                 foreach ($data as $index => $row) {
                     list($key, $val) = explode(':', $row);
                     $data[trim($key)] = trim($val);
                     unset($data[$index]);
                 }
             } else {
                 $import = $import[0];
                 $data = [];
             }
             $confs[$import] = $data;
             $raw = str_replace($macro, '', $raw);
         }
         return !count($confs) ? $raw : sprintf('~imports: %s%s%s', json_encode($confs), PHP_EOL, $raw);
     });
     // import extras
     $conf->registerMacroPostParser(function (&$obj) {
         if (isset($obj['~imports'])) {
             $imports = $obj['~imports'];
             unset($obj['~imports']);
             foreach ($imports as $import => $mergedata) {
                 $data = $this->load($import, $mergedata);
                 $obj = array_merge_recursive($obj, $data);
             }
         }
         return $obj;
     });
 }
Example #5
0
 /**
  * @param string $method
  * @param string $type, default = null
  * @return boolean
  */
 public static final function parsePropertyNameFromMethod($method, $type = null)
 {
     $flags = [static::$P_GET, static::$P_SET, static::$P_IS, static::$P_ADD, static::$P_REMOVE, static::$F_FINDBY, static::$F_FINDONEBY];
     $prefix = implode('|', array_map(function ($flag) {
         return sprintf('^%s', $flag);
     }, $flags));
     $prop = strtolower(preg_replace([sprintf('/%s/', $prefix), '/(\\w)([A-Z])/'], ['', '$1_$2'], $method));
     switch ($type) {
         // addRole => $roles[]
         case static::$P_ADD:
         case static::$P_REMOVE:
             $word = new Word();
             $prop = $word->pluralize($prop);
             break;
             // setName = $name
         // setName = $name
         default:
             break;
     }
     return $prop;
 }
Example #6
0
 public function testWordsWithIdenticalSingluarAndPluralForm()
 {
     $this->assertEquals('bison', $this->word->pluralize('bison'));
 }