Example #1
0
 /**
  * {@inheritdoc}
  */
 public function render(Request $request)
 {
     $view = parent::render($request);
     $queryParams = $request->getQueryParams();
     $page = max(1, array_get($queryParams, 'page'));
     $params = ['id' => (int) array_get($queryParams, 'id'), 'page' => ['near' => array_get($queryParams, 'near'), 'offset' => ($page - 1) * 20, 'limit' => 20]];
     $document = $this->preload($request->getAttribute('actor'), $params);
     $getResource = function ($link) use($document) {
         return array_first($document->included, function ($key, $value) use($link) {
             return $value->type === $link->type && $value->id === $link->id;
         });
     };
     $url = function ($newQueryParams) use($queryParams, $document) {
         $newQueryParams = array_merge($queryParams, $newQueryParams);
         $queryString = http_build_query($newQueryParams);
         return app('Flarum\\Forum\\UrlGenerator')->toRoute('discussion', ['id' => $document->data->id]) . ($queryString ? '?' . $queryString : '');
     };
     $posts = [];
     foreach ($document->included as $resource) {
         if ($resource->type === 'posts' && isset($resource->relationships->discussion) && isset($resource->attributes->contentHtml)) {
             $posts[] = $resource;
         }
     }
     $view->setTitle($document->data->attributes->title);
     $view->setDocument($document);
     $view->setContent(app('view')->make('flarum.forum::discussion', compact('document', 'page', 'getResource', 'posts', 'url')));
     return $view;
 }
 /**
  * @test
  */
 public function itAcceptsIntrospectionForNodeQuery()
 {
     $query = '{
       __schema {
         queryType {
           fields {
             name
             type {
               name
               kind
             }
             args {
               name
               type {
                 kind
                 ofType {
                   name
                   kind
                 }
               }
             }
           }
         }
       }
     }';
     $response = $this->graphqlResponse($query);
     $fields = array_get($response, 'data.__schema.queryType.fields');
     $nodeField = array_first($fields, function ($key, $value) {
         return $value['name'] == 'node';
     });
     $this->assertEquals(['name' => 'node', 'type' => ['name' => 'Node', 'kind' => 'INTERFACE'], 'args' => [['name' => 'id', 'type' => ['kind' => 'NON_NULL', 'ofType' => ['name' => 'ID', 'kind' => 'SCALAR']]]]], $nodeField);
 }
Example #3
0
 /**
  * Get the registered provider instance if exists
  *
  * @param \Illuminate\Support\ServiceProvider|string $provider
  *
  * @return \Illuminate\Support\ServiceProvider|null mixed
  */
 public function getProvider($provider)
 {
     $name = is_string($provider) ? $provider : get_class($provider);
     return array_first($this->providers, function ($key, $value) use($name) {
         return $value instanceof $name;
     });
 }
Example #4
0
 public static function isPathSupported($path)
 {
     $extension = array_first(array_keys(static::$compilers), function ($key, $value) use($path) {
         return ends_with($path, $value);
     });
     return $extension !== null;
 }
Example #5
0
 public function signin_onSubmit()
 {
     $rules = ['login' => 'required|min:2|max:32', 'password' => 'required|min:2'];
     $validation = Validator::make(post(), $rules);
     if ($validation->fails()) {
         throw new ValidationException($validation);
     }
     // Authenticate user
     $user = BackendAuth::authenticate(['login' => post('login'), 'password' => post('password')], true);
     // Load version updates
     UpdateManager::instance()->update();
     // Log the sign in event
     AccessLog::add($user);
     // User cannot access the dashboard
     if (!$user->hasAccess('backend.access_dashboard')) {
         $true = function () {
             return true;
         };
         if ($first = array_first(BackendMenu::listMainMenuItems(), $true)) {
             return Redirect::intended($first->url);
         }
     }
     // Redirect to the intended page after successful sign in
     return Redirect::intended(Backend::url('backend'));
 }
 /**
  * Before mail send trigger.
  */
 public function triggerBeforeMailSend($mail)
 {
     $config = $this->getConfig();
     $first_recipient = array_first_key($mail->message->getTo());
     if ($exception_driver = $this->getSendingMethodForEmailAddress($first_recipient, $config)) {
         $driver_class = '\\Rhymix\\Framework\\Drivers\\Mail\\' . $exception_driver;
         if (class_exists($driver_class)) {
             $mail->driver = $driver_class::getInstance(config("mail.{$exception_driver}"));
         }
     }
     if (!$mail->getFrom()) {
         $mail->setFrom($config->sender_email, $config->sender_name ?: null);
     } elseif (toBool($config->force_sender)) {
         if (stripos($mail->driver->getName(), 'woorimail') !== false && config('mail.woorimail.api_type') === 'free') {
             // no-op
         } else {
             $original_sender_email = array_first_key($mail->message->getFrom());
             $original_sender_name = array_first($mail->message->getFrom());
             if ($original_sender_email !== $config->sender_email) {
                 $mail->setFrom($config->sender_email, $original_sender_name ?: $config->sender_name);
                 $mail->setReplyTo($original_sender_email);
             }
         }
     }
 }
Example #7
0
 public function testArrayFirst()
 {
     $array = array('name' => 'taylor', 'otherDeveloper' => 'dayle');
     $this->assertEquals('dayle', array_first($array, function ($key, $value) {
         return $value == 'dayle';
     }));
 }
 public function __construct(PostTypeRepositoryInterface $postTypeRepos, PostRepositoryInterface $postRepos, TemplateRepositoryInterface $templateRepos, PostService $postService)
 {
     $this->postTypeRepos = $postTypeRepos;
     $this->postRepos = $postRepos;
     $this->templateRepos = $templateRepos;
     $this->postService = $postService;
     $this->authUser = Auth::user();
     if (!$this->authUser->is('superadmin')) {
         App::abort(403, 'Access denied');
     }
     //get the post type from the url param
     $routeParamters = Route::current()->parameters();
     $postTypeId = $routeParamters['posttypeid'];
     $this->postType = $this->postTypeRepos->find($postTypeId);
     //get the categories, category check convert to route middleware
     $categoryRootId = $this->postType->getConfiguredRootCategory();
     if (!$categoryRootId) {
         return Redirect::route('admin.post-types.configuration', [$postTypeId, "root"])->send();
     }
     $root = Category::find($categoryRootId);
     if (!$root->descendants()->count()) {
         return Redirect::route('admin.post-types.configuration', [$postTypeId, "descendants"])->send();
     }
     $categories = $root->descendants()->get();
     foreach ($categories as $category) {
         $this->categories[$category->id] = $category->title;
     }
     //localization
     $this->locales = Locale::where('language', '!=', 'en')->lists('language', 'language');
     $this->first_locale = array_first($this->locales, function () {
         return true;
     });
     //share the post type submenu to the layout
     View::share('postTypesSubmenu', $this->postTypeRepos->renderMenu());
 }
Example #9
0
 /**
  * Start the exception handling for the request.
  *
  * @return void
  */
 public function startExceptionHandling()
 {
     $provider = array_first($this->serviceProviders, function ($key, $provider) {
         return $provider instanceof ExceptionServiceProvider;
     });
     $provider->startHandling($this);
 }
Example #10
0
 /**
  * Initialize the cache system.
  * 
  * @param array $config
  * @return void
  */
 public static function init($config)
 {
     if (!is_array($config)) {
         $config = array($config);
     }
     if (isset($config['type'])) {
         $driver_name = $config['type'];
         $class_name = '\\Rhymix\\Framework\\Drivers\\Cache\\' . $config['type'];
         if (isset($config['ttl'])) {
             self::$_ttl = intval($config['ttl']);
         }
         $config = isset($config['servers']) ? $config['servers'] : array();
     } elseif (preg_match('/^(apc|dummy|file|memcache|redis|sqlite|wincache|xcache)/', strval(array_first($config)), $matches)) {
         $driver_name = $matches[1] . ($matches[1] === 'memcache' ? 'd' : '');
         $class_name = '\\Rhymix\\Framework\\Drivers\\Cache\\' . $driver_name;
     } else {
         $driver_name = null;
         $class_name = null;
     }
     if ($class_name && class_exists($class_name) && $class_name::isSupported()) {
         self::$_driver = $class_name::getInstance($config);
         self::$_driver_name = strtolower($driver_name);
     } else {
         self::$_driver = Drivers\Cache\Dummy::getInstance(array());
         self::$_driver_name = 'dummy';
     }
     if (self::$_driver->prefix) {
         self::$_prefix = substr(sha1(\RX_BASEDIR), 0, 10) . ':' . \RX_VERSION . ':';
     } else {
         self::$_prefix = \RX_VERSION . ':';
     }
     return self::$_driver;
 }
Example #11
0
 /**
  * Get an annotation by its type.
  *
  * @param string $type
  *
  * @return mixed
  */
 protected function getAnnotationByType($type)
 {
     return array_first($this->annotations, function ($key, $annotation) use($type) {
         $type = sprintf('Dingo\\Blueprint\\Annotation\\%s', $type);
         return $annotation instanceof $type;
     });
 }
 /**
  * Check the custom event classmap
  * & return the fully-qualified classname
  * if the event exists
  *
  * @param  string $eventName
  * @return string|null
  */
 private function checkCustomEventClassmap($eventName)
 {
     $classmap = config("event_subscriber.custom_event_classmap", []);
     return array_first($classmap, function ($key, $path) use($eventName) {
         $isMatch = preg_match('/' . $eventName . '$/', $path, $matches) === 1;
         return $isMatch && class_exists(array_get($matches, 1));
     }, null);
 }
Example #13
0
 public function testArrayFirst()
 {
     $array = ['test1', 'test2', 'test3'];
     // test array integrity
     $this->assertEquals('test1', array_first($array));
     $this->assertEquals(current($array), array_first($array));
     $this->assertEquals('test1', current($array));
 }
Example #14
0
/**
* Get the first item in an array.
*
* @param $array
*	...
*
* @param $traverseChildArrays
*	If the first item is a child array, treat the stack recursively and find the first non-array value.
*
* @return
*	...
*/
function array_first(array $array = array(), $traverseChildArrays = false)
{
    $result = reset($array);
    if ($traverseChildArrays and is_array($result)) {
        $result = array_first($result, true);
    }
    return $result;
}
Example #15
0
 public function getRegistered($provider)
 {
     $name = is_string($provider) ? $provider : get_class($provider);
     if (array_key_exists($name, $this->loadedProviders)) {
         return array_first($this->serviceProviders, function ($key, $value) use($name) {
             return get_class($value) == $name;
         });
     }
 }
Example #16
0
 /**
  * Returns configuration settings for the current day
  * (based on the current day path)
  */
 protected function getConfigSettingsForCurrentDay()
 {
     $allDays = Config::get('days');
     $page = Request::segment(1);
     // return the first item with the correct path
     return array_first($allDays, function ($key, $value) use($page) {
         return $value[self::DAY_PATH] == $page;
     });
 }
Example #17
0
 public function allowRules($rule)
 {
     $allow = array_first($this->getRules(), function ($index, $instance) use($rule) {
         if (str_is($instance, $rule)) {
             return true;
         }
     });
     return $allow !== null;
 }
Example #18
0
function get_valid_locale($requestedLocale)
{
    if (in_array($requestedLocale, config('app.available_locales'), true)) {
        return $requestedLocale;
    }
    return array_first(config('app.available_locales'), function ($_key, $value) use($requestedLocale) {
        return starts_with($requestedLocale, $value);
    }, config('app.fallback_locale'));
}
 /**
  * Find a model in the collection by key.
  *
  * @param  mixed  $key
  * @param  mixed  $default
  * @return \Illuminate\Database\Eloquent\Model
  */
 public function find($key, $default = null)
 {
     if ($key instanceof Model) {
         $key = $key->getKey();
     }
     return array_first($this->items, function ($itemKey, $model) use($key) {
         return $model->getKey() == $key;
     }, $default);
 }
Example #20
0
 /**
  * Find an entity in the collection by key.
  *
  * @param  mixed  $key
  * @param  mixed  $default
  * 
  * @return \Analogue\ORM\Entity
  */
 public function find($key, $default = null)
 {
     if ($key instanceof Mappable) {
         $key = $this->getEntityKey($key);
     }
     return array_first($this->items, function ($itemKey, $entity) use($key) {
         return $this->getEntityKey($entity) == $key;
     }, $default);
 }
Example #21
0
 /**
  * Check if the request is made to API route.
  *
  * @param $request
  * @return bool
  */
 private function isApiRequest($request)
 {
     $fistSegment = array_first($request->segments(), function ($key, $value) {
         return $value;
     });
     if ($fistSegment == 'api') {
         return true;
     }
     return false;
 }
Example #22
0
 public function route($verb, $path)
 {
     $route = array_first($this->routes, function ($i, $route) use($verb, $path) {
         return $route->match($verb, $path);
     });
     if (is_null($route)) {
         die("No matching route found.\n");
     }
     return $route;
 }
Example #23
0
 /**
  * Setup the object from the route parameters
  *
  * @param array $params
  *
  * @return FormInterface
  */
 public function setup(array $params)
 {
     $model = array_first($params, function ($key, $value) {
         return $value instanceof Model;
     });
     if ($model) {
         $this->editingModel($model);
     }
     return $this;
 }
Example #24
0
 /**
  * Match eagerly loaded child models to their parent models.
  *
  * @param  string  $relationship
  * @param  array   $parents
  * @param  array   $children
  * @return void
  */
 public function match($relationship, &$parents, $children)
 {
     $foreign = $this->foreign_key();
     foreach ($parents as &$parent) {
         $matching = array_first($children, function ($k, $v) use(&$parent, $foreign) {
             return $v->{$foreign} == $parent->get_key();
         });
         $parent->relationships[$relationship] = $matching;
     }
 }
Example #25
0
 /**
  * Find a model in the collection by key.
  *
  * @param  mixed  $key
  * @param  mixed  $default
  *
  * @return DataModel
  */
 public function find($key, $default = null)
 {
     if ($key instanceof DataModel) {
         $key = $key->getId();
     }
     return array_first($this->items, function ($itemKey, $model) use($key) {
         /** @var DataModel $model */
         return $model->getId() == $key;
     }, $default);
 }
 /**
  * Allows you retrieve the most recent social provider
  * authorized with.
  *
  * @return mixed
  */
 public function getRecentAuthData()
 {
     $providers = $this->session->get('__sp_auth.p');
     if (!$providers) {
         return null;
     }
     return array_first($providers, function ($provider) {
         return $provider === $this->session->get('__sp_auth.r');
     });
 }
Example #27
0
 /**
  * @param $role
  * @return bool
  */
 public function inRole($role)
 {
     $role = array_first($this->roles, function ($index, $instance) use($role) {
         if ($instance->getRoleId() == $role || $instance->getRoleSlug() == $role) {
             return true;
         }
         return false;
     });
     return $role !== null;
 }
Example #28
0
 private function endpoint()
 {
     $endpoint = $this->config['endpoint'];
     if (!isset($this->endpoint)) {
         $endpoint = is_array($endpoint) ? array_first($endpoint) : $endpoint;
     } else {
         $endpoint = $endpoint[$this->endpoint];
     }
     return new Client(['base_uri' => $endpoint]);
     $this->apiHeaders = [];
 }
Example #29
0
 /**
  * Custom permissions check that will redirect to the next
  * available menu item, if permission to this page is denied.
  */
 protected function checkPermissionRedirect()
 {
     if (!$this->user->hasAccess('backend.access_dashboard')) {
         $true = function () {
             return true;
         };
         if ($first = array_first(BackendMenu::listMainMenuItems(), $true)) {
             return Redirect::intended($first->url);
         }
     }
 }
Example #30
0
 public function getThumbnail(string $url)
 {
     try {
         $data = $this->getData($url);
         $image = array_first($data['links']['thumbnail'], function ($key, $value) {
             return $this->isImage($value);
         });
         return data_get($image, 'href', null);
     } catch (RequestException $e) {
     }
     return null;
 }