Example #1
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 #2
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 #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
 /**
  * I hate php. need non-static for closure binding
  * load an initializer file
  * @param string $name
  * @param array $args, default: array
  */
 public function initialize($name, array $args = [])
 {
     $word = new Word();
     $init = false;
     $project_init = sprintf('%s\\Initializer\\%s', $this->conf->get('app:namespace'), $word->classicalCase($name));
     $standard_init = sprintf('Fabrico\\Initializer\\StdInitializers\\%s', $word->classicalCase($name));
     if (!in_array($name, $this->initialized)) {
         $this->initialized[] = $name;
         if (class_exists($project_init)) {
             $init = $project_init;
         } elseif (class_exists($standard_init)) {
             $init = $standard_init;
         }
         if ($init) {
             $initializer = new $init();
             if ($initializer instanceof JitInitializer) {
                 $initializer->setConfiguration($this->conf);
                 $initializer->setProperties($args);
                 return $initializer->initialize();
             } else {
                 throw new RuntimeException(sprintf(''));
             }
         }
     }
 }
Example #6
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 #7
0
 /**
  * reads super-globals to create itself
  * @return Request
  */
 public function importFromGlobals()
 {
     $word = new Word();
     $this->setUri(explode('?', $_SERVER['REQUEST_URI'], 2)[0]);
     $this->setPort($_SERVER['SERVER_PORT']);
     $this->setMethod($_SERVER['REQUEST_METHOD']);
     $this->setParameters(new PublicObject($_REQUEST));
     foreach ($_SERVER as $key => $val) {
         if (strpos($key, self::HEADER_SERVER_PREFIX) === 0) {
             $key = str_replace(self::HEADER_SERVER_PREFIX, '', $key);
             $key = strtolower($key);
             $key = $word->humanCase($key);
             $key = ucwords($key);
             $key = str_replace(' ', '-', $key);
             $this->header->{$key} = $val;
         }
     }
     return $this;
 }
Example #8
0
 /**
  * @dataProvider dataProviderStrings
  */
 public function testConvertingToPropertyCase($str)
 {
     $this->assertEquals('property_name', $this->word->propertyCase($str));
 }