<?php
namespace controls;
class Site
{
  private static function snakeToCamel( $snake )
  {
    return lcfirst( str_replace( '_', '', ucwords( $snake, '_' ) ) );
  }

  public static function route()
  {
    global $user;

    $module = \S::get( 'module' );
    $action = \S::get( 'action' );

    if ( !\controls\Users::permissions( $user['id'], $module, $action ) )
      return;

    // New Controllers namespace (camelCase methods)
    $controller_class = '\Controllers\\';
    $parts = explode( '_', $module );
    if ( is_array( $parts ) ) foreach ( $parts as $part )
      $controller_class .= ucfirst( $part );
    $controller_class .= 'Controller';

    $camel_action = self::snakeToCamel( $action );

    if ( class_exists( $controller_class ) and method_exists( $controller_class, $camel_action ) )
      return call_user_func_array( array( $controller_class, $camel_action ), array() );

    // Legacy controls namespace (snake_case methods)
    $class = '\controls\\';
    $parts = explode( '_', $module );
    if ( is_array( $parts ) ) foreach ( $parts as $part )
      $class .= ucfirst( $part );

    if ( class_exists( $class ) and method_exists( new $class, $action ) )
      return call_user_func_array( array( $class, $action ), array() );
  }
}
