Example #1
0
 protected function getRealPath($phpFile)
 {
     if (Str::startsWith($this->path, ['phar'])) {
         $realPath = $phpFile->getPath() . '/' . $phpFile->getFilename();
     } else {
         $realPath = $phpFile->getRealPath();
     }
     return $realPath;
 }
Example #2
0
 /**
  * return an array of values filtered by the key ending with $endswith
  * @param $arr []
  * @param $endswith string
  * @return array
  */
 public static function filterKeyEndsWith(&$arr, $endswith)
 {
     $results = array();
     foreach ($arr as $key => $value) {
         if (Str::endsWith($key, $endswith) === true) {
             $results[$key] = $value;
         }
     }
     return $results;
 }
Example #3
0
 /**
  * @param $namespace
  * @throws \Exception
  */
 private function findAndInclude($namespace)
 {
     $finder = new Finder();
     $namespaceParts = explode('\\', $namespace);
     $class = array_pop($namespaceParts);
     $finder->name($class . '.php');
     $finder->in(getcwd() . $this->vendorExtension);
     //local vendor dir
     if (iterator_count($finder) === 0) {
         throw new \Exception('Cannot find detected class');
     }
     foreach ($finder as $file) {
         //TODO - there must be a better way to do this? Seems inefficient if we are checking namespace and path. Why not just check the path?
         if ($file->isFile() && (Str::contains($file->getRelativePath(), 'RoboCommand') !== false || Str::contains($file->getRelativePath(), 'Command'))) {
             include_once $file->getRealPath();
         }
     }
 }
Example #4
0
    public function testGetBefore()
    {
        /**
         * test when the token is at the end
         * should return an array with a 12345 at the start and 78na123 at the end
         */
        $string = '12345an5678 an91234an56an78na123an';
        $aft = Str::getBefore($string, 'an');
        $this->assertArrayHasKey('an', $aft);
        $this->assertEquals("12345", $aft['an'][0]);
        $this->assertEquals("78na123", $aft['an'][4]);
        /**
         * test when the token is at the start
         * should return a blank entry at the start and not 78na123 at the end
         */
        $string = 'an12345an5678 an91234an56an78na123';
        $aft = Str::getBefore($string, 'an');
        $this->assertArrayHasKey('an', $aft);
        $this->assertEquals("", $aft['an'][0]);
        $this->assertCount(5, $aft['an']);
        /**
         * test when the token is in the string
         * should not return 1 as the last member
         */
        $string = '12345an5678 an91234an56an78na123an1';
        $aft = Str::getBefore($string, 'an');
        $this->assertArrayHasKey('an', $aft);
        $this->assertCount(5, $aft['an']);
        /**
         * test when the token is at the start and end
         * should have nothing at the start and 78na123 at the end
         */
        $string = 'an12345an5678 an91234an56an78na123an';
        $aft = Str::getBefore($string, 'an');
        $this->assertArrayHasKey('an', $aft);
        $this->assertCount(6, $aft['an']);
        $this->assertEquals("", $aft['an'][0]);
        $this->assertEquals("78na123", $aft['an'][5]);
        /**
         * test when the phrase is not in the string
         */
        $string = 'an12345an5678 an91234an56an78na123an';
        $aft = Str::getBefore($string, 'fail');
        $this->assertArrayHasKey('fail', $aft);
        $this->assertCount(0, $aft['fail']);
        //Simple string
        $string = 'this is some text that we can search through';
        $b4 = Str::getBefore($string, 'some');
        $this->assertCount(1, $b4);
        $this->assertRegExp('/^this is /', $b4['some'][0]);
        //array of needles
        $b4 = Str::getBefore($string, ['some', 'that', 'rch', 'at', 'heynongman']);
        $this->assertCount(5, $b4);
        $this->assertArrayHasKey('some', $b4);
        $this->assertArrayHasKey('that', $b4);
        $this->assertArrayHasKey('rch', $b4);
        $this->assertArrayHasKey('at', $b4);
        $this->assertArrayHasKey('heynongman', $b4);
        $this->assertCount(0, $b4['heynongman']);
        $this->assertRegExp('/^this is /', $b4['some'][0]);
        $this->assertRegExp('/^this is some text /', $b4['that'][0]);
        $this->assertRegExp('/^this is some text that we can sea/', $b4['rch'][0]);
        $this->assertRegExp('/^this is some text th/', $b4['at'][0]);
        //Test the classics
        $string = <<<EOH
consectetur adipiscing elit. Morbi id gravida quam. Lorem ipsum dolor sit amet
Vestibulum vel nisl id dolor tincidunt finibus id et erat. Nullam pharetra est sed pellentesque suscipit.
Integer sit amet justo condimentum elit aliquam blandit. Cras dapibus est ac risus bibendum porta.
Nullam gravida nisl sem, nec fermentum ipsum vestibulum eu. Nam posuere velit sit amet nunc consequat varius.
Aenean ante felis, suscipit ut dui a, posuere eleifend est. Vestibulum sit amet orci et libero dapibus malesuada.
Phasellus sagittis erat ac suscipit bibendum. Donec at tortor rutrum, ultrices mi vel, consequat risus. Maecenas feugiat tempus sem eu interdum.
EOH;
        $b4 = Str::getBefore($string, ['id', 'ip', ' ip', 'Cras', 'posuere']);
        $this->assertCount(5, $b4);
        $this->assertArrayHasKey('id', $b4);
        $this->assertArrayHasKey('ip', $b4);
        $this->assertArrayHasKey(' ip', $b4);
        $this->assertArrayHasKey('Cras', $b4);
        $this->assertArrayHasKey('posuere', $b4);
        $this->assertRegExp('/^consectetur adipiscing elit. Morbi /', $b4['id'][0]);
        $this->assertRegExp('/^consectetur ad/', $b4['ip'][0]);
        $this->assertRegExp('/^consectetur adipiscing elit. Morbi id gravida quam. Lorem/', $b4[' ip'][0]);
        $this->assertRegExp('/^consectetur adipiscing elit. Morbi id gravida quam. Lorem ipsum dolor sit amet
Vestibulum vel nisl id dolor tincidunt finibus id et erat. Nullam pharetra est sed pellentesque suscipit.
Integer sit amet justo condimentum elit aliquam blandit. /', $b4['Cras'][0]);
        $this->assertRegExp('/^consectetur adipiscing elit. Morbi id gravida quam. Lorem ipsum dolor sit amet
Vestibulum vel nisl id dolor tincidunt finibus id et erat. Nullam pharetra est sed pellentesque suscipit.
Integer sit amet justo condimentum elit aliquam blandit. Cras dapibus est ac risus bibendum porta.
Nullam gravida nisl sem, nec fermentum ipsum vestibulum eu. Nam /', $b4['posuere'][0]);
    }