/** * Gets the value of an environment variable. Supports boolean, empty and null. * * @param string $key * @param mixed $default * @return mixed */ function env($key, $default = null) { $value = getenv($key); if ($value === false) { return value($default); } switch (strtolower($value)) { case 'true': case '(true)': return true; case 'false': case '(false)': return false; case 'empty': case '(empty)': return ''; case 'null': case '(null)': return; } if (Text::startsWith($value, '"') && Text::endsWith($value, '"')) { return substr($value, 1, -1); } return $value; }
public function eager($model, $field = null, $localKey = null, $foreignKey = '_id') { if ($field == null || $localKey == null) { $className = strtolower((new \ReflectionClass($model))->getShortName()); if (Text::endsWith($className, 's')) { $className = substr($className, 0, -1); } } if ($field == null) { $field = $className; } if ($localKey == null) { $localKey = $className . '_id'; } $keys = []; foreach ($this->array as $item) { if (!in_array($item->{$localKey}, $keys)) { $keys[] = $item->{$localKey}; } } $result = $model::init()->find([$foreignKey => ['$in' => $keys]])->keyBy('_id'); foreach ($this->array as $item) { $item->setRelation($field, $result[(string) $item->{$localKey}]); } return $this; }
protected function getIdFieldName($model) { $className = strtolower((new \ReflectionClass($model))->getShortName()); if (Text::endsWith($className, 's')) { $className = substr($className, 0, -1); } return $className . '_id'; }
public static function endsWith($str, $end, $ignoreCase = true) { return parent::endsWith($str, $end, $ignoreCase); }
public function testEndsWith() { $this->assertFalse(PhText::endsWith("", "")); $this->assertFalse(PhText::endsWith("", "hello")); $this->assertTrue(PhText::endsWith("Hello", "o")); $this->assertTrue(PhText::endsWith("Hello", "lo")); $this->assertTrue(PhText::endsWith("Hello", "Hello")); $this->assertFalse(PhText::endsWith("Hello", "LLO")); $this->assertFalse(PhText::endsWith("Hello", "hello")); $this->assertFalse(PhText::endsWith("Hello", "hello", true)); $this->assertTrue(PhText::endsWith("Hello", "hello", false)); $this->assertTrue(PhText::endsWith("Hello", "o", false)); }
public function getOperations($controllerClass) { $operations = array(); $ref = new \ReflectionClass($controllerClass); $methods = $ref->getMethods(); $reader = new \Phalcon\Annotations\Adapter\Memory(); $reflector = $reader->get($controllerClass); $operationAnnotations = $reflector->getMethodsAnnotations(); foreach ($methods as $method) { if (!\Phalcon\Text::endsWith($method->name, 'Action')) { continue; } $operationKey = substr($method->name, 0, -6); $operationName = $operationKey; $operationDes = ''; if (isset($operationAnnotations[$method->name]) && ($annotations = $operationAnnotations[$method->name])) { if (!$annotations->has('operationName') || !$annotations->has('operationDescription')) { continue; } $annotation = $annotations->get('operationName'); $operationName = implode('', $annotation->getArguments()); $annotation = $annotations->get('operationDescription'); $operationDes = implode('', $annotation->getArguments()); } $operation = array('name' => $operationName, 'resourceKey' => $controllerClass, 'operationKey' => $operationKey, 'description' => $operationDes); $operations[] = $operation; } return $operations; }
public static function endsWith($str, $end, $ignoreCase = null) { return Text::endsWith($str, $end, $ignoreCase); }
public function boot(Container $container) { // Pails 不使用Phalcon的Module功能,通过Namespace组织Controllers. // 如果出现多级,比如AdminApi,则一定要以Namespace的形式组织 Admin\Api\xxxxController // // Note: from phalcon 3, closure bind di as $this by default. so no use($app) needed. // $container->setShared('router', function () { // $router = new Annotations(false); $router->removeExtraSlashes(true); $router->setEventsManager($this->get('eventsManager')); $router->setDefaultNamespace('App\\Controllers'); $router->setDefaultController('application'); $router->setDefaultAction('index'); // Process /config/routes.php // Verb Path Action Route Name // GET /photo index photo.index // GET /photo/create create photo.create // POST /photo store photo.store // GET /photo/{photo} show photo.show // GET /photo/{photo}/edit edit photo.edit // PUT/PATCH /photo/{photo} update photo.update // DELETE /photo/{photo} destroy photo.destroy foreach ($this->getConfig('routes') as $url => $route) { // $resourceDefaults = ['index', 'create', 'store', 'show', 'edit', 'update', 'destroy']; // // // a RESTful resource // if (isset($route['resource'])) { // if (!isset($route[''])) {} // foreach ($this->getResourceMethods($defaults, $options) as $m) { // $this->{'addResource'.ucfirst($m)}($url, $route, $options); // } // } else { // if (count($route) !== count($route, COUNT_RECURSIVE)) { // if (isset($route['pattern']) && isset($route['paths'])) { // $method = isset($route['httpMethods']) ? $route['httpMethods'] : null; // $router->add($route['pattern'], $route['paths'], $method); // } else { // throw new \RuntimeException(sprintf('No route pattern and paths found by route %s', $url)); // } // } else { // $router->add($url, $route); // } // } } // 定义注解路由 $iterator = new \RecursiveIteratorIterator(new \RecursiveDirectoryIterator($this->path('Controllers')), \RecursiveIteratorIterator::SELF_FIRST); foreach ($iterator as $item) { if (Text::endsWith($item, "Controller.php", false)) { $name = str_replace([$this->path('Controllers') . DIRECTORY_SEPARATOR, "Controller.php"], "", $item); $name = str_replace(DIRECTORY_SEPARATOR, "\\", $name); $router->addResource('App\\Controllers\\' . $name); } } // 定义404路由 $router->notFound(["controller" => "application", "action" => "notfound"]); // return $router; }); }
public function testEndsWithException() { $this->setExpectedException('\\Phalcon\\Exception'); \Phalcon\Text::endsWith(false, 'he'); }