Пример #1
0
 /**
  * Instantiates this class with the specified service name and log file path.
  * If the file doesn't exist, and $autoCreate is TRUE, then the file will be
  * created. Note that the target directory *must* exist before the file will
  * be created, or else an exception will be thrown.
  *
  * @param string $serviceName of this log file
  * @param string $filePath to log file
  * @param boolean $autoCreate TRUE to create the file if missing
  */
 public function __construct($serviceName, $filePath, $autoCreate = true)
 {
     // Always call parent constructor.
     parent::__construct($serviceName);
     Assert::that(['condition' => !System::isDir($filePath), 'message' => 'The error log file path [%s] cannot be a directory', 'args' => [$filePath], 'type' => Error::TYPE_ARGUMENT]);
     if (!System::isFile($filePath)) {
         Assert::that(['condition' => $autoCreate, 'message' => 'The error log file [%s] is not present', 'args' => [$filePath], 'type' => Error::TYPE_ARGUMENT]);
         Assert::that(['condition' => System::touch($filePath), 'message' => 'The error log file [%s] cannot be created', 'args' => [$filePath], 'type' => Error::TYPE_RUNTIME]);
     }
     // At this point, a production environment should be already set up to
     // include this file or create it when necessary.
     $this->fileHandle = System::openFile($filePath, 'a');
     Assert::that(['condition' => $this->fileHandle, 'message' => 'Unable to open error log file [%s] for writing', 'args' => [$filePath], 'type' => Error::TYPE_RUNTIME]);
 }
Пример #2
0
 /**
  * Returns a list of filters for the action at the specified script path.
  * Search for filters starts at $routePath and ends at $scriptPath, so filters
  * are first found on the top level directory and ends at the deepest path.
  *
  * @param string $routePath top level directory to start searching from
  * @param string $matchUri farthest path to traverse
  * @param \Fine47\MicroRouter\Interfaces\Query $query for the action
  * @return array list of filters found, an empty array otherwise
  */
 protected function getActionFilters($routePath, $matchUri, Interfaces\Query $query)
 {
     $filters = [];
     // Get the name of PHP script that should include the filters.
     $filtersScriptName = $this->getFilterScriptName($query);
     // Start searching from top directory.
     $filtersScriptPath = $routePath;
     // Sub-directories to dig into.
     $subDirectories = explode('/', $matchUri);
     // Add also the top directory.
     array_unshift($subDirectories, '');
     // Cycle through the route path and look for filters starting at top level.
     while (!empty($subDirectories)) {
         // Go to the next sub-directory.
         $filtersScriptPath .= '/' . array_shift($subDirectories);
         // Construct path to possible PHP script.
         $filtersScript = $filtersScriptPath . '/' . $filtersScriptName;
         // Does it exist?
         if (System::isFile($filtersScript)) {
             // Read it.
             $filtersList = (require_once $filtersScript);
             // Result should be an array.
             if (is_array($filtersList) && !empty($filtersList)) {
                 foreach ($filtersList as $filterName => $filter) {
                     if (!isset($filters[$filterName]) && $filter instanceof Interfaces\Filter) {
                         $filters[$filterName] = $filter;
                     }
                 }
             }
             // GC.
             unset($filtersList);
         }
     }
     return $filters;
 }