/** * Get the scaffolded template for a view * * @param string $name * @return string Compiled template */ protected function getScaffoldedTemplate($name) { $model = $this->cache->getModelName(); // post $models = Pluralizer::plural($model); // posts $Models = ucwords($models); // Posts $Model = Pluralizer::singular($Models); // Post // Create and Edit views require form elements if ($name === 'create.blade' or $name === 'edit.blade') { $formElements = $this->makeFormElements(); $this->template = str_replace('{{formElements}}', $formElements, $this->template); } // Replace template vars in view foreach (array('model', 'models', 'Models', 'Model') as $var) { $this->template = str_replace('{{' . $var . '}}', ${$var}, $this->template); } // And finally create the table rows list($headings, $fields, $editAndDeleteLinks) = $this->makeTableRows($model); $this->template = str_replace('{{headings}}', implode(PHP_EOL . "\t\t\t\t", $headings), $this->template); $this->template = str_replace('{{fields}}', implode(PHP_EOL . "\t\t\t\t\t", $fields) . PHP_EOL . $editAndDeleteLinks, $this->template); return $this->template; }
/** * Get the scaffolded template for a view * * @param string $name * @return string Compiled template */ protected function getScaffoldedTemplate($name) { $model = $this->cache->getModelName(); // post $models = Pluralizer::plural($model); // posts $Models = ucwords($models); // Posts $Model = Pluralizer::singular($Models); // Post $layouts_key = 'layouts.' . $this->cache->getValue('layout'); $locales_key = empty($this->cache->getValue('namespace')) ? $models : $this->cache->getValue('namespace') . "/{$models}"; $multi_key = empty($this->cache->getValue('namespace')) ? $models : str_replace('/', '.', $this->cache->getValue('namespace')) . ".{$models}"; // Create and Edit views require form elements if ($name === 'create.blade' or $name === 'edit.blade' or $name === 'fields.blade') { $formElements = $this->makeFormElements($locales_key); $this->template = str_replace('{{formElements}}', $formElements, $this->template); } // Replace template vars in view foreach (array('model', 'models', 'Models', 'Model', 'layouts_key', 'locales_key', 'multi_key') as $var) { $this->template = str_replace('{{' . $var . '}}', ${$var}, $this->template); } // And finally create the table rows if ($name === 'index.blade') { list($headings, $fields, $editAndDeleteLinks) = $this->makeTableRows($model, $locales_key, $multi_key); $this->template = str_replace('{{headings}}', implode(PHP_EOL . "\t\t\t\t", $headings), $this->template); $this->template = str_replace('{{fields}}', implode(PHP_EOL . "\t\t\t\t\t", $fields) . PHP_EOL . $editAndDeleteLinks, $this->template); } if ($name === 'show.blade') { list($content, $editAndDeleteLinks) = $this->makeShowInfo($model, $locales_key, $multi_key); $this->template = str_replace('{{content}}', implode(PHP_EOL . "\t\t\t\t\t", $content) . PHP_EOL . $editAndDeleteLinks, $this->template); } return $this->template; }
/** * Execute the console command. * * @return void */ public function fire() { // Scaffolding should always begin with the singular // form of the now. $this->model = Pluralizer::singular($this->argument('name')); $this->fields = $this->option('fields'); if (is_null($this->fields)) { throw new MissingFieldsException('You must specify the fields option.'); } // We're going to need access to these values // within future commands. I'll save them // to temporary files to allow for that. $this->cache->fields($this->fields); $this->cache->modelName($this->model); $this->generateModel(); $this->generateController(); $this->generateViews(); $this->generateMigration(); $this->generateSeed(); if (get_called_class() === 'Way\\Generators\\Commands\\ScaffoldGeneratorCommand') { $this->generateTest(); } $this->generator->updateRoutesFile($this->model); $this->info('Updated ' . app_path() . '/routes.php'); // We're all finished, so we // can delete the cache. $this->cache->destroyAll(); }
public function generate($path, $name) { $singular = Pluralizer::singular($name); $plural = Pluralizer::plural($name); $segments = explode('/', $path); $module_dir = array_pop($segments); return new Collection(['model' => new BackboneComponent($path . '/' . $plural . '/models/' . $name . '.js', $module_dir . '/' . $plural . '/models/' . $name, ucfirst($singular)), 'view' => new BackboneComponent($path . '/' . $plural . '/views/' . $singular . '_view.js', $module_dir . '/' . $plural . '/views/' . $singular . '_view', ucfirst($singular) . 'View'), 'collection' => new BackboneComponent($path . '/' . $plural . '/collections/' . $plural . '.js', $module_dir . '/' . $plural . '/collections/' . $plural, ucfirst($singular) . 'Collection'), 'index' => new BackboneComponent($path . '/' . $plural . '/index.js', $module_dir . '/' . $plural . '/index', 'Index')]); return new Collection($ret); }
/** * Fetch the compiled template for a seed * * @param string $template Path to template * @param string $className * * @return string Compiled template */ protected function getTemplate($template, $className) { $this->template = $this->file->get($template); $name = Pluralizer::singular(str_replace('TableSeeder', '', $className)); $modelVars = GeneratorsServiceProvider::getModelVars($name); $this->template = str_replace('{{className}}', $className, $this->template); $template = GeneratorsServiceProvider::replaceModelVars($this->template, $modelVars); return $this->replaceStandardParams($template); }
public function setApiName($name) { $this->_api_name = Pluralizer::singular($name); $this->_collection_name = Str::studly(Str::singular(Str::slug($this->_api_name, " "))); if (!class_exists("AndrewLamers\\Chargify\\" . $this->_collection_name)) { eval("namespace Andrewlamers\\Chargify; class " . $this->_collection_name . " extends \\Andrewlamers\\Chargify\\Fluent {}"); } $this->_collection_name = "Andrewlamers\\Chargify\\" . $this->_collection_name; }
private function formatTree(SimpleXMLElement $xml, array $serialized, SimpleXMLElement $rootNode = null, $parentName = null, SimpleXMLElement $parentNode = null) { foreach ($serialized as $key => $value) { if (is_array($value)) { // Branch if (count($value) === 0) { // Empty array $child = $xml->addChild($key); $child->addAttribute("array", null); } else { if (is_numeric($key)) { // Numeric in array, singularize $key = Pluralizer::singular($parentName); if (!isset($parentNode["array"])) { $parentNode->addAttribute("array", $key); } } $child = $xml->addChild($key); $this->formatTree($child, $value, $rootNode, $key, $child); } } else { // Leaf if ($key === "_ref") { // Ref $parentNode->addAttribute("ref", $value); } elseif ($key === "_id") { // Id if (!is_null($parentNode)) { $parentNode->addAttribute("id", $value); } else { $rootNode->addAttribute("id", $value); } } elseif (is_null($value)) { // If value is null, denote it in the attribute $child = $xml->addChild($key); $child->addAttribute("scalar", "null"); } elseif (is_int($value)) { // If value is int, denote it in the attribute $child = $xml->addChild($key, $value); $child->addAttribute("scalar", "integer"); } elseif (is_float($value)) { // If value is float, denote it in the attribute $child = $xml->addChild($key, $value); $child->addAttribute("scalar", "float"); } elseif (is_bool($value)) { // If value is boolean, denote it in the attribute $child = $xml->addChild($key, $value ? 1 : 0); $child->addAttribute("scalar", "boolean"); } else { // Value $xml->addChild($key, $value); } } } }
/** * Get template for a scaffold * * @param string $template Path to template * @param string $name * @return string */ protected function getScaffoldedController($template, $name) { $collection = strtolower(str_replace('Controller', '', $name)); // dogs $modelInstance = Pluralizer::singular($collection); // dog $modelClass = ucwords($modelInstance); // Dog foreach (array('modelInstance', 'modelClass', 'collection') as $var) { $this->template = str_replace('{{' . $var . '}}', ${$var}, $this->template); } return $this->template; }
public function getTemplate($name, $namespace) { $path = __DIR__ . '/templates/model.txt'; $this->template = $this->file->get($path); // Prepare strings to be placed on the template $singular = Pluralizer::singular(ucfirst($name)); $singularLower = strtolower($singular); $plural = Pluralizer::plural(ucfirst($name)); // Replace $this->template = str_replace('{{namespace}}', $namespace, $this->template); $this->template = str_replace('{{singular}}', $singular, $this->template); $this->template = str_replace('{{plural}}', $plural, $this->template); $this->template = str_replace('{{singularLower}}', $singularLower, $this->template); return $this->template; }
/** * Get template for a scaffold * * @param string $template Path to template * @param string $name * @return string */ protected function getScaffoldedController($template, $className) { $model = $this->cache->getModelName(); // post $models = Pluralizer::plural($model); // posts $Models = ucwords($models); // Posts $Model = Pluralizer::singular($Models); // Post foreach (array('model', 'models', 'Models', 'Model', 'className') as $var) { $this->template = str_replace('{{' . $var . '}}', ${$var}, $this->template); } return $this->template; }
/** * Parse the resource name from the called method. * * @param string $method * @return string */ protected function parseResourceName($method) { // Here we check if the method is returned with // a different resource name. if (array_key_exists($method, $this->resourceResponseNames)) { return $this->resourceResponseNames[$method]; } $plural = strtolower(str_replace($this->trimable, null, $method)); // Here we check if the method is returned with // a plural resource name. if (in_array($plural, $this->pluralResponseNames)) { return $plural; } // Lastly we create a singular resource name. return Pluralizer::singular($plural); }
/** * For a given table, generate the migration object with the appropriate * parameters * @param string $tableName The name of the table * @param array $columns An array of SchemaExtractor Column objects */ private function createTableMigration($tableName, $columns) { // Get the parsed columns $parsedColumns = $this->schemaExtractor->extract($columns, $this->dbType); // Get the model name form the table name $modelName = ucwords(Pluralizer::singular($tableName)); // Create a new migration $this->migrationList->create($modelName, $tableName); // Now, proceed towards adding columns foreach ($parsedColumns as $column) { $type = $this->getLaravelColumnType($column); // For primary keys, we simply set pK if ($type == 'increments') { $this->migrationList->setPrimaryKey($modelName, $column->name); continue; } $c = array('name' => $column->name, 'type' => $type, 'parameters' => $this->getLaravelColumnParameters($column->parameters, $type), 'default' => is_null($column->defaultValue) ? '' : $column->defaultValue, 'unsigned' => $column->unsigned, 'nullable' => $column->null, 'primary' => $column->index == 'primary', 'unique' => $column->index == 'unique', 'index' => $column->index == 'multicolumn'); $this->migrationList->addColumn($modelName, $c); } }
/** * Get template for a scaffold * * @param string $template Path to template * @param string $name * @return string */ protected function getScaffoldedController($template, $className) { $model = $this->cache->getModelName(); // post $models = Pluralizer::plural($model); // posts $Models = ucwords($models); // Posts $Model = Pluralizer::singular($Models); // Post $namespace = $this->cache->getValue('namespace'); $classNamespace = empty($namespace) ? '' : "namespace " . ucwords(str_replace('/', '\\', $namespace)) . ";"; $multi_key = empty($namespace) ? $models : str_replace('/', '.', $namespace) . ".{$models}"; $fields = join(",", array_map(function ($f) { return "'{$f}'"; }, array_keys($this->cache->getFields()))); foreach (array('model', 'models', 'Models', 'Model', 'className', 'multi_key', 'classNamespace', 'fields') as $var) { $this->template = str_replace('{{' . $var . '}}', ${$var}, $this->template); } return $this->template; }
public function fire() { if (!class_exists('Way\\Generators\\GeneratorsServiceProvider')) { $this->error('Way Generators are not installed! Lapigen needs this package to run!'); } $lowerCase = strtolower($this->argument('name')); $capitalFirst = ucfirst($lowerCase); $this->line(''); $this->line('|***************************************************|'); $this->line('|'); $this->line('| I choose you LAPIGEN!'); $this->line('|'); $this->line('| Generating full API resource!'); $this->line('|'); $this->line('|***************************************************|'); $this->line(''); $this->line('*** Controller ***'); $controllerName = Pluralizer::singular($capitalFirst) . 'Controller'; $this->call('generate:controller', array('name' => $controllerName, '--path' => $this->option('controllerpath'), '--template' => __DIR__ . '/stubs/controller.txt')); $this->line(''); $this->line('*** Route ***'); $routesFilePath = $routesFilePath = app_path() . '/routes.php'; $this->setLapiRoute($routesFilePath, $lowerCase, $controllerName); $this->line("Updated {$routesFilePath}"); $this->line(''); $this->line('*** Model ***'); $this->call('generate:model', array('name' => Pluralizer::singular($capitalFirst), '--path' => $this->option('modelpath'), '--template' => __DIR__ . '/stubs/model.txt')); $this->line(''); $this->line('*** Seeder ***'); $this->call('generate:seed', array('name' => Pluralizer::plural($lowerCase), '--path' => $this->option('seedpath'))); $this->line(''); $this->line('*** Migration and "artisan optimize" ***'); $this->call('generate:migration', array('name' => 'create_' . Pluralizer::plural($lowerCase) . '_table', '--path' => $this->option('migrationspath'), '--fields' => $this->option('fields'))); $this->line(''); $this->line('|***************************************************|'); $this->line('|'); $this->line('| Ready to rumble!'); $this->line('|'); $this->line('|***************************************************|'); }
public function singular($word) { return Pluralizer::singular($word); }
/** * Retrieves the singular name of the table * * @param boolean $format * @return string */ public function getSingular($format = false) { if (!isset($this->singular)) { $this->singular = Pluralizer::singular($this->table); } return $format ? $this->formatTableName($this->singular) : $this->singular; }
/** * Generate a model file from a database table. * * @param $table * @return void */ protected function generateTable($table) { //prefix is the sub-directory within app $prefix = $this->option('dir'); $ignoreTable = $this->option("ignore"); if ($this->option("ignoresystem")) { $ignoreSystem = "users,permissions,permission_role,roles,role_user,migrations,password_resets"; if (is_string($ignoreTable)) { $ignoreTable .= "," . $ignoreSystem; } else { $ignoreTable = $ignoreSystem; } } // if we have ignore tables, we need to find all the posibilites if (is_string($ignoreTable) && preg_match("/^" . $table . "|^" . $table . ",|," . $table . ",|," . $table . "\$/", $ignoreTable)) { $this->info($table . " is ignored"); return; } $class = VariableConversion::convertTableNameToClassName($table); $name = Pluralizer::singular($this->parseName($prefix . $class)); if ($this->files->exists($path = $this->getPath($name)) && !$this->option('force')) { return $this->error($this->extends . ' for ' . $table . ' already exists!'); } $this->makeDirectory($path); $this->files->put($path, $this->replaceTokens($name, $table)); $this->info($this->extends . ' for ' . $table . ' created successfully.'); }
/** * Convert a word to it's singular * @param $word * @return string */ public function singular($word) { return $this->pluralizer->singular($word); }
/** * call has many relationship * **/ public function TransactionExtensions() { return $this->hasMany('App\\Models\\TransactionExtension', Pluralizer::singular($this->getTable()) . '_id'); }
/** * @param $table_name * @return mixed */ private static function getClassByTable($table_name) { $class = Config::get('refinement.table_map.' . $table_name); return $class ?: ucfirst(Pluralizer::singular($table_name)); }
/** * call has many relationship * **/ public function PointLogs() { return $this->hasMany('App\\Models\\PointLog', Pluralizer::singular($this->getTable()) . '_id'); }
protected function className($resourceName) { return $this->camelCase(Pluralizer::singular($resourceName)); }
/** * Execute the console command. * * @throws MissingFieldsException * @throws TemplateNameDoesNotExist */ public function fire() { $this->generator->setOptions($this->option()); // Scaffolding should always begin with the singular // form of the now. $this->model = Pluralizer::singular($this->argument('name')); // common error for field types $fields = trim($this->option('fields')); $fields = GeneratorsServiceProvider::mapFieldTypes($fields); $this->fields = $fields; $templateDir = str_finish($this->option('template-dir'), "/"); $defaultDirs = $this->getDefaultTemplateSubDirs(); if ($this->fields === null) { throw new MissingFieldsException('You must specify the fields option.'); } if (!is_dir(GeneratorsServiceProvider::getTemplatePath($this->templateDirs, '/'))) { throw new TemplateNameDoesNotExist('template-name ' . $this->templateDirs . ' is not a sub-directory or templates/.'); } $templateDir = str_finish($templateDir, "/"); $isDefault = false; foreach ($defaultDirs as &$defaultDir) { $defaultDir = str_finish($defaultDir, "/"); if ($templateDir === $defaultDir) { $isDefault = true; } } $this->templateDirs = $isDefault ? [] : [$templateDir]; $this->templateDirs = array_merge($this->templateDirs, $defaultDirs); // We're going to need access to these values // within future commands. I'll save them // to temporary files to allow for that. $this->cache->fields($this->fields); $this->cache->modelName($this->model); $this->generateModel(); $this->generateController(); $this->generateViews(); $this->generateMigration(); if (!$this->option('bench')) { $this->generateSeed(); } $this->generateTranslations(); if (get_called_class() === 'Vsch\\Generators\\Commands\\ScaffoldGeneratorCommand') { $this->generateLangScaffold(); } if (get_called_class() === 'Vsch\\Generators\\Commands\\ScaffoldGeneratorCommand') { $this->generateTest(); } $routesFile = parent::getSrcPath(self::PATH_ROUTES); if ($routesFile) { if ($this->generator->updateRoutesFile($routesFile, $this->model, $this->getRouteTemplatePath())) { $this->info('Updated ' . $routesFile); } else { $this->warn('Did not need to update ' . $routesFile); } } else { $this->info(self::PATH_ROUTES . ' dir_map not set to a value in config, routes need to be manually updated'); } // We're all finished, so we // can delete the cache. $this->cache->destroyAll(); }
/** * Call generate:views * * @return void */ protected function generateLocales() { $localesDir = app_path() . '/lang'; $fields = $this->cache->getFields(); $model = $this->cache->getModelName(); $models = Pluralizer::plural($model); $Models = ucwords($models); $Model = Pluralizer::singular($Models); foreach ($this->locales as $locale) { $localeDir = empty($this->namespace) ? $localesDir . '/' . $locale : $localesDir . '/' . $locale . '/' . $this->namespace; $this->generator->folders(array($localeDir)); $utils = ''; $source = ''; if ($this->generator->file->exists(__DIR__ . "/../Generators/templates/lang/{$locale}/model.txt")) { $utils = $this->generator->file->get(__DIR__ . "/../Generators/templates/lang/{$locale}/utils.txt"); $source = $this->generator->file->get(__DIR__ . "/../Generators/templates/lang/{$locale}/model.txt"); } else { $utils = $this->generator->file->get(__DIR__ . "/../Generators/templates/lang/en/utils.txt"); $source = $this->generator->file->get(__DIR__ . "/../Generators/templates/lang/en/model.txt"); } $modelFields = ''; foreach (array_keys($fields) as $field) { $modelFields .= " '{$field}' => '" . ucwords($field) . "',\n"; } foreach (array('model', 'models', 'Models', 'Model', 'modelFields') as $var) { $source = str_replace('{{' . $var . '}}', ${$var}, $source); } if (!$this->generator->file->exists($localesDir . '/' . $locale . "/utils.php")) { $this->generator->file->put($localesDir . '/' . $locale . "/utils.php", $utils); } $this->generator->file->put($localeDir . "/{$models}.php", $source); } }
/** * Get the singular form of an English word. * * @param string $value * @return string */ public static function singular($value) { return Pluralizer::singular($value); }
/** * Returns the fully qualified class * of the model associated with this * controller. * * This method assumes that the model * class is the singularized form of * the controller's name minus the * 'Controller' suffix. * * It will also assume that the model class * is namespaced under 'App', unless specified * otherwise. A different namespace can * be set by overriding getModelNamespace(). * * @return string */ public function getRelatedModelClass() { $fqController = explode('\\', static::class); $model = Pluralizer::singular(str_replace('Controller', '', end($fqController))); $ns = $this->getModelNamespace() ?: 'App'; return $ns . '\\' . $model; }
public static function getModelVars($originalModelName) { // figure out the format of the modelName: has _ then snake, has - then dash, has space then space, else assume CamelCase $modelName = $originalModelName; if (str_contains($modelName, '_')) { $modelName = Pluralizer::singular(str_replace(' ', '', ucwords(str_replace('_', ' ', $modelName)))); } else { if (str_contains($modelName, '-')) { $modelName = Pluralizer::singular(str_replace(' ', '', ucwords(str_replace('-', ' ', $modelName)))); } else { if (str_contains($modelName, ' ')) { $modelName = Pluralizer::singular(str_replace(' ', '', ucwords($modelName))); } else { $modelName = Pluralizer::singular($modelName); } } } $camelModel = strtolower(substr($modelName, 0, 1)) . substr($modelName, 1); // blockedEmail $camelModels = Pluralizer::plural($camelModel); // blockedEmails $CamelModel = strtoupper(substr($camelModel, 0, 1)) . substr($camelModel, 1); // BlockedEmail $CamelModels = strtoupper(substr($camelModels, 0, 1)) . substr($camelModels, 1); // BlockedEmails $model = strtolower($camelModel); // blockedemail $models = strtolower($camelModels); // blockedemails $Model = $CamelModel; // BlockedEmail $Models = $CamelModels; // BlockedEmails $MODEL = strtoupper($camelModel); // BLOCKEDEMAIL $MODELS = strtoupper($camelModels); // BLOCKEDEMAILS $snake_model = snake_case($camelModel); $snake_models = snake_case($camelModels); $Snake_Model = str_replace(' ', '_', ucwords(snake_case($camelModel, ' '))); $Snake_Models = str_replace(' ', '_', ucwords(snake_case($camelModels, ' '))); $SNAKE_MODEL = strtoupper($snake_model); $SNAKE_MODELS = strtoupper($snake_models); $dash_model = str_replace('_', '-', $snake_model); $dash_models = str_replace('_', '-', $snake_models); $Dash_Model = str_replace('_', '-', $Snake_Model); $Dash_Models = str_replace('_', '-', $Snake_Models); $DASH_MODEL = str_replace('_', '-', $SNAKE_MODEL); $DASH_MODELS = str_replace('_', '-', $SNAKE_MODELS); $space_model = str_replace('-', ' ', $dash_model); $space_models = str_replace('-', ' ', $dash_models); $Space_Model = str_replace('-', ' ', $Dash_Model); $Space_Models = str_replace('-', ' ', $Dash_Models); $SPACE_MODEL = str_replace('-', ' ', $DASH_MODEL); $SPACE_MODELS = str_replace('-', ' ', $DASH_MODELS); $modelVars = ['camelModel' => $camelModel, 'camelModels' => $camelModels, 'CamelModel' => $CamelModel, 'CamelModels' => $CamelModels, 'model' => $model, 'models' => $models, 'MODEL' => $MODEL, 'MODELS' => $MODELS, 'Model' => $Model, 'Models' => $Models, 'snake_model' => $snake_model, 'snake_models' => $snake_models, 'Snake_Model' => $Snake_Model, 'Snake_Models' => $Snake_Models, 'SNAKE_MODEL' => $SNAKE_MODEL, 'SNAKE_MODELS' => $SNAKE_MODELS, 'dash-model' => $dash_model, 'dash-models' => $dash_models, 'Dash-Model' => $Dash_Model, 'Dash-Models' => $Dash_Models, 'DASH-MODEL' => $DASH_MODEL, 'DASH-MODELS' => $DASH_MODELS, 'space model' => $space_model, 'space models' => $space_models, 'Space Model' => $Space_Model, 'Space Models' => $Space_Models, 'SPACE MODEL' => $SPACE_MODEL, 'SPACE MODELS' => $SPACE_MODELS]; return $modelVars; }