public function initVariables() { $this->modelNamePlural = Str::plural($this->modelName); $this->tableName = strtolower(Str::snake($this->modelNamePlural)); $this->modelNameCamel = Str::camel($this->modelName); $this->modelNamePluralCamel = Str::camel($this->modelNamePlural); $this->modelNamespace = Config::get('generator.namespace_model', 'App') . "\\" . $this->modelName; }
public function generate() { $templateData = $this->commandData->templatesHelper->getTemplate('Controller', 'api'); if ($this->commandData->pointerModel) { $pointerRelationship = []; foreach ($this->commandData->inputFields as $field) { if ($field['type'] == 'pointer') { $arr = explode(',', $field['typeOptions']); if (count($arr) > 0) { $modelName = $arr[0]; $pointerRelationship[] = "'" . Str::camel($modelName) . "'"; } } } $templateData = str_replace('$POINTER_MODELS_RELATIONSHIP$', "with([" . implode(", ", $pointerRelationship) . "])->", $templateData); } else { $templateData = str_replace('$POINTER_MODELS_RELATIONSHIP$', '', $templateData); } $templateData = GeneratorUtils::fillTemplate($this->commandData->dynamicVars, $templateData); $fileName = $this->commandData->modelName . 'APIController.php'; if (!file_exists($this->path)) { mkdir($this->path, 0755, true); } $path = $this->path . $fileName; $this->commandData->fileHelper->writeFile($path, $templateData); $this->commandData->commandObj->comment("\nAPI Controller created: "); $this->commandData->commandObj->info($fileName); }
/** * Resolve the callback for a policy check. * * @param \Illuminate\Contracts\Auth\Authenticatable $user * @param string $ability * @param array $arguments * * @return callable */ protected function resolvePolicyCallback($user, $ability, array $arguments) { return function () use($user, $ability, $arguments) { $instance = $this->getPolicyFor($arguments[0]); if (method_exists($instance, 'before')) { // We will prepend the user and ability onto the arguments so that the before // callback can determine which ability is being called. Then we will call // into the policy before methods with the arguments and get the result. $beforeArguments = array_merge([$user, $ability], $this->getPolicyArguments($arguments)); $result = call_user_func_array([$instance, 'before'], $beforeArguments); // If we received a non-null result from the before method, we will return it // as the result of a check. This allows developers to override the checks // in the policy and return a result for all rules defined in the class. if (!is_null($result)) { return $result; } } if (strpos($ability, '-') !== false) { $ability = Str::camel($ability); } if (!is_callable([$instance, $ability])) { return false; } return call_user_func_array([$instance, $ability], array_merge([$user], $this->getPolicyArguments($arguments))); }; }
/** * Creates the new policies for the rest config. * @param array $mappedPolicies * @return array The first element are the new class usages and the second element is the policy mapping. * @throws Exception */ public function createNewPolicies(array $mappedPolicies) { $appNamespace = $this->getAppNamespace(); $config = $this->getConfig(); $newUsages = []; foreach (@$config['tables'] ?: [] as $table => $tableConfig) { $policyName = Str::ucfirst(Str::camel($table)) . 'Policy'; $className = preg_replace('/(\\w+\\\\)+/', '', $tableConfig['model']); $classCall = $className . '::class'; if (!class_exists($fullPolicyName = $appNamespace . '\\Policies\\' . $policyName)) { if (Artisan::call('make:policy', ['name' => $policyName])) { throw new Exception('Could not write ' . $policyName); } // if app(File::class, [$policyFile = app_path("Policies/{$policyName}.php")])->setContent(str_replace([' }', "\nclass "], [" }\n{$this->getPolicyMethods($table)}", "use {$tableConfig['model']};\n\nclass "], file_get_contents($policyFile)))->save(); } // if if (!array_key_exists($classCall, $mappedPolicies)) { $newUsages = array_merge($newUsages, [$tableConfig['model'], str_replace('\\\\', '\\', $fullPolicyName)]); $mappedPolicies[$className . '::class'] = $policyName . '::class'; } // if } return array($newUsages, $mappedPolicies); }
private function generateRelations() { if ($this->commandData->tableName == '') { return ''; } $code = ''; //Get what tables it belongs to $relations = DataBaseHelper::getForeignKeysFromTable($this->commandData->tableName); foreach ($relations as $r) { $referencedTableName = preg_replace("/{$this->prefix}/uis", '', $r->REFERENCED_TABLE_NAME); $referencedTableName = ucfirst(Str::camel(StringUtils::singularize($referencedTableName))); $code .= " public function " . $referencedTableName . "() {\n"; $code .= " " . 'return $this->belongsTo(' . "'\$NAMESPACE_MODEL\$\\" . $referencedTableName . "', '" . $r->COLUMN_NAME . "'); \n"; $code .= " }\n\n"; } //Get what tables it is referenced $relations = DataBaseHelper::getReferencesFromTable($this->commandData->tableName); foreach ($relations as $r) { $tableName = preg_replace("/{$this->prefix}/uis", '', $r->TABLE_NAME); $tableName = ucfirst(Str::camel(StringUtils::singularize($tableName))); $code .= " public function " . Str::plural($tableName) . "() {\n"; $code .= " " . 'return $this->hasMany(' . "'\$NAMESPACE_MODEL\$\\" . $tableName . "', '" . $r->COLUMN_NAME . "'); \n"; $code .= " }\n\n"; } return $code; }
/** * Construct with attributes * * @param array $attributes */ public function __construct(array $attributes = []) { if (isset($attributes['attributes'])) { $attributes['attributes'] = new Attributes($attributes['attributes']); } // normalize relationship names to camel case if (isset($attributes['relationships'])) { foreach ($attributes['relationships'] as $name => $relationship) { $normalizedName = Str::camel($name); $attributes['relationships'][$normalizedName] = new Relationship($relationship); if ($normalizedName === $name) { continue; } unset($attributes['relationships'][$name]); } } if (isset($attributes['links'])) { foreach ($attributes['links'] as $key => $link) { $attributes['links'][$key] = new Link($link); } } if (isset($attributes['meta'])) { $attributes['meta'] = new Meta($attributes['meta']); } parent::__construct($attributes); }
public function initVariables() { $this->modelNamePlural = Str::plural($this->modelName); $this->modelNameCamel = Str::camel($this->modelName); $this->modelNamePluralCamel = Str::camel($this->modelNamePlural); $this->initDynamicVariables(); }
protected function initValidator() { $class = $this->getValidatorClass(); if ($class) { $validator = new $class(); $this->validator_messages = $validator->messages(); $this->validator_rules = $validator->rules(); foreach ($this->validator_rules as $element_name => $element_rules) { if (false !== mb_strpos($element_name, '.')) { $element_name = str_replace('.', '][', $element_name) . ']'; $e = explode(']', $element_name, 2); $element_name = implode('', $e); } foreach ($this->getElementsByName($element_name) as $el) { $element_rules = explode('|', $element_rules); foreach ($element_rules as $rule) { $_rule = explode(':', $rule); $rule_name = Arr::get($_rule, 0); $rule_params = Arr::get($_rule, 1); $rule_params = explode(',', $rule_params); $method = Str::camel('rule_' . $rule_name); if (method_exists($el, $method)) { call_user_func_array([$el, $method], $rule_params); } } } } } return $this; }
/** * @param $cars * @depends testArrayApply */ public function testDtoApply($cars) { $model = Cars::make($cars); $this->assertEquals(count($this->data), count($model->toArray())); foreach ($this->data as $key => $value) { $this->assertEquals($value, $model->toArray()[Str::camel($key)]); } }
/** * {@inheritDoc} */ public function getFilters() { return array(new \Twig_SimpleFilter('camel_case', array('Illuminate\\Support\\Str', 'camel'), array('is_safe' => array('html'))), new \Twig_SimpleFilter('snake_case', array('Illuminate\\Support\\Str', 'snake'), array('is_safe' => array('html'))), new \Twig_SimpleFilter('studly_case', array('Illuminate\\Support\\Str', 'studly'), array('is_safe' => array('html'))), new \Twig_SimpleFilter('str_*', function ($name) { $arguments = array_slice(func_get_args(), 1); $name = Str::camel($name); return call_user_func_array(array('Illuminate\\Support\\Str', $name), $arguments); }, array('is_safe' => array('html')))); }
/** * {@inheritDoc} */ public function getFunctions() { return [new Twig_SimpleFunction('link_to', [$this->html, 'link'], ['is_safe' => ['html']]), new Twig_SimpleFunction('link_to_asset', [$this->html, 'linkAsset'], ['is_safe' => ['html']]), new Twig_SimpleFunction('link_to_route', [$this->html, 'linkRoute'], ['is_safe' => ['html']]), new Twig_SimpleFunction('link_to_action', [$this->html, 'linkAction'], ['is_safe' => ['html']]), new Twig_SimpleFunction('html_*', function ($name) { $arguments = array_slice(func_get_args(), 1); $name = Str::camel($name); return call_user_func_array([$this->html, $name], $arguments); }, ['is_safe' => ['html']])]; }
/** * {@inheritDoc} */ public function getFunctions() { return [new Twig_SimpleFunction('asset', [$this->url, 'asset'], ['is_safe' => ['html']]), new Twig_SimpleFunction('action', [$this->url, 'action'], ['is_safe' => ['html']]), new Twig_SimpleFunction('url', [$this, 'url'], ['is_safe' => ['html']]), new Twig_SimpleFunction('route', [$this->url, 'route'], ['is_safe' => ['html']]), new Twig_SimpleFunction('route_has', [$this->router, 'has'], ['is_safe' => ['html']]), new Twig_SimpleFunction('secure_url', [$this->url, 'secure'], ['is_safe' => ['html']]), new Twig_SimpleFunction('secure_asset', [$this->url, 'secureAsset'], ['is_safe' => ['html']]), new Twig_SimpleFunction('url_*', function ($name) { $arguments = array_slice(func_get_args(), 1); $name = IlluminateStr::camel($name); return call_user_func_array([$this->url, $name], $arguments); })]; }
/** * @param Event $event */ public function handle(Event $event) { $cloneModel = $this->clone->find($event->request['clone_id']); $group = $this->group->find($event->request['group_id']); $sitePath = Str::camel($event->request['name']); $event->site->updateStatus('Cloning the repo'); $this->envoy->run('clone --path="' . $group->starting_path . '" --name="' . $sitePath . '" --url=' . $cloneModel->url, true); }
/** * {@inheritDoc} */ public function getFilters() { return [new Twig_SimpleFilter('camel_case', [$this->callback, 'camel']), new Twig_SimpleFilter('snake_case', [$this->callback, 'snake']), new Twig_SimpleFilter('studly_case', [$this->callback, 'studly']), new Twig_SimpleFilter('str_*', function ($name) { $arguments = array_slice(func_get_args(), 1); $name = IlluminateStr::camel($name); return call_user_func_array([$this->callback, $name], $arguments); })]; }
/** * @param Event $event */ public function handle(Event $event) { $event->site->updateStatus('Running the installer'); $installerType = $event->request['installType'] == 'base' ? null : '"--' . $event->request['installType'] . '"'; $group = $this->group->find($event->request['group_id']); $sitePath = Str::camel($event->request['name']); $this->envoy->run('make-site --path="' . $group->starting_path . '" --name="' . $sitePath . '" --type=' . $installerType, true); }
/** * {@inheritDoc} */ public function getFunctions() { return [new Twig_SimpleFunction('form_*', function ($name) { $arguments = array_slice(func_get_args(), 1); $name = Str::camel($name); return call_user_func_array([$this->form, $name], $arguments); }, ['is_safe' => ['html']])]; }
private function prepareModelNames() { $this->modelNames['plural'] = Str::plural($this->modelName); $this->modelNames['camel'] = Str::camel($this->modelName); $this->modelNames['camelPlural'] = Str::camel($this->modelNames['plural']); $this->modelNames['snake'] = Str::snake($this->modelName); $this->modelNames['snakePlural'] = Str::snake($this->modelNames['plural']); }
/** * {@inheritDoc} */ public function getFunctions() { $html = $this->html; return array(new \Twig_SimpleFunction('link_to', array($html, 'link'), array('is_safe' => array('html'))), new \Twig_SimpleFunction('link_to_asset', array($html, 'linkAsset'), array('is_safe' => array('html'))), new \Twig_SimpleFunction('link_to_route', array($html, 'linkRoute'), array('is_safe' => array('html'))), new \Twig_SimpleFunction('link_to_action', array($html, 'linkAction'), array('is_safe' => array('html'))), new \Twig_SimpleFunction('html_*', function ($name) use($html) { $arguments = array_slice(func_get_args(), 1); $name = Str::camel($name); return call_user_func_array(array($html, $name), $arguments); }, array('is_safe' => array('html')))); }
/** * {@inheritDoc} */ public function getFunctions() { $url = $this->url; return array(new \Twig_SimpleFunction('asset', array($url, 'asset'), array('is_safe' => array('html'))), new \Twig_SimpleFunction('action', array($url, 'action'), array('is_safe' => array('html'))), new \Twig_SimpleFunction('url', array($this, 'url'), array('is_safe' => array('html'))), new \Twig_SimpleFunction('route', array($url, 'route'), array('is_safe' => array('html'))), new \Twig_SimpleFunction('secure_url', array($url, 'secure'), array('is_safe' => array('html'))), new \Twig_SimpleFunction('secure_asset', array($url, 'secureAsset'), array('is_safe' => array('html'))), new \Twig_SimpleFunction('url_*', function ($name) use($url) { $arguments = array_slice(func_get_args(), 1); $name = Str::camel($name); return call_user_func_array(array($url, $name), $arguments); })); }
/** * 得到已上传文件的URL * @param $disk * @param $path * @return string */ public function getUploadUrl($disk, $path) { $url = ''; $methodName = 'get' . Str::camel($disk) . 'Url'; if (method_exists($this->strategy, $methodName)) { $url = $this->strategy->{$methodName}($path); } return $url; }
/** * {@inheritDoc} */ public function getFunctions() { $form = $this->form; return array(new \Twig_SimpleFunction('form_*', function ($name) use($form) { $arguments = array_slice(func_get_args(), 1); $name = Str::camel($name); return call_user_func_array(array($form, $name), $arguments); }, array('is_safe' => array('html')))); }
/** * @param $name * @param bool $usePrefix * * @return bool|ModelEx */ public function getModel($name, bool $usePrefix = false) { try { $prefix = $usePrefix ? defined('MODEL_PREFIX') ? MODEL_PREFIX : 'M' : ''; $class = $this->normalize($prefix . ucfirst(Str::camel(Str::singular("{$name}"))), defined('MODEL_DIR') ? MODEL_DIR : 'App\\Model'); return class_exists($class) ? $class : false; } catch (\Throwable $e) { } return false; }
/** * Create out-going messages. We keep track of sent messages for further usage * * @param array $fields * @return Message|null */ protected function createOutbound(array $fields) { $basic = $this->createBasic($fields); $outbound = new Outbound(); $outbound->content = collect($fields)->filter(function ($val, $name) { return !in_array(Str::camel($name), $this->common); })->toJson(); $outbound->save(); $outbound->message()->save($basic); return $basic; }
protected function seedTables() { $this->setForeignKeyChecks(false); $this->setSQLMode(); foreach ($this->tables as $tableName) { $tableNameFormatted = strpos($tableName, '_') !== false ? Str::title($tableName) : ucfirst(Str::camel($tableName)); $tableSeeder = $tableNameFormatted . 'TableSeeder'; $this->call($tableSeeder); } $this->setForeignKeyChecks(true); }
public function handle(array $request) { $group = $this->group->find($request['group_id']); $sitePath = Str::camel($request['name']); if (settingEnabled('nginx') == 1) { $site = $this->createNginxSite($request, $group, $sitePath); } elseif (settingEnabled('homestead') == 1) { $site = $this->createHomesteadSite($request, $group, $sitePath); } event(new SiteWasInstalled($site, $request)); }
/** * Get API Method by class name. * * @return string */ protected function getApiMethod() { if (!isset($this->apiMethod)) { $words = explode('_', Str::snake(class_basename($this)), -1); $endpoint = strtolower(array_shift($words)); $action = Str::camel(implode('_', $words)) ?: '*'; $method = sprintf('%s.%s', $endpoint, $action); $this->apiMethod = $endpoint ? $method : 'undefined'; } return $this->apiMethod; }
/** * Send the method to the needed class * * @param $method * @param $arguments * * @return mixed */ public function __call($method, $arguments) { if (!empty($arguments)) { $probClass = "\\SaleBoss\\Services\\Menu\\CustomBuilders\\" . ucfirst(Str::camel($arguments[0])); if (class_exists($probClass)) { $custom = App::make($probClass); return call_user_func_array([$custom, $method], $arguments); } } return call_user_func_array(array($this->builder, $method), $arguments); }
/** * Format an Eloquent collection. * * @param \Illuminate\Database\Eloquent\Collection $collection * * @return string */ public function formatEloquentCollection($collection) { if ($collection->isEmpty()) { return $this->encode([]); } $model = $collection->first(); $key = Str::plural($model->getTable()); if (!$model::$snakeAttributes) { $key = Str::camel($key); } return $this->encode([$key => $collection->toArray()]); }
/** * Handle calls to undefined variables. * * @param string $name * @return mixed */ public function __get($name) { $method = Str::camel($name); // Give priority to snake-cased versions of attribute names defined // as methods. For example, accessing $view->first_name will run // $view->firstName() if it's available. if (method_exists($this, $method)) { return $this->{$method}(); } $attribute = Str::snake($name); return $this->getModel()->{$attribute}; }
/** * Set properties on object using array of properties. * * It checks whether an object has a property setter and uses it first. * * @param object $object * @param array $properties */ public static function configure($object, array $properties) { foreach ($properties as $property => $value) { $property = Str::camel($property); $method = 'set' . ucfirst($property); if (method_exists($object, $method)) { $object->{$method}($value); } else { $object->{$property} = $value; } } }