Overview

Namespaces

  • Autarky
    • Config
      • Loaders
    • Console
    • Container
      • Exception
      • Factory
      • Proxy
    • Database
    • Errors
    • Events
    • Files
    • Http
    • Logging
    • Providers
    • Routing
      • Events
    • Testing
    • TwigTemplating
      • Extensions
    • Utils

Classes

  • Autarky\Application
  • Autarky\Config\ConfigProvider
  • Autarky\Config\FileStore
  • Autarky\Config\LoaderFactory
  • Autarky\Config\Loaders\CachingYamlFileLoader
  • Autarky\Config\Loaders\PhpFileLoader
  • Autarky\Config\Loaders\YamlFileLoader
  • Autarky\Console\Application
  • Autarky\Console\Command
  • Autarky\Console\RouteDispatchCommand
  • Autarky\Console\RouteListCommand
  • Autarky\Container\Container
  • Autarky\Container\ContainerProvider
  • Autarky\Container\Factory\AbstractArgument
  • Autarky\Container\Factory\ClassArgument
  • Autarky\Container\Factory\Definition
  • Autarky\Container\Factory\Factory
  • Autarky\Container\Factory\ScalarArgument
  • Autarky\Container\Proxy\AbstractProxy
  • Autarky\Container\Proxy\ProxyProvider
  • Autarky\Database\ConnectionManager
  • Autarky\Database\DatabaseProvider
  • Autarky\Errors\ErrorHandlerManager
  • Autarky\Errors\ErrorHandlerProvider
  • Autarky\Errors\StubErrorHandler
  • Autarky\Events\EventDispatcher
  • Autarky\Events\EventDispatcherProvider
  • Autarky\Files\LockingFilesystem
  • Autarky\Files\PathResolver
  • Autarky\Http\CookieMiddleware
  • Autarky\Http\CookieProvider
  • Autarky\Http\CookieQueue
  • Autarky\Http\SessionMiddleware
  • Autarky\Http\SessionProvider
  • Autarky\Logging\ChannelManager
  • Autarky\Logging\DefaultLogConfigurator
  • Autarky\Logging\LoggingErrorHandler
  • Autarky\Logging\LoggingProvider
  • Autarky\Provider
  • Autarky\Providers\AbstractDependantProvider
  • Autarky\Providers\AbstractProvider
  • Autarky\Routing\Configuration
  • Autarky\Routing\Controller
  • Autarky\Routing\DefaultRouteConfigurator
  • Autarky\Routing\Events\AbstractRouteEvent
  • Autarky\Routing\Events\AfterEvent
  • Autarky\Routing\Events\BeforeEvent
  • Autarky\Routing\Events\RouteMatchedEvent
  • Autarky\Routing\Route
  • Autarky\Routing\RoutePathGenerator
  • Autarky\Routing\Router
  • Autarky\Routing\RoutingProvider
  • Autarky\Routing\UrlGenerator
  • Autarky\Testing\TestCase
  • Autarky\Testing\WebTestCase
  • Autarky\TwigTemplating\Extensions\PartialExtension
  • Autarky\TwigTemplating\Extensions\SessionExtension
  • Autarky\TwigTemplating\Extensions\UrlGenerationExtension
  • Autarky\TwigTemplating\Template
  • Autarky\TwigTemplating\TemplateContext
  • Autarky\TwigTemplating\TemplateEvent
  • Autarky\TwigTemplating\TemplatingEngine
  • Autarky\TwigTemplating\TwigEnvironment
  • Autarky\TwigTemplating\TwigTemplate
  • Autarky\TwigTemplating\TwigTemplatingProvider
  • Autarky\Utils\ArrayUtil

Interfaces

  • Autarky\Config\ConfigInterface
  • Autarky\Config\LoaderInterface
  • Autarky\ConfiguratorInterface
  • Autarky\Container\CallableInvokerInterface
  • Autarky\Container\ClassResolverInterface
  • Autarky\Container\ContainerAwareInterface
  • Autarky\Container\ContainerInterface
  • Autarky\Container\Factory\ArgumentInterface
  • Autarky\Container\Factory\FactoryInterface
  • Autarky\Database\ConnectionFactoryInterface
  • Autarky\Errors\ErrorHandlerInterface
  • Autarky\Errors\ErrorHandlerManagerInterface
  • Autarky\Events\EventDispatcherAwareInterface
  • Autarky\Providers\ConsoleProviderInterface
  • Autarky\Providers\DependantProviderInterface
  • Autarky\Providers\ProviderInterface
  • Autarky\Routing\InvokerInterface
  • Autarky\Routing\RoutePathGeneratorInterface
  • Autarky\Routing\RouterInterface

Traits

  • Autarky\Container\ContainerAwareTrait
  • Autarky\Events\EventDispatcherAwareTrait
  • Autarky\Routing\ControllerTrait

Exceptions

  • Autarky\Config\LoadException
  • Autarky\Container\Exception\ContainerException
  • Autarky\Container\Exception\NotInstantiableException
  • Autarky\Container\Exception\ResolvingException
  • Autarky\Container\Exception\ResolvingInternalException
  • Autarky\Container\Exception\UnresolvableArgumentException
  • Autarky\Database\CannotConnectException
  • Autarky\Files\IOException
  • Autarky\Providers\ProviderException
  • Overview
  • Namespace
  • Class
  1: <?php
  2: /**
  3:  * This file is part of the Autarky package.
  4:  *
  5:  * (c) Andreas Lutro <anlutro@gmail.com>
  6:  *
  7:  * For the full copyright and license information, please view the LICENSE
  8:  * file that was distributed with this source code.
  9:  */
 10: 
 11: namespace Autarky\Routing;
 12: 
 13: use Symfony\Component\HttpFoundation\Request;
 14: use Symfony\Component\HttpFoundation\Response;
 15: use Symfony\Component\HttpFoundation\RedirectResponse;
 16: use Symfony\Component\HttpFoundation\JsonResponse;
 17: use Symfony\Component\EventDispatcher\Event;
 18: 
 19: use Autarky\Container\ContainerAwareTrait;
 20: 
 21: /**
 22:  * Trait for controller functionality. Any class that implements this trait
 23:  * should also implement the interface Autarky\Container\ContainerAwareInterface
 24:  * - the interface's methods are implemented by the trait, but you still need to
 25:  * implement the interface on the class.
 26:  */
 27: trait ControllerTrait
 28: {
 29:     use ContainerAwareTrait;
 30: 
 31:     /**
 32:      * Render a template.
 33:      *
 34:      * @param  string $name Name of the view.
 35:      * @param  array  $data Data to pass to the view.
 36:      *
 37:      * @return string
 38:      */
 39:     protected function render($name, array $data = array())
 40:     {
 41:         return $this->container->resolve('Autarky\TwigTemplating\TemplatingEngine')
 42:             ->render($name, $data);
 43:     }
 44: 
 45:     /**
 46:      * Generate the URL to a route.
 47:      *
 48:      * @param  string $name   Name of the route.
 49:      * @param  array  $params Route parameters.
 50:      *
 51:      * @return string
 52:      */
 53:     protected function url($name, array $params = array())
 54:     {
 55:         return $this->container->resolve('Autarky\Routing\UrlGenerator')
 56:             ->getRouteUrl($name, $params);
 57:     }
 58: 
 59:     /**
 60:      * Get the session manager.
 61:      *
 62:      * @return \Symfony\Component\HttpFoundation\Session\Session
 63:      */
 64:     protected function getSession()
 65:     {
 66:         return $this->container->resolve('Symfony\Component\HttpFoundation\Session\Session');
 67:     }
 68: 
 69:     /**
 70:      * Flash something to the session.
 71:      *
 72:      * @param  string $key
 73:      * @param  mixed  $value
 74:      *
 75:      * @return void
 76:      */
 77:     protected function flash($key, $value)
 78:     {
 79:         $this->getSession()
 80:             ->getFlashBag()
 81:             ->set($key, $value);
 82:     }
 83: 
 84:     /**
 85:      * Flash an array of messages to the session.
 86:      *
 87:      * @param  array  $messages
 88:      *
 89:      * @return void
 90:      */
 91:     protected function flashMessages($messages)
 92:     {
 93:         $flashBag = $this->getSession()
 94:             ->getFlashBag();
 95: 
 96:         foreach ((array) $messages as $message) {
 97:             $flashBag->add('_messages', $message);
 98:         }
 99:     }
100: 
101:     /**
102:      * Flash input to session.
103:      *
104:      * @param  \Symfony\Component\HttpFoundation\Request $request Optional
105:      *
106:      * @return void
107:      */
108:     protected function flashInput(Request $request = null)
109:     {
110:         if ($request === null) {
111:             $request = $this->container
112:                 ->resolve('Symfony\Component\HttpFoundation\RequestStack')
113:                 ->getCurrentRequest();
114:         }
115: 
116:         $this->flash('_old_input', $request->request->all());
117:     }
118: 
119:     /**
120:      * Get old input flashed to the session.
121:      *
122:      * @return array
123:      */
124:     protected function getOldInput()
125:     {
126:         return $this->getSession()
127:             ->getFlashBag()
128:             ->peek('_old_input', []);
129:     }
130: 
131:     /**
132:      * Get the event dispatcher instance.
133:      *
134:      * @return \Symfony\Component\EventDispatcher\EventDispatcherInterface
135:      */
136:     protected function getEventDispatcher()
137:     {
138:         return $this->container->resolve('Symfony\Component\EventDispatcher\EventDispatcherInterface');
139:     }
140: 
141:     /**
142:      * Dispatch an event.
143:      *
144:      * @param  string $name
145:      * @param  Event  $event
146:      *
147:      * @return mixed
148:      */
149:     protected function dispatchEvent($name, Event $event)
150:     {
151:         return $this->getEventDispatcher()
152:             ->dispatch($name, $event);
153:     }
154: 
155:     /**
156:      * Log a message.
157:      *
158:      * @param  string $level   https://github.com/php-fig/log/blob/master/Psr/Log/LogLevel.php
159:      * @param  string $message
160:      * @param  array  $context
161:      *
162:      * @return void
163:      */
164:     protected function log($level, $message, array $context = array())
165:     {
166:         $this->getLogger()
167:             ->log($level, $message, $context);
168:     }
169: 
170:     /**
171:      * Get the logger instance.
172:      *
173:      * @return \Psr\Log\LoggerInterface
174:      */
175:     protected function getLogger()
176:     {
177:         return $this->container->resolve('Psr\Log\LoggerInterface');
178:     }
179: 
180:     /**
181:      * Create a response.
182:      *
183:      * @param  string  $content
184:      * @param  integer $statusCode
185:      *
186:      * @return \Symfony\Component\HttpFoundation\Response
187:      */
188:     protected function response($content, $statusCode = 200)
189:     {
190:         return new Response($content, $statusCode);
191:     }
192: 
193:     /**
194:      * Create a redirect response.
195:      *
196:      * @param  string  $name       Name of the route to redirect to
197:      * @param  array   $params     Route parameters
198:      * @param  integer $statusCode Default: 302
199:      *
200:      * @return \Symfony\Component\HttpFoundation\RedirectResponse
201:      */
202:     protected function redirect($name, array $params = array(), $statusCode = 302)
203:     {
204:         return new RedirectResponse($this->url($name, $params), $statusCode);
205:     }
206: 
207:     /**
208:      * Create a JSON response.
209:      *
210:      * @param  array   $data
211:      * @param  integer $statusCode
212:      *
213:      * @return \Symfony\Component\HttpFoundation\JsonResponse
214:      */
215:     protected function json(array $data, $statusCode = 200)
216:     {
217:         return new JsonResponse($data, $statusCode);
218:     }
219: }
220: 
Autarky Framework API documentation generated by ApiGen