473,396 Members | 1,836 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

How to understand $this->get('user_locator')->getUserGeoBoundaries();

103 100+
I am following course bout Symfony2, Extending Symfony2 Web Application Framework, Chapter 1
https://www.packtpub.com/packtlib/bo...sec08/Services

I can not understand how is implemented line:
Expand|Select|Wrap|Line Numbers
  1.  $boundaries = $this->get('user_locator')->getUserGeoBoundaries();
Where is defined 'user_locator', where is defined method getUserGeoBoundaries() ?

C:\Bitnami\wampstack-5.5.30-0\sym_prog\ExtSym2\src\Khepin\BookBundle\Controlle r\DefaultController.php

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. namespace Khepin\BookBundle\Controller;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Controller\Controller;
  6. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
  7. use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
  8. use Khepin\BookBundle\Form\JoinEventType;
  9. use Khepin\BookBundle\Event\MeetupEvent;
  10. use Khepin\BookBundle\Event\MeetupEvents;
  11.  
  12. use Geocoder\HttpAdapter\CurlHttpAdapter;
  13. use Geocoder\Geocoder;
  14. use Geocoder\Provider\FreeGeoIpProvider;
  15.  
  16. class DefaultController extends Controller
  17. {
  18.     /**
  19.      * @Route("/")
  20.      * @Template()
  21.      */
  22.     public function indexAction()
  23.     {
  24.         $boundaries = $this->get('user_locator')->getUserGeoBoundaries();
  25.  
  26.         // Create our database query
  27.         $em = $this->getDoctrine()->getManager();
  28.  
  29.         $qb = $em->createQueryBuilder();
  30.         $qb->select('e')
  31.             ->from('KhepinBookBundle:Event', 'e')
  32.             ->where('e.latitude < :lat_max')
  33.             ->andWhere('e.latitude > :lat_min')
  34.             ->andWhere('e.longitude < :long_max')
  35.             ->andWhere('e.longitude > :long_min')
  36.             ->setParameters($boundaries);
  37.  
  38.         // Retrieve interesting events
  39.         $events = $qb->getQuery()->execute();
  40.  
  41.         return compact('events');
  42.     }
  43. ..
  44. }

C:\Bitnami\wampstack-5.5.30-0\sym_prog\ExtSym2\vendor\willdurand\geocoder\src\ Geocoder\Geocoder.php
Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /**
  4.  * This file is part of the Geocoder package.
  5.  * For the full copyright and license information, please view the LICENSE
  6.  * file that was distributed with this source code.
  7.  *
  8.  * @license    MIT License
  9.  */
  10.  
  11. namespace Geocoder;
  12.  
  13. use Geocoder\Provider\ProviderInterface;
  14. use Geocoder\Result\ResultFactoryInterface;
  15. use Geocoder\Result\DefaultResultFactory;
  16.  
  17. /**
  18.  * @author William Durand <william.durand1@gmail.com>
  19.  */
  20. class Geocoder implements GeocoderInterface
  21. {
  22.     /**
  23.      * Version
  24.      */
  25.     const VERSION = '2.3.2';
  26.  
  27.     /**
  28.      * @var integer
  29.      */
  30.     const MAX_RESULTS = 5;
  31.  
  32.     /**
  33.      * @var ProviderInterface[]
  34.      */
  35.     private $providers = array();
  36.  
  37.     /**
  38.      * @var ProviderInterface
  39.      */
  40.     private $provider;
  41.  
  42.     /**
  43.      * @var ResultFactoryInterface
  44.      */
  45.     private $resultFactory;
  46.  
  47.     /**
  48.      * @var integer
  49.      */
  50.     private $maxResults;
  51.  
  52.     /**
  53.      * @param ProviderInterface      $provider
  54.      * @param ResultFactoryInterface $resultFactory
  55.      * @param integer                $maxResults
  56.      */
  57.     public function __construct(ProviderInterface $provider = null, ResultFactoryInterface $resultFactory = null, $maxResults = self::MAX_RESULTS)
  58.     {
  59.         $this->provider = $provider;
  60.  
  61.         $this->setResultFactory($resultFactory);
  62.         $this->limit($maxResults);
  63.     }
  64.  
  65.     /**
  66.      * @param ResultFactoryInterface $resultFactory
  67.      */
  68.     public function setResultFactory(ResultFactoryInterface $resultFactory = null)
  69.     {
  70.         $this->resultFactory = $resultFactory ?: new DefaultResultFactory();
  71.     }
  72.  
  73.     /**
  74.      * @param integer $maxResults
  75.      *
  76.      * @return GeocoderInterface
  77.      */
  78.     public function limit($maxResults)
  79.     {
  80.         $this->maxResults = $maxResults;
  81.  
  82.         return $this;
  83.     }
  84.  
  85.     /**
  86.      * @return integer $maxResults
  87.      */
  88.     public function getMaxResults()
  89.     {
  90.         return $this->maxResults;
  91.     }
  92.  
  93.     /**
  94.      * {@inheritDoc}
  95.      */
  96.     public function geocode($value)
  97.     {
  98.         if (empty($value)) {
  99.             // let's save a request
  100.             return $this->returnResult(array());
  101.         }
  102.  
  103.         $provider = $this->getProvider()->setMaxResults($this->getMaxResults());
  104.         $data     = $provider->getGeocodedData(trim($value));
  105.         $result   = $this->returnResult($data);
  106.  
  107.         return $result;
  108.     }
  109.  
  110.     /**
  111.      * {@inheritDoc}
  112.      */
  113.     public function reverse($latitude, $longitude)
  114.     {
  115.         if (empty($latitude) || empty($longitude)) {
  116.             // let's save a request
  117.             return $this->returnResult(array());
  118.         }
  119.  
  120.         $provider = $this->getProvider()->setMaxResults($this->getMaxResults());
  121.         $data     = $provider->getReversedData(array($latitude, $longitude));
  122.         $result   = $this->returnResult($data);
  123.  
  124.         return $result;
  125.     }
  126.  
  127.     /**
  128.      * Registers a provider.
  129.      *
  130.      * @param ProviderInterface $provider
  131.      *
  132.      * @return GeocoderInterface
  133.      */
  134.     public function registerProvider(ProviderInterface $provider)
  135.     {
  136.         if (null !== $provider) {
  137.             $this->providers[$provider->getName()] = $provider;
  138.         }
  139.  
  140.         return $this;
  141.     }
  142.  
  143.     /**
  144.      * Registers a set of providers.
  145.      *
  146.      * @param ProviderInterface[] $providers
  147.      *
  148.      * @return GeocoderInterface
  149.      */
  150.     public function registerProviders(array $providers = array())
  151.     {
  152.         foreach ($providers as $provider) {
  153.             $this->registerProvider($provider);
  154.         }
  155.  
  156.         return $this;
  157.     }
  158.  
  159.     /**
  160.      * Sets the provider to use.
  161.      *
  162.      * @param string $name A provider's name
  163.      *
  164.      * @return GeocoderInterface
  165.      */
  166.     public function using($name)
  167.     {
  168.         if (isset($this->providers[$name])) {
  169.             $this->provider = $this->providers[$name];
  170.         }
  171.  
  172.         return $this;
  173.     }
  174.  
  175.     /**
  176.      * Returns registered providers indexed by name.
  177.      *
  178.      * @return ProviderInterface[]
  179.      */
  180.     public function getProviders()
  181.     {
  182.         return $this->providers;
  183.     }
  184.  
  185.     /**
  186.      * Returns the provider to use.
  187.      *
  188.      * @return ProviderInterface
  189.      */
  190.     protected function getProvider()
  191.     {
  192.         if (null === $this->provider) {
  193.             if (0 === count($this->providers)) {
  194.                 throw new \RuntimeException('No provider registered.');
  195.             } else {
  196.                 $this->provider = $this->providers[key($this->providers)];
  197.             }
  198.         }
  199.  
  200.         return $this->provider;
  201.     }
  202.  
  203.     /**
  204.      * @param array $data An array of data.
  205.      *
  206.      * @return ResultInterface
  207.      */
  208.     protected function returnResult(array $data = array())
  209.     {
  210.         return $this->resultFactory->createFromArray($data);
  211.     }
  212. }
  213.  
  214.  
  215.  
  216. C:\Bitnami\wampstack-5.5.30-0\sym_prog\ExtSym2\vendor\willdurand\geocoder\src\Geocoder\Provider\FreeGeoIpProvider.php
  217. <?php
  218.  
  219. /**
  220.  * This file is part of the Geocoder package.
  221.  * For the full copyright and license information, please view the LICENSE
  222.  * file that was distributed with this source code.
  223.  *
  224.  * @license    MIT License
  225.  */
  226.  
  227. namespace Geocoder\Provider;
  228.  
  229. use Geocoder\Exception\NoResultException;
  230. use Geocoder\Exception\UnsupportedException;
  231.  
  232. /**
  233.  * @author William Durand <william.durand1@gmail.com>
  234.  */
  235. class FreeGeoIpProvider extends AbstractProvider implements ProviderInterface
  236. {
  237.     /**
  238.      * @var string
  239.      */
  240.     const ENDPOINT_URL = 'http://freegeoip.net/json/%s';
  241.  
  242.     /**
  243.      * {@inheritDoc}
  244.      */
  245.     public function getGeocodedData($address)
  246.     {
  247.         if (!filter_var($address, FILTER_VALIDATE_IP)) {
  248.             throw new UnsupportedException('The FreeGeoIpProvider does not support Street addresses.');
  249.         }
  250.  
  251.         if (in_array($address, array('127.0.0.1', '::1'))) {
  252.             return array($this->getLocalhostDefaults());
  253.         }
  254.  
  255.         $query = sprintf(self::ENDPOINT_URL, $address);
  256.  
  257.         return $this->executeQuery($query);
  258.     }
  259.  
  260.     /**
  261.      * {@inheritDoc}
  262.      */
  263.     public function getReversedData(array $coordinates)
  264.     {
  265.         throw new UnsupportedException('The FreeGeoIpProvider is not able to do reverse geocoding.');
  266.     }
  267.  
  268.     /**
  269.      * {@inheritDoc}
  270.      */
  271.     public function getName()
  272.     {
  273.         return 'free_geo_ip';
  274.     }
  275.  
  276.     /**
  277.      * @param string $query
  278.      *
  279.      * @return array
  280.      */
  281.     protected function executeQuery($query)
  282.     {
  283.         $content = $this->getAdapter()->getContent($query);
  284.  
  285.         if (null === $content) {
  286.             throw new NoResultException(sprintf('Could not execute query %s', $query));
  287.         }
  288.  
  289.         $data = (array) json_decode($content);
  290.  
  291.         if (empty($data)) {
  292.             throw new NoResultException(sprintf('Could not execute query %s', $query));
  293.         }
  294.  
  295.         //it appears that for US states the region code is not returning the FIPS standard
  296.         if ('US' === $data['country_code'] && isset($data['region_code']) && !is_numeric($data['region_code'])) {
  297.             $newRegionCode = $this->stateToRegionCode($data['region_code']);
  298.             $data['region_code'] = is_numeric($newRegionCode) ? $newRegionCode : null;
  299.         }
  300.  
  301.         return array(array_merge($this->getDefaults(), array(
  302.             'latitude'    => isset($data['latitude']) ? $data['latitude'] : null,
  303.             'longitude'   => isset($data['longitude']) ? $data['longitude'] : null,
  304.             'city'        => isset($data['city']) ? $data['city'] : null,
  305.             'zipcode'     => isset($data['zipcode']) ? $data['zipcode'] : null,
  306.             'region'      => isset($data['region_name']) ? $data['region_name'] : null,
  307.             'regionCode'  => isset($data['region_code']) ? $data['region_code'] : null,
  308.             'country'     => isset($data['country_name']) ? $data['country_name'] : null,
  309.             'countryCode' => isset($data['country_code']) ? $data['country_code'] : null,
  310.         )));
  311.     }
  312.  
  313.     /**
  314.      * Converts the state code to FIPS standard
  315.      *
  316.      * @param string $state
  317.      *
  318.      * @return string|integer The FIPS code or the state code if not found
  319.      */
  320.     private function stateToRegionCode($state)
  321.     {
  322.         $codes = $this->getRegionCodes();
  323.  
  324.         return array_key_exists($state, $codes) ? $codes[$state] : $state;
  325.     }
  326.  
  327.     /**
  328.      * Returns an array of state codes => FIPS codes
  329.      * @see http://www.epa.gov/enviro/html/codes/state.html
  330.      *
  331.      * @return array
  332.      */
  333.     private function getRegionCodes()
  334.     {
  335.         return array(
  336.             'AK' => 2, //ALASKA
  337.             'AL' => 1, //ALABAMA
  338.             'AR' => 5, //ARKANSAS
  339.             'AS' => 60, //AMERICAN SAMOA
  340.             'AZ' => 4, //ARIZONA
  341.             'CA' => 6, //CALIFORNIA
  342.             'CO' => 8, //COLORADO
  343.             'CT' => 9, //CONNECTICUT
  344.             'DC' => 11, //DISTRICT OF COLUMBIA
  345.             'DE' => 10, //DELAWARE
  346.             'FL' => 12, //FLORIDA
  347.             'GA' => 13, //GEORGIA
  348.             'GU' => 66, //GUAM
  349.             'HI' => 15, //HAWAII
  350.             'IA' => 19, //IOWA
  351.             'ID' => 16, //IDAHO
  352.             'IL' => 17, //ILLINOIS
  353.             'IN' => 18, //INDIANA
  354.             'KS' => 20, //KANSAS
  355.             'KY' => 21, //KENTUCKY
  356.             'LA' => 22, //LOUISIANA
  357.             'MA' => 25, //MASSACHUSETTS
  358.             'MD' => 24, //MARYLAND
  359.             'ME' => 23, //MAINE
  360.             'MI' => 26, //MICHIGAN
  361.             'MN' => 27, //MINNESOTA
  362.             'MO' => 29, //MISSOURI
  363.             'MS' => 28, //MISSISSIPPI
  364.             'MT' => 30, //MONTANA
  365.             'NC' => 37, //NORTH CAROLINA
  366.             'ND' => 38, //NORTH DAKOTA
  367.             'NE' => 31, //NEBRASKA
  368.             'NH' => 33, //NEW HAMPSHIRE
  369.             'NJ' => 34, //NEW JERSEY
  370.             'NM' => 35, //NEW MEXICO
  371.             'NV' => 32, //NEVADA
  372.             'NY' => 36, //NEW YORK
  373.             'OH' => 39, //OHIO
  374.             'OK' => 40, //OKLAHOMA
  375.             'OR' => 41, //OREGON
  376.             'PA' => 42, //PENNSYLVANIA
  377.             'PR' => 72, //PUERTO RICO
  378.             'RI' => 44, //RHODE ISLAND
  379.             'SC' => 45, //SOUTH CAROLINA
  380.             'SD' => 46, //SOUTH DAKOTA
  381.             'TN' => 47, //TENNESSEE
  382.             'TX' => 48, //TEXAS
  383.             'UT' => 49, //UTAH
  384.             'VA' => 51, //VIRGINIA
  385.             'VI' => 78, //VIRGIN ISLANDS
  386.             'VT' => 50, //VERMONT
  387.             'WA' => 53, //WASHINGTON
  388.             'WI' => 55, //WISCONSIN
  389.             'WV' => 54, //WEST VIRGINIA
  390.         );
  391.     }
  392. }
Nov 16 '15 #1
2 1215
gintare
103 100+
I am sorry, i have found the method getUserGeoBoundaries(); in
\src\Khepin\BookBundle\Geo\UserLocator.php

but it is not clear for me how we can access it from \src\Khepin\BookBundle\Controller\DefaultControlle r.php
Nov 16 '15 #2
gintare
103 100+
ANSWER!

$this->get('user_locator') means that i am getting service named 'user-locator' using method get, which is defined in every class, which extends from
Expand|Select|Wrap|Line Numbers
  1. Symfony\Bundle\FrameworkBundle\Controller\Controller
.

sym_prog\ExtSym2\app\config\services.yml
Expand|Select|Wrap|Line Numbers
  1. services:
  2. ...
  3.     user_locator:
  4.         class: Khepin\BookBundle\Geo\UserLocator
  5.         scope: request
  6.         arguments: [@geocoder, @request]
This service points to the class
Expand|Select|Wrap|Line Numbers
  1. Khepin\BookBundle\Geo\UserLocator
, which has a method
Expand|Select|Wrap|Line Numbers
  1. getUserGeoBoundaries()
.
Nov 16 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own...
10
by: forgotten field | last post by:
Hi,how are you? I have been studying C++ by myself, but recently I am having a real problem. I am learning about the basic usage of a doubly linked list using polymorphism. However, I have got the...
10
by: Simon Mansfield | last post by:
I have been given a list of exercises to do by my tutor, one of which is this: http://www.cems.uwe.ac.uk/~amclymer/Software%20Design%20and%20C++/Exercises/Exercise%208/Exercise.html Which i am...
9
by: TCMA | last post by:
I am looking for some tools to help me understand source code of a program written in C++ by someone else. Are there any non-commercial, open source C or C++ tools to reverse engineer C or C++...
51
by: Richard Hengeveld | last post by:
Hi all, I'm trying to understand how pointers for function parameters work. As I understand it, if you got a function like: void f(int *i) { *i = 0; }
2
by: Tony Johansson | last post by:
Hello!! Below I have three classes. They are Base, Sub and Test. Sub is inheriting from Base. The main issue here is to pass the reference of Test up to the base class Base. This work but I...
54
by: smnoff | last post by:
Below is a section from string.c at this linkhttp://cvs.opensolaris.org/source/xref/on/usr/src/common/util/string.cthat I am trying to fully understand.I don't fully understand LINE 514; not to...
3
by: Ben Thomas | last post by:
Hello, I have the following code which I don't understand why it works : #include <iostream> using namespace std; void DontWork (unsigned int& i) { cout << i << endl; }
6
by: CD1 | last post by:
#include <string> This code won't compile. There is no variable called "foo2". :)
6
by: Bishop | last post by:
a += (UInt32)(url + (url << 8) + (url << 16) + (url << 24)); I'm trying to understand what the above line of code does. I understand that the url part of (url << 8) returns part of the string...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.