Advertisement
yesamarcos

Controller usa Library para listar métodos

Jan 20th, 2018
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.94 KB | None | 0 0
  1. // ####################################################################
  2. // Controller #########################################################
  3. // ####################################################################
  4.  
  5. <?php defined('BASEPATH') OR exit('No direct script access allowed');
  6.  
  7. class Controllers extends CI_Controller {
  8.  
  9.     public function __construct(){
  10.         parent::__construct();
  11.         $this->load->library('controllerlist');
  12.     }
  13.  
  14.     /**
  15.      * Registrando os controllers no banco de dados ...
  16.      * @return [type] [description]
  17.      */
  18.     // public function registerController(){
  19.     private function registerController(){
  20.         exit();
  21.         // Excluindo todos os registros de métodos ...
  22.         $trunc = $this->db->query("TRUNCATE TABLE sys_metodos");
  23.         $trunc_1 = $this->db->query("TRUNCATE TABLE sys_permissoes");
  24.         // Reescrevendo todos os registros de métodos ...
  25.         $ctrls = $this->controllerlist->getControllers();
  26.         $c = 0;
  27.         $count = 0;
  28.         foreach ($ctrls as $index => $val){
  29.             foreach ($val as $indice) {
  30.                 // echo $c++ ." - ". $index ." - ".  $indice . "<br>";
  31.                 $data = array(
  32.                     'classe'            => $index,                  // controller
  33.                     'metodo'            => $indice,                 // metodo
  34.                     'apelido'           => "{$index}/{$indice}",    // controller/metodo
  35.                     'privado'           => 0,                       // 0 publico 1 privado                 
  36.                     'apelido_classe'    => $index,                  // controller
  37.                     'modulo'            => "Pesquisa"
  38.                 );
  39.                 $sql = $this->db->insert('sys_metodos', $data);
  40.                 $data_1 = array(
  41.                     'id_metodo' => $this->db->insert_id(),
  42.                     'id_usuario' => 1
  43.                 );
  44.                 $sql_1 = $this->db->insert('sys_permissoes', $data_1);
  45.             }
  46.         } echo "Terminado.";
  47.     }
  48. }
  49.  
  50. /* End of file controllers.php */
  51. /* Location: ./application/controllers/controllers.php */
  52.  
  53. // ####################################################################
  54. // Biblioteca #########################################################
  55. // ####################################################################
  56.  
  57. <?php if (!defined('BASEPATH')) exit('No direct script access allowed');
  58. /***
  59.  * File: (Codeigniterapp)/libraries/Controllerlist.php
  60.  *
  61.  * A simple library to list all your controllers with their methods.
  62.  * This library will return an array with controllers and methods
  63.  *
  64.  * The library will scan the "controller" directory and (in case of) one (1) subdirectory level deep
  65.  * for controllers
  66.  *
  67.  * Usage in one of your controllers:
  68.  *
  69.  * $this->load->library('controllerlist');
  70.  * print_r($this->controllerlist->getControllers());
  71.  *
  72.  * @author Peter Prins
  73.  */
  74. class ControllerList {
  75.  
  76.     /**
  77.      * Codeigniter reference
  78.      */
  79.     private $CI;
  80.  
  81.     /**
  82.      * Array that will hold the controller names and methods
  83.      */
  84.     private $aControllers;
  85.  
  86.     // Construct
  87.     function __construct() {
  88.         // Get Codeigniter instance
  89.         $this->CI = get_instance();
  90.  
  91.         // Get all controllers
  92.         $this->setControllers();
  93.     }
  94.  
  95.     /**
  96.      * Return all controllers and their methods
  97.      * @return array
  98.      */
  99.     public function getControllers() {
  100.         return $this->aControllers;
  101.     }
  102.  
  103.     /**
  104.      * Set the array holding the controller name and methods
  105.      */
  106.     public function setControllerMethods($p_sControllerName, $p_aControllerMethods) {
  107.         $this->aControllers[$p_sControllerName] = $p_aControllerMethods;
  108.     }
  109.  
  110.     /**
  111.      * Search and set controller and methods.
  112.      */
  113.     private function setControllers() {
  114.         // Loop through the controller directory
  115.         foreach(glob(APPPATH . 'controllers/*') as $controller) {
  116.  
  117.             // if the value in the loop is a directory loop through that directory
  118.             if(is_dir($controller)) {
  119.                 // Get name of directory
  120.                 $dirname = basename($controller, EXT);
  121.  
  122.                 // Loop through the subdirectory
  123.                 foreach(glob(APPPATH . 'controllers/'.$dirname.'/*') as $subdircontroller) {
  124.                     // Get the name of the subdir
  125.                     $subdircontrollername = basename($subdircontroller, EXT);
  126.  
  127.                     // Load the controller file in memory if it's not load already
  128.                     if(!class_exists($subdircontrollername)) {
  129.                         $this->CI->load->file($subdircontroller);
  130.                     }
  131.                     // Add the controllername to the array with its methods
  132.                     $aMethods = get_class_methods($subdircontrollername);
  133.                     $aUserMethods = array();
  134.                     foreach($aMethods as $method) {
  135.                         if($method != '__construct' && $method != 'get_instance' && $method != $subdircontrollername) {
  136.                             $aUserMethods[] = $method;
  137.                         }
  138.                     }
  139.                     $this->setControllerMethods($subdircontrollername, $aUserMethods);                                      
  140.                 }
  141.             }
  142.             else if(pathinfo($controller, PATHINFO_EXTENSION) == "php"){
  143.                 // value is no directory get controller name                
  144.                 $controllername = basename($controller, EXT);
  145.  
  146.                 // Load the class in memory (if it's not loaded already)
  147.                 if(!class_exists($controllername)) {
  148.                     $this->CI->load->file($controller);
  149.                 }
  150.  
  151.                 // Add controller and methods to the array
  152.                 $aMethods = get_class_methods($controllername);
  153.                 $aUserMethods = array();
  154.                 if(is_array($aMethods)){
  155.                     foreach($aMethods as $method) {
  156.                         if($method != '__construct' && $method != 'get_instance' && $method != $controllername) {
  157.                             $aUserMethods[] = $method;
  158.                         }
  159.                     }
  160.                 }
  161.  
  162.                 $this->setControllerMethods($controllername, $aUserMethods);                                
  163.             }
  164.         }  
  165.     }
  166. }
  167. // EOF
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement