Пример #1
0
 public function acfSelectOptions($group_name, $field_name)
 {
     $settings = get_posts(['name' => "acf_{$group_name}", 'post_type' => 'acf']);
     if (!count($settings)) {
         return [];
     }
     $field_meta = F\select(F\pluck(get_post_meta($settings[0]->ID), 0), function ($val, $key) use($field_name) {
         return strpos($key, 'field_') === 0 && ($data = unserialize($val)) && $data['name'] === $field_name;
     });
     if (!count($field_meta)) {
         return [];
     }
     $data = unserialize(reset($field_meta));
     return $data['choices'];
 }
 /**
  * Gets a list of all the published events
  *
  * @ApiDoc(
  *  resource = true,
  *  description = "Gets a list of all the published events, optionally filtered by a given date",
  *  filters = {
  *      {"name" = "since", "dataType" = "integer"},
  *      {"name" = "page", "dataType" = "integer"},
  *      {"name" = "limit", "dataType" = "integer"},
  *  },
  *  output = "Hateoas\Representation\CollectionRepresentation",
  *  statusCodes = {
  *      200 = "Returned along with the list of published events"
  *  }
  * )
  */
 public function getEventsAction(Request $request)
 {
     $page = $request->query->get('page', 1);
     $limit = $request->query->get('limit', (int) $this->getParameter('events_pagination_limit'));
     $pager = $this->get('published_events_pager');
     $pager->setCurrentPage($page);
     $pager->setMaxPerPage($limit);
     if ($request->query->has('since')) {
         $since = $request->query->get('since');
         $events = select(map($this->get('snc_redis.default')->lrange($this->getParameter('published_events_key'), 0, -1), new DeserializationCallable($this->get('jms_serializer'))), function ($event) use($since) {
             return (int) $event['created_on'] >= (int) $since;
         });
         $pager = (new Pagerfanta(new ArrayAdapter($events)))->setCurrentPage($page)->setMaxPerPage($limit);
     }
     return (new PagerfantaFactory())->createRepresentation($pager, new Route('get_events', [], true));
 }
 public function handle()
 {
     $this->info('Initializing Leader Selection...');
     // Only do cron setup if environment is configured to use it (This way we don't accidentally run on workers)
     if (getenv('USE_CRON') == 'true') {
         //check to see if we are in an instance
         $ch = curl_init('http://169.254.169.254/latest/meta-data/instance-id');
         //magic ip from AWS
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
         if ($result = curl_exec($ch)) {
             $this->info('Instance ID: ' . $result);
             // Get this instance metadata so we can find the environment it's running in
             $tags = $info = $this->ecClient->describeInstances(['Filters' => [['Name' => 'instance-id', 'Values' => [$result]]]])->get('Reservations')[0]['Instances'][0]['Tags'];
             // Get environment name
             $environmentName = F\first($tags, function ($tagArray) {
                 return $tagArray['Key'] == 'elasticbeanstalk:environment-name';
             })['Value'];
             $this->info('Environment: ' . $environmentName);
             $this->info('Getting Instances with Environment: ' . $environmentName);
             // Get instances that have this environment tagged
             $info = $this->ecClient->describeInstances(['Filters' => [['Name' => 'tag-value', 'Values' => [$environmentName]]]]);
             $instances = F\map($info->get('Reservations'), function ($i) {
                 return current($i['Instances']);
             });
             $this->info('Getting potential instances...');
             // Only want instances that are running
             $candidateInstances = F\select($instances, function ($instanceMeta) {
                 return $instanceMeta['State']['Code'] == 16;
             });
             $leader = false;
             if (!empty($candidateInstances)) {
                 //there are instances running
                 if (count($candidateInstances) > 1) {
                     // if there is more than one we sort by launch time and get the oldest
                     $this->info('More than one instance running, finding the oldest...');
                     $oldestInstance = F\sort($candidateInstances, function ($left, $right) {
                         return $left['LaunchTime'] > $right['LaunchTime'];
                     })[0];
                 } else {
                     $this->info('Only one instance running...');
                     $oldestInstance = reset($candidateInstances);
                 }
                 if ($oldestInstance['InstanceId'] == $result) {
                     // if this instance is the oldest instance it's the leader
                     $leader = true;
                 }
             } else {
                 $this->info('No candidate instances found. \'O Brave New World!');
                 $leader = true;
             }
             // No leader is running so we'll setup this one as the leader
             // and create a cron entry to run the scheduler
             if ($leader) {
                 $this->info('We are the Leader! Initiating Cron Setup');
                 $this->call('system:start:cron');
             } else {
                 // Instance was found, don't do any cron stuff
                 $this->info('We are not a leader instance :( Maybe next time...');
                 $this->info('Leader should be running on Instance ' . $leader['InstanceId']);
             }
             $this->info('Leader Selection Done!');
         } else {
             // Probably be run from your local machine
             $this->error('Did not detect an ec2 environment. Exiting.');
         }
     } else {
         $this->info('USE_CRON env var not set. Exiting.');
     }
 }
    // That returns a fib sequence under a certain number
    $solution = reduce_left(select(new takeWhileFib(4000000), $isEven), function ($value, $index, $collection, $reduction) {
        return $reduction + $value;
    }, 0);
    return $solution;
});
Route::get('problem3', function () {
    //The prime factors of 13195 are 5, 7, 13 and 29.
    //What is the largest prime factor of the number 600851475143 ?
    // How can we do this in a functional way?
    // Well Brute Force would be...
    // max($number => getPrimeFactors)
    $pf = function ($number) {
        return select(new takeWhile($number), function ($n, $number) {
            if ($n > 0) {
                return $number % $n == 0;
            }
            return false;
        });
    };
    //    $answer = max($pf(30));
    return $pf(10);
});
/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| This route group applies the "web" middleware group to every route
| it contains. The "web" middleware group is defined in your HTTP
| kernel and includes session state, CSRF protection, and more.
|
Пример #5
0
 protected function acfImagesInfo($field_name, $post_id)
 {
     $acf_images = get_field($field_name, $post_id);
     $html_id = $post_id ? "{$field_name}-{$post_id}" : $field_name;
     $images = empty($acf_images) ? [] : F\map(F\select($acf_images, function ($acf_image) {
         return strpos($acf_image['mime_type'], 'image') === 0;
     }), array($this, 'getImageAttributesFromAcfImage'));
     $first = count($images) ? $images[0] : NULL;
     return compact('html_id', 'images', 'first');
 }
 /**
  * @param callable $f
  * @return array
  */
 public function filter(\Closure $f)
 {
     return F\select($this->_data, $f);
 }